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 316
316:         def to_json(*a)
317:           if opts = a.first.is_a?(Hash)
318:             opts = model.json_serializer_opts.merge(a.first)
319:             a = []
320:           else
321:             opts = model.json_serializer_opts
322:           end
323: 
324:           case collection_root = opts[:root]
325:           when nil, false, :instance
326:             collection_root = false
327:           else
328:             opts = opts.dup
329:             unless collection_root == :both
330:               opts.delete(:root)
331:             end
332:             unless collection_root.is_a?(String)
333:               collection_root = model.send(:pluralize, model.send(:underscore, model.to_s))
334:             end
335:           end
336: 
337:           res = if row_proc 
338:             array = if opts[:array]
339:               opts = opts.dup
340:               opts.delete(:array)
341:             else
342:               all
343:             end
344:             array.map{|obj| Literal.new(Sequel.object_to_json(obj, opts))}
345:            else
346:             all
347:           end
348: 
349:           if collection_root
350:             Sequel.object_to_json({collection_root => res}, *a)
351:           else
352:             Sequel.object_to_json(res, *a)
353:           end
354:         end

[Validate]