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 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, wraps the collection in a root object using the pluralized, underscored model name as the key. 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. If set to a string, wraps the collection in a root object using the string as the key.

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 349
349:         def to_json(*a)
350:           if opts = a.first.is_a?(Hash)
351:             opts = model.json_serializer_opts.merge(a.first)
352:             a = []
353:           else
354:             opts = model.json_serializer_opts
355:           end
356: 
357:           case collection_root = opts[:root]
358:           when nil, false, :instance
359:             collection_root = false
360:           else
361:             opts = opts.dup
362:             unless collection_root == :both
363:               opts.delete(:root)
364:             end
365:             unless collection_root.is_a?(String)
366:               collection_root = model.send(:pluralize, model.send(:underscore, model.send(:demodulize, model.to_s)))
367:             end
368:           end
369: 
370:           res = if row_proc 
371:             array = if opts[:array]
372:               opts = opts.dup
373:               opts.delete(:array)
374:             else
375:               all
376:             end
377:             array.map{|obj| Literal.new(Sequel.object_to_json(obj, opts))}
378:            else
379:             all
380:           end
381: 
382:           if collection_root
383:             Sequel.object_to_json({collection_root => res}, *a)
384:           else
385:             Sequel.object_to_json(res, *a)
386:           end
387:         end

[Validate]