Module: Sequel::Plugins::JsonSerializer::DatasetMethods

Defined in:
lib/sequel/plugins/json_serializer.rb

Instance Method Summary collapse

Instance Method Details

#to_json(*a) ⇒ Object

Return a JSON string representing an array of all objects in this dataset. Takes the same options as the the instance method, and passes them to every instance.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/sequel/plugins/json_serializer.rb', line 204

def to_json(*a)
  if opts = a.first.is_a?(Hash)
    opts = model.json_serializer_opts.merge(a.first)
    a = []
  else
    opts = model.json_serializer_opts
  end
  res = if row_proc 
    array = if opts[:array]
      opts = opts.dup
      opts.delete(:array)
    else
      all
    end
    array.map{|obj| Literal.new(obj.to_json(opts))}
   else
    all
  end
  opts[:root] ? {model.send(:pluralize, model.send(:underscore, model.to_s)) => res}.to_json(*a) : res.to_json(*a)
end