I found some very old threads, and blogs from other people struggling
with this, but is there a to_json plugin for Sequel?

The confusing thing is there are to_json methods in Sequel, but they
just inspect the 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
       @users.to_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.

-Nate

-- 
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