Method: BinaryCodec::STObject.from_parser

Defined in:
lib/binary-codec/types/st_object.rb

.from_parser(parser, size_hint = nil) ⇒ STObject

Construct a STObject from a BinaryParser

Parameters:

  • parser (BinaryParser)

    BinaryParser to read STObject from

  • size_hint (Integer) (defaults to: nil)

    Optional size hint for the object

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/binary-codec/types/st_object.rb', line 23

def self.from_parser(parser, size_hint = nil)
  list = BytesList.new
  bytes = BinarySerializer.new(list)

  until parser.end?
    field = parser.read_field

    break if field.name == OBJECT_END_MARKER

    associated_value = parser.read_field_value(field)

    bytes.write_field_and_value(field, associated_value)
    bytes.put(OBJECT_END_MARKER_BYTE) if field.type == ST_OBJECT
  end

  STObject.new(list.to_bytes)
end