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