Method: BinaryCodec::STObject#to_json

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

#to_jsonString

Method to get the JSON interpretation of self.bytes

Returns:

  • (String)

    A stringified JSON object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/binary-codec/types/st_object.rb', line 44

def to_json()
  parser = BinaryParser.new(to_s)
  accumulator = {}

  until parser.end?
    field = parser.read_field
    break if field.name == OBJECT_END_MARKER # Break if the object end marker is reached
    value = parser.read_field_value(field).to_json
    value = JSON.parse(value) if field.type == ST_OBJECT || field.type == Amount
    accumulator[field.name] = value
  end

  JSON.generate(accumulator)
end