My Ruby class Routeguide::Rectangle
object looks like this, (Pay attention on lo & hi variables, those are objects of class Routeguide::Point
)
<Routeguide::Rectangle: lo: <Routeguide::Point: latitude: 400000000, longitude: -750000000>, hi: <Routeguide::Point: latitude: 420000000, longitude: -730000000>>`
I have correct json string for this object,
json_string = '{"lo":{"latitude":400000000,"longitude":-750000000},"hi":{"latitude":420000000,"longitude":-730000000}}'
I need to parse this json string again to create Routeguide::Rectangle
class object like:
<Routeguide::Rectangle: lo: <Routeguide::Point: latitude: 400000000, longitude: -750000000>, hi: <Routeguide::Point: latitude: 420000000, longitude: -730000000>>`
I am using below code to achieve this:
JSON.parse(json_string, object_class: Routeguide::Rectangle)
But that is giving me error on JSON.parse method:
#<ArgumentError: Unknown field: latitude>
Stacktrace:
[
"/Users/psen/.rbenv/versions/2.7.7/lib/ruby/2.7.0/json/common.rb:156:in `[]='",
"/Users/psen/.rbenv/versions/2.7.7/lib/ruby/2.7.0/json/common.rb:156:in `parse'",
"/Users/psen/.rbenv/versions/2.7.7/lib/ruby/2.7.0/json/common.rb:156:in `parse'"
]
Assuming that you're also generating that json string, you could look into using JSON Additions. You'd have to define a Custom Addition to help with the serialization & deserialization.