module Sequel::Plugins::JsonSerializer::InstanceMethods

  1. lib/sequel/plugins/json_serializer.rb

Public Instance methods

from_json(json, opts=OPTS)

Parse the provided JSON, which should return a hash, and process the hash with from_json_node.

[show source]
    # File lib/sequel/plugins/json_serializer.rb
217 def from_json(json, opts=OPTS)
218   from_json_node(Sequel.parse_json(json), opts)
219 end
from_json_node(hash, opts=OPTS)

Using the provided hash, update the instance with data contained in the hash. By default, just calls set with the hash values.

Options:

:associations

Indicates that the associations cache should be updated by creating a new associated object using data from the hash. Should be a Symbol for a single association, an array of symbols for multiple associations, or a hash with symbol keys and dependent association option hash values.

:fields

Changes the behavior to call set_fields using the provided fields, instead of calling set.

[show source]
    # File lib/sequel/plugins/json_serializer.rb
230 def from_json_node(hash, opts=OPTS)
231   unless hash.is_a?(Hash)
232     raise Error, "parsed json doesn't return a hash"
233   end
234 
235   populate_associations = {}
236 
237   if assocs = opts[:associations]
238     assocs = case assocs
239     when Symbol
240       {assocs=>OPTS}
241     when Array
242       assocs_tmp = {}
243       assocs.each{|v| assocs_tmp[v] = OPTS}
244       assocs_tmp
245     when Hash
246       assocs
247     else
248       raise Error, ":associations should be Symbol, Array, or Hash if present"
249     end
250 
251     assocs.each do |assoc, assoc_opts|
252       if assoc_values = hash.delete(assoc.to_s)
253         unless r = model.association_reflection(assoc)
254           raise Error, "Association #{assoc} is not defined for #{model}"
255         end
256 
257         populate_associations[assoc] = if r.returns_array?
258           raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
259           assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
260         else
261           raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
262           assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
263         end
264       end
265     end
266   end
267 
268   if fields = opts[:fields]
269     set_fields(hash, fields, opts)
270   else
271     set(hash)
272   end
273 
274   populate_associations.each do |assoc, values|
275     associations[assoc] = values
276   end
277 
278   self
279 end
json_serializer_opts(opts=OPTS)

Set the json serialization options that will be used by default in future calls to to_json. This is designed for cases where the model object will be used inside another data structure which to_json is called on, and as such will not allow passing of arguments to to_json.

Example:

obj.json_serializer_opts(only: :name)
[obj].to_json # => '[{"name":"..."}]'
[show source]
    # File lib/sequel/plugins/json_serializer.rb
291 def json_serializer_opts(opts=OPTS)
292   @json_serializer_opts = (@json_serializer_opts||OPTS).merge(opts)
293 end
to_json(*a)

Return a string in JSON format. Accepts the following options:

:except

Symbol or Array of Symbols of columns not to include in the JSON output.

:include

Symbol, Array of Symbols, or a Hash with Symbol keys and Hash values specifying associations or other non-column attributes to include in the JSON output. Using a nested hash, you can pass options to associations to affect the JSON used for associated objects.

:only

Symbol or Array of Symbols of columns to only include in the JSON output, ignoring all other columns.

:root

Qualify the JSON with the name of the object. If a string is given, use the string as the key, otherwise use an underscored version of the model’s name.

[show source]
    # File lib/sequel/plugins/json_serializer.rb
312 def to_json(*a)
313   opts = model.json_serializer_opts
314   opts = opts.merge(@json_serializer_opts) if @json_serializer_opts
315   if (arg_opts = a.first).is_a?(Hash)
316     opts = opts.merge(arg_opts)
317     a = []
318   end
319 
320   vals = values
321   cols = if only = opts[:only]
322     Array(only)
323   else
324     vals.keys - Array(opts[:except])
325   end
326 
327   h = {}
328 
329   cols.each{|c| h[c.to_s] = get_column_value(c)}
330   if inc = opts[:include]
331     if inc.is_a?(Hash)
332       inc.each do |k, v|
333         if k.is_a?(Sequel::SQL::AliasedExpression)
334           key_name = k.alias.to_s
335           k = k.expression
336         else
337           key_name = k.to_s
338         end
339 
340         v = v.empty? ? [] : [v]
341         h[key_name] = JsonSerializer.object_to_json_data(public_send(k), *v)
342       end
343     else
344       Array(inc).each do |c|
345         if c.is_a?(Sequel::SQL::AliasedExpression)
346           key_name = c.alias.to_s
347           c = c.expression
348         else
349           key_name = c.to_s
350         end
351 
352         h[key_name] = JsonSerializer.object_to_json_data(public_send(c))
353       end
354     end
355   end
356 
357   if root = opts[:root]
358     unless root.is_a?(String)
359       root = model.send(:underscore, model.send(:demodulize, model.to_s))
360     end
361     h = {root => h}
362   end
363 
364   h = yield h if defined?(yield)
365   Sequel.object_to_json(h, *a)
366 end
to_json_data(*args)

Convert the receiver to a JSON data structure using the given arguments.

[show source]
    # File lib/sequel/plugins/json_serializer.rb
369 def to_json_data(*args)
370   if defined?(yield)
371     to_json(*args){|x| return yield(x)}
372   else
373     to_json(*args){|x| return x}
374   end
375 end