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
221 def from_json(json, opts=OPTS)
222   from_json_node(Sequel.parse_json(json), opts)
223 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
234 def from_json_node(hash, opts=OPTS)
235   unless hash.is_a?(Hash)
236     raise Error, "parsed json doesn't return a hash"
237   end
238 
239   populate_associations = {}
240 
241   if assocs = opts[:associations]
242     assocs = case assocs
243     when Symbol
244       {assocs=>OPTS}
245     when Array
246       assocs_tmp = {}
247       assocs.each{|v| assocs_tmp[v] = OPTS}
248       assocs_tmp
249     when Hash
250       assocs
251     else
252       raise Error, ":associations should be Symbol, Array, or Hash if present"
253     end
254 
255     assocs.each do |assoc, assoc_opts|
256       if assoc_values = hash.delete(assoc.to_s)
257         unless r = model.association_reflection(assoc)
258           raise Error, "Association #{assoc} is not defined for #{model}"
259         end
260 
261         populate_associations[assoc] = if r.returns_array?
262           raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
263           assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
264         else
265           raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
266           assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
267         end
268       end
269     end
270   end
271 
272   if fields = opts[:fields]
273     set_fields(hash, fields, opts)
274   else
275     set(hash)
276   end
277 
278   populate_associations.each do |assoc, values|
279     associations[assoc] = values
280   end
281 
282   self
283 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
295 def json_serializer_opts(opts=OPTS)
296   @json_serializer_opts = (@json_serializer_opts||OPTS).merge(opts)
297 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
316 def to_json(*a)
317   opts = model.json_serializer_opts
318   opts = opts.merge(@json_serializer_opts) if @json_serializer_opts
319   if (arg_opts = a.first).is_a?(Hash)
320     opts = opts.merge(arg_opts)
321     a = []
322   end
323 
324   vals = values
325   cols = if only = opts[:only]
326     Array(only)
327   else
328     vals.keys - Array(opts[:except])
329   end
330 
331   h = {}
332 
333   cols.each{|c| h[c.to_s] = get_column_value(c)}
334   if inc = opts[:include]
335     if inc.is_a?(Hash)
336       inc.each do |k, v|
337         if k.is_a?(Sequel::SQL::AliasedExpression)
338           key_name = k.alias.to_s
339           k = k.expression
340         else
341           key_name = k.to_s
342         end
343 
344         v = v.empty? ? [] : [v]
345         h[key_name] = JsonSerializer.object_to_json_data(public_send(k), *v)
346       end
347     else
348       Array(inc).each do |c|
349         if c.is_a?(Sequel::SQL::AliasedExpression)
350           key_name = c.alias.to_s
351           c = c.expression
352         else
353           key_name = c.to_s
354         end
355 
356         h[key_name] = JsonSerializer.object_to_json_data(public_send(c))
357       end
358     end
359   end
360 
361   if root = opts[:root]
362     unless root.is_a?(String)
363       root = model.send(:underscore, model.send(:demodulize, model.to_s))
364     end
365     h = {root => h}
366   end
367 
368   h = yield h if defined?(yield)
369   Sequel.object_to_json(h, *a)
370 end
to_json_data(*args, &block)

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

[show source]
    # File lib/sequel/plugins/json_serializer.rb
373 def to_json_data(*args, &block)
374   if block
375     to_json(*args){|x| return block.call(x)}
376   else
377     to_json(*args){|x| return x}
378   end
379 end