module Sequel::Plugins::JsonSerializer::DatasetMethods
Public Instance Methods
Store default options used when calling to_json
on this dataset. These options take precedence over the class level options, and can be overridden by passing options directly to to_json.
# File lib/sequel/plugins/json_serializer.rb 371 def json_serializer_opts(opts=OPTS) 372 clone(:json_serializer_opts=>opts) 373 end
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.
- :instance_block
-
A block to pass to
to_json
for each value in the dataset (or :array option). - :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.
# File lib/sequel/plugins/json_serializer.rb 391 def to_json(*a) 392 opts = model.json_serializer_opts 393 394 if ds_opts = @opts[:json_serializer_opts] 395 opts = opts.merge(ds_opts) 396 end 397 398 if (arg = a.first).is_a?(Hash) 399 opts = opts.merge(arg) 400 a = [] 401 end 402 403 case collection_root = opts[:root] 404 when nil, false, :instance 405 collection_root = false 406 else 407 opts = opts.dup 408 unless collection_root == :both 409 opts.delete(:root) 410 end 411 unless collection_root.is_a?(String) 412 collection_root = model.send(:pluralize, model.send(:underscore, model.send(:demodulize, model.to_s))) 413 end 414 end 415 416 res = if row_proc || @opts[:eager_graph] 417 array = if opts[:array] 418 opts = opts.dup 419 opts.delete(:array) 420 else 421 all 422 end 423 array.map{|obj| Literal.new(Sequel.object_to_json(obj, opts, &opts[:instance_block]))} 424 else 425 all 426 end 427 428 res = {collection_root => res} if collection_root 429 res = yield res if block_given? 430 431 Sequel.object_to_json(res, *a) 432 end