Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Complete JSON gem compatibility (recreating objects) #34

tisba opened this Issue Oct 23, 2010 · 3 comments


None yet
4 participants

tisba commented Oct 23, 2010

Currently yajl-ruby does not support building objects from parsed input like the JSON gem does. I've created a gist (http://gist.github.com/642163) to illustrate how this works with the JSON gem. What's very nice about the functionality is that the JSON gem reconstructs the objects recursively (by calling json_create on the class corresponding to the json_class-field - which is configureable via JSON.create_id, IIRC).

This is a really useful feature, if you want to encode (nested) business objects for storing them to a document-oriented database, like CouchDB.

The top-level decoding and setting the correct classes can be done with this "hack":
obj = JSON.parse(obj)
obj = Object.const_get(obj["json_class"]).json_create(obj) if obj["json_class"]

ajsharp commented May 1, 2011


fujin commented Sep 17, 2011

thanks for the hack - would really like it if yajl did this natively +1


brianmario commented Feb 9, 2013

Sorry it's taken me so long to get back to you on this. I've decided against supporting arbitrary object creation because I'm of the stance that you should be fully aware of how objects are being created from data you receive over the wire or otherwise. Never trust what you're given ;)

@brianmario brianmario closed this Feb 9, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment