Class: TypeSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/javonet-ruby-sdk/core/protocol/type_serializer.rb

Class Method Summary collapse

Class Method Details

.double_to_bytes(value) ⇒ Object

64-bit IEEE-754 double → LE bytes



117
118
119
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 117

def self.double_to_bytes(value)
  [value].pack('E').bytes   # little-endian double
end

.float_to_bytes(value) ⇒ Object

32-bit IEEE-754 float → LE bytes



112
113
114
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 112

def self.float_to_bytes(value)
  [value].pack('e').bytes   # little-endian float
end

.int_to_bytes(value) ⇒ Object

32-bit signed int → LE bytes



92
93
94
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 92

def self.int_to_bytes(value)
  [value].pack('l<').bytes
end

.longlong_to_bytes(value) ⇒ Object

64-bit signed → LE bytes



102
103
104
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 102

def self.longlong_to_bytes(value)
  [value].pack('q<').bytes
end

.serialize_bool(bool_value) ⇒ Object



45
46
47
48
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 45

def self.serialize_bool(bool_value)
  encoded_bool_list = bool_value ? [1] : [0]
  [Type::JAVONET_BOOLEAN, encoded_bool_list.length] + encoded_bool_list
end

.serialize_byte(byte_value) ⇒ Object



55
56
57
58
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 55

def self.serialize_byte(byte_value)
  encoded_byte_list = [byte_value].pack('C').bytes
  [Type::JAVONET_BYTE, encoded_byte_list.length] + encoded_byte_list
end

.serialize_char(char_value) ⇒ Object



60
61
62
63
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 60

def self.serialize_char(char_value)
  encoded_char_list = [char_value].pack('C').bytes
  [Type::JAVONET_CHAR, encoded_char_list.length] + encoded_char_list
end

.serialize_command(command) ⇒ Object



29
30
31
32
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 29

def self.serialize_command(command)
  length = int_to_bytes(command.payload.length)
  [Type::COMMAND] + length + [command.runtime_name, command.command_type]
end

.serialize_double(double_value) ⇒ Object



70
71
72
73
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 70

def self.serialize_double(double_value)
  encoded_double_list = double_to_bytes(double_value)
  [Type::JAVONET_DOUBLE, encoded_double_list.length] + encoded_double_list
end

.serialize_float(float_value) ⇒ Object



50
51
52
53
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 50

def self.serialize_float(float_value)
  encoded_float_list = float_to_bytes(float_value)
  [Type::JAVONET_FLOAT, encoded_float_list.length] + encoded_float_list
end

.serialize_int(int_value) ⇒ Object



40
41
42
43
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 40

def self.serialize_int(int_value)
  encoded_int_list = int_to_bytes(int_value)
  [Type::JAVONET_INTEGER, encoded_int_list.length] + encoded_int_list
end

.serialize_longlong(longlong_value) ⇒ Object



65
66
67
68
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 65

def self.serialize_longlong(longlong_value)
  encoded_longlong_list = longlong_to_bytes(longlong_value)
  [Type::JAVONET_LONG_LONG, encoded_longlong_list.length] + encoded_longlong_list
end

.serialize_nilObject



85
86
87
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 85

def self.serialize_nil
  [Type::JAVONET_NULL, 1, 0]
end

.serialize_primitive(payload_item) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 5

def self.serialize_primitive(payload_item)
  if payload_item.nil?
    serialize_nil
  elsif [true, false].include?(payload_item)
    serialize_bool(payload_item)
  elsif payload_item.is_a?(Integer)
    if (-2**31..2**31 - 1).include?(payload_item)
      serialize_int(payload_item)
    elsif (-2**63..2**63 - 1).include?(payload_item)
      serialize_longlong(payload_item)
    else
      serialize_ullong(payload_item)
    end
  elsif payload_item.is_a?(String)
    serialize_string(payload_item)
  elsif payload_item.is_a?(Float)
    serialize_double(payload_item)
  elsif payload_item.is_a?(FalseClass) || payload_item.is_a?(TrueClass)
    serialize_bool(payload_item)
  else
    raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}."
  end
end

.serialize_string(string_value) ⇒ Object



34
35
36
37
38
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 34

def self.serialize_string(string_value)
  encoded_string_list = string_value.bytes
  length = int_to_bytes(encoded_string_list.length)
  [Type::JAVONET_STRING, StringEncodingModeJavonet::UTF8] + length + encoded_string_list
end

.serialize_uint(uint_value) ⇒ Object



75
76
77
78
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 75

def self.serialize_uint(uint_value)
  encoded_uint_list = uint_to_bytes(uint_value)
  [Type::JAVONET_UNSIGNED_INTEGER, encoded_uint_list.length] + encoded_uint_list
end

.serialize_ullong(ullong_value) ⇒ Object



80
81
82
83
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 80

def self.serialize_ullong(ullong_value)
  encoded_ullong_list = ullong_to_bytes(ullong_value)
  [Type::JAVONET_UNSIGNED_LONG_LONG, encoded_ullong_list.length] + encoded_ullong_list
end

.uint_to_bytes(value) ⇒ Object

32-bit unsigned int → LE bytes



97
98
99
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 97

def self.uint_to_bytes(value)
  [value].pack('L<').bytes
end

.ullong_to_bytes(value) ⇒ Object

64-bit unsigned → LE bytes



107
108
109
# File 'lib/javonet-ruby-sdk/core/protocol/type_serializer.rb', line 107

def self.ullong_to_bytes(value)
  [value].pack('Q<').bytes
end