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 369
369:         def to_json(*a)
370:           if opts = a.first.is_a?(Hash)
371:             opts = model.json_serializer_opts.merge(a.first)
372:             a = []
373:           else
374:             opts = model.json_serializer_opts
375:           end
376: 
377:           collection_root = case opts[:root]
378:           when nil, false, :instance
379:             false
380:           when :collection
381:             opts = opts.dup
382:             opts.delete(:root)
383:             opts[:naked] = true unless opts.has_key?(:naked)
384:             true
385:           when :both
386:             true
387:           else
388:             Sequel::Deprecation.deprecate('The to_json :root=>true option will mean :root=>:collection starting in Sequel 4.  Use :root=>:both if you want to wrap both the collection and each item in a wrapper.')
389:             true
390:           end
391: 
392:           res = if row_proc 
393:             array = if opts[:array]
394:               opts = opts.dup
395:               opts.delete(:array)
396:             else
397:               all
398:             end
399:             array.map{|obj| Literal.new(Sequel.object_to_json(obj, opts))}
400:            else
401:             all
402:           end
403: 
404:           if collection_root
405:             Sequel.object_to_json({model.send(:pluralize, model.send(:underscore, model.to_s)) => res}, *a)
406:           else
407:             Sequel.object_to_json(res, *a)
408:           end
409:         end

[Validate]