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 380 def json_serializer_opts(opts=OPTS) 381 clone(:json_serializer_opts=>opts) 382 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 |
: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 400 def to_json(*a) 401 opts = model.json_serializer_opts 402 403 if ds_opts = @opts[:json_serializer_opts] 404 opts = opts.merge(ds_opts) 405 end 406 407 if (arg = a.first).is_a?(Hash) 408 opts = opts.merge(arg) 409 a = [] 410 end 411 412 case collection_root = opts[:root] 413 when nil, false, :instance 414 collection_root = false 415 else 416 opts = opts.dup 417 unless collection_root == :both 418 opts.delete(:root) 419 end 420 unless collection_root.is_a?(String) 421 collection_root = model.send(:pluralize, model.send(:underscore, model.send(:demodulize, model.to_s))) 422 end 423 end 424 425 res = if row_proc || @opts[:eager_graph] 426 array = if opts[:array] 427 opts = opts.dup 428 opts.delete(:array) 429 else 430 all 431 end 432 JsonSerializer.object_to_json_data(array, opts, &opts[:instance_block]) 433 else 434 all 435 end 436 437 res = {collection_root => res} if collection_root 438 res = yield res if defined?(yield) 439 440 Sequel.object_to_json(res, *a) 441 end