On Thursday, August 16, 2012 9:12:43 AM UTC-7, Alex Cavalli wrote: > > Hi there, > > I'm using Sequel 3.38.0 and ActiveSupport 3.2.6. > > I'm trying to use the JSON Serializer plugin to generate JSON for some > nested model structures. The project that I'm working on also makes use of > some ActiveSupport features. Unfortunately, this appears to create a > conflict when ActiveSupport overrides the to_json methods on Hash and > Array. I've found a workaround in monkeypatching the monkeypatched Hash and > Array classes, but it's pretty ugly. Is there a better way? > > Here's a sample gist that shows the conflict: > > <script src="https://gist.github.com/3371268.js"> </script> > > Running this script will produce the following: > > Good JSON > > [{"name":"Bob","id":1,"albums":[{"label":"Summer","tracks":[{"id":1,"length":10,"json_class":"Track"},{"id":2,"length":20,"json_class":"Track"}],"id":1,"json_class":"Album"},{"label":"Winter","tracks":[{"id":3,"length":30,"json_class":"Track"},{"id":4,"length":40,"json_class":"Track"}],"id":2,"json_class":"Album"}],"json_class":"Artist"}] > > Bad JSON > > [{"json":"{\"name\":\"Bob\",\"id\":1,\"albums\":[{\"json\":\"{\\\"label\\\":\\\"Summer\\\",\\\"tracks\\\":[{\\\"id\\\":1,\\\"length\\\":10},{\\\"id\\\":2,\\\"length\\\":20}],\\\"id\\\":1,\\\"json_class\\\":\\\"Album\\\"}\"},{\"json\":\"{\\\"label\\\":\\\"Winter\\\",\\\"tracks\\\":[{\\\"id\\\":3,\\\"length\\\":30},{\\\"id\\\":4,\\\"length\\\":40}],\\\"id\\\":2,\\\"json_class\\\":\\\"Album\\\"}\"}],\"json_class\":\"Artist\"}"}] > > Good JSON, ugly workaround > > [{"name":"Bob","id":1,"albums":[{"label":"Summer","tracks":[{"id":1,"length":10,"json_class":"Track"},{"id":2,"length":20,"json_class":"Track"}],"id":1,"json_class":"Album"},{"label":"Winter","tracks":[{"id":3,"length":30,"json_class":"Track"},{"id":4,"length":40,"json_class":"Track"}],"id":2,"json_class":"Album"}],"json_class":"Artist"}] > > > Is there a better workaround or some configuration step I'm missing? >
Don't use ActiveSupport::JSON? Unfortunately, there really isn't a better way. ActiveSupport::JSON basically completely takes over the JSON serialization process in a way incompatible with the standard JSON library. I consider that a bug in ActiveSupport, but it's unlikely they will change it. Even getting obvious bugs fixed in ActiveSupport has been a chore in my experience. Your workaround basically undoes what ActiveSupport does, using the standard JSON library for serializing hashes and arrays, which is why it works. I'm not sure if there is a way to use the ActiveSupport features you want without requiring ActiveSupport::JSON. ActiveSupport 3 is theoretically supposed to be modular in that you can only require certain parts without the whole ball of wax. However, in my experience, requiring one feature often loads other unrelated and undesired features. For example, loading ActiveSupport's Duration class requires loading ActiveSupport's inflection support, even though if you just want to use Duration for time calculations, there is no need for inflections. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/sequel-talk/-/Ie8ExMtTLhQJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
