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. Additionally, respects the following options:
:array : | An array of instances. If this is not provided, calls all on the receiver
to get the array.
|
:root : | If set to :collection, only wraps the collection in a root object. If set
to :instance, only wraps the instances in a root object. If set to :both,
wraps both the collection and instances in a root object. Unfortunately,
for backwards compatibility, if this option is true and doesn‘t match
one of those symbols, it defaults to both. That may change in a future
version, so for forwards compatibility, you should pick a specific symbol
for your desired behavior.
|
[Source]
223: def to_json(*a)
224: if opts = a.first.is_a?(Hash)
225: opts = model.json_serializer_opts.merge(a.first)
226: a = []
227: else
228: opts = model.json_serializer_opts
229: end
230:
231: collection_root = case opts[:root]
232: when nil, false, :instance
233: false
234: when :collection
235: opts = opts.dup
236: opts.delete(:root)
237: opts[:naked] = true unless opts.has_key?(:naked)
238: true
239: else
240: true
241: end
242:
243: res = if row_proc
244: array = if opts[:array]
245: opts = opts.dup
246: opts.delete(:array)
247: else
248: all
249: end
250: array.map{|obj| Literal.new(obj.to_json(opts))}
251: else
252: all
253: end
254:
255: if collection_root
256: {model.send(:pluralize, model.send(:underscore, model.to_s)) => res}.to_json(*a)
257: else
258: res.to_json(*a)
259: end
260: end