Module Sequel::Plugins::JsonSerializer::DatasetMethods
In: lib/sequel/plugins/json_serializer.rb

Methods

to_json  

Public Instance methods

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]

     # File lib/sequel/plugins/json_serializer.rb, line 312
312:         def to_json(*a)
313:           if opts = a.first.is_a?(Hash)
314:             opts = model.json_serializer_opts.merge(a.first)
315:             a = []
316:           else
317:             opts = model.json_serializer_opts
318:           end
319: 
320:           collection_root = case opts[:root]
321:           when nil, false, :instance
322:             false
323:           when :both
324:             true
325:           else
326:             opts = opts.dup
327:             opts.delete(:root)
328:             true
329:           end
330: 
331:           res = if row_proc 
332:             array = if opts[:array]
333:               opts = opts.dup
334:               opts.delete(:array)
335:             else
336:               all
337:             end
338:             array.map{|obj| Literal.new(Sequel.object_to_json(obj, opts))}
339:            else
340:             all
341:           end
342: 
343:           if collection_root
344:             Sequel.object_to_json({model.send(:pluralize, model.send(:underscore, model.to_s)) => res}, *a)
345:           else
346:             Sequel.object_to_json(res, *a)
347:           end
348:         end

[Validate]