I have a serialized JSON String (chef role definition actually) and it has a json_class key, making the ruby JSON parser try to force it to be a Chef::Role object. How can I make the parser ignore this key and just simply deserialize into a normal Hash?
I just had the same problem, and found the answer by reading the source of the JSON gem - just unset JSON.create_id before trying to do the parse:
JSON.create_id = nil
JSON.parse('{ "json_class": "Chef::Role" }').class => Hash
EDIT: Note that since version 1.7 of the gem (1.8.0 is current as I type this update), the above hack is no longer necessary. JSON#parse
now ignores json_class
, and JSON#load
should be used instead for unmarshalling dumped objects.
The "json_class" key is there to indicate what Object the json should be un-marshaled as. It is added by JSON.dump. In more recent versions of JSON, JSON.parse
will ignore "json_class", un-marshaling to a Hash. While JSON.load
will un-marshal to the indicated Object (in your case, a Chef::Role).
JSON.parse('{ "json_class": "Chef::Role" }').class => Hash
JSON.load('{ "json_class": "Chef::Role" }').class => Chef::Role
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With