On Jun 5, 7:58 am, Nate Wiger <[email protected]> wrote:
> I found some very old threads, and blogs from other people struggling
> with this, but is there a to_json plugin for Sequel?

One was added earlier this week:
http://tracker.gamemp3s.net/webui/download?id=c008da4cc7aa05f0fc588c28a145a8e862fcaff2

> The confusing thing is there are to_json methods in Sequel, but they
> just inspect the object.

That's because the json library adds to_json to Object.

> For the single-record case, ie:
>
>   class Main < Sinatra::Base
>     get '/users/:id.json' do |id|
>       @user = User[id] or halt 404
>       @user.to_json
>     end
>   end
>
>  I just monkey-patched a method into Sequel::Model that takes
> advantage of Hash/Array.to_json:
>
>   Sequel.extension :inflector
>   class Sequel::Model
>     def to_json
>      {self.class.table_name.to_s.singularize => values}.to_json
>     end
>   end
>
> For the collection case, ie:
>
>    class Main < Sinatra::Base
>      get '/users.json' do
>       �...@users = User.all
>       �[email protected]_json
>      end
>    end
>
> I have this but it needs to be generalized:
>
>   {'users' => @users.collect{|r| {'user' => r.values}}}.to_json
>
> Not sure where to put it because of the fact it's a dataset
> collection.
>
> So, (1) is there already a to_json plugin and (2) if not, input on the
> above approaches?  I can throw it into a plugin; seems reusable enough
> and I need it anyways.

Try out the json_serializer plugin and let me know what you think.

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.

Reply via email to