Class: BinaryCodec::BinarySerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/serdes/binary_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(sink) ⇒ BinarySerializer

Returns a new instance of BinarySerializer.



6
7
8
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 6

def initialize(sink)
  @sink = sink || BytesList.new
end

Instance Method Details

#put(bytes) ⇒ Object



14
15
16
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 14

def put(bytes)
  @sink.put(bytes)
end

#write(value) ⇒ Object



10
11
12
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 10

def write(value)
  value.to_byte_sink(@sink)
end

#write_bytes_list(bytes_list) ⇒ Object



22
23
24
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 22

def write_bytes_list(bytes_list)
  bytes_list.to_byte_sink(@sink)
end

#write_field_and_value(field, value, is_unl_modify_workaround = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 26

def write_field_and_value(field, value, is_unl_modify_workaround = false)
  field_header = FieldHeader.new(type: field.header.type, nth: field.nth)
  type_class = SerializedType.get_type_by_name(field.type)
  associated_value = type_class.from(value)

  if !associated_value.respond_to?(:to_byte_sink) || field.name.nil?
    raise 'Error'
  end

  @sink.put(field_header.to_bytes)

  if field.is_variable_length_encoded
    write_length_encoded(associated_value, is_unl_modify_workaround)
  else
    associated_value.to_byte_sink(@sink)
  end
end

#write_length_encoded(value, is_unl_modify_workaround = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 44

def write_length_encoded(value, is_unl_modify_workaround = false)
  bytes = BytesList.new

  unless is_unl_modify_workaround
    # This part doesn't happen for the Account field in a UNLModify transaction
    value.to_byte_sink(bytes)
  end

  self.put(encode_variable_length(bytes.get_length))
  write_bytes_list(bytes)
end

#write_type(type, value) ⇒ Object



18
19
20
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 18

def write_type(type, value)
  write(type.from(value))
end