On Jun 6, 8:10 am, Nate Wiger <[email protected]> wrote: > > > Try out the json_serializer plugin and let me know what you think. > > Funny, sometimes I feel like I'm tuned-in to the Sequel zeitgeist. :-) > > The plugin looks really nice overall, but I did have a couple > questions. Here's my example code: > > @users = User.all > puts @users.first.to_json > puts @users.to_json > > This yields: > > {"json_class":"User","first":"a","last":"b","dob":"2010-06-05 > 20:16:36 -0700"} > > [{"json_class":"User","first":"a","last":"b","dob":"2010-06-05 > 20:16:36 -0700"}, > {"json_class":"User","first":"b","last":"c","dob":"2010-06-05 > 20:16:36 -0700"}] > > Questions: > > 1) How difficult would it be to turn on the "root node" behavior that > some JSON frameworks expect? So something like: > > @user.to_json(:root => true)
This should be possible, as it's just an additional option. Send in a patch with specs and an update to the RDoc and I'll certainly consider it. > @users.to_json(:root => true) This won't work. @users is an array, and Sequel doesn't override Array#to_json. Now, if @users was a dataset, it could probably work. You might want separate options for the array root node and the individual root nodes, though. > 2) For the options that make sense, would it be possible to propagate > these to class-level settings as well? So: > > Sequel::Model.plugin :json_serializer, :naked => true, :root => > true Sure. It could take an options hash that is used as the default options for that model's instances. I'll accept a patch for this as well, but make sure you follow the example of the other built-in plugins in terms of storing the options and passing them to subclasses. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. 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.
