module Sequel::Plugins::JsonSerializer::InstanceMethods

Public Instance Methods

from_json(json, opts=OPTS) click to toggle source

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

    # File lib/sequel/plugins/json_serializer.rb
203 def from_json(json, opts=OPTS)
204   from_json_node(Sequel.parse_json(json), opts)
205 end
from_json_node(hash, opts=OPTS) click to toggle source

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.

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

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":"..."}]'
    # File lib/sequel/plugins/json_serializer.rb
277 def json_serializer_opts(opts=OPTS)
278   @json_serializer_opts = (@json_serializer_opts||OPTS).merge(opts)
279 end
to_json(*a) { |h| ... } click to toggle source

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.

    # File lib/sequel/plugins/json_serializer.rb
298 def to_json(*a)
299   opts = model.json_serializer_opts
300   opts = opts.merge(@json_serializer_opts) if @json_serializer_opts
301   if (arg_opts = a.first).is_a?(Hash)
302     opts = opts.merge(arg_opts)
303     a = []
304   end
305 
306   vals = values
307   cols = if only = opts[:only]
308     Array(only)
309   else
310     vals.keys - Array(opts[:except])
311   end
312 
313   h = {}
314 
315   cols.each{|c| h[c.to_s] = get_column_value(c)}
316   if inc = opts[:include]
317     if inc.is_a?(Hash)
318       inc.each do |k, v|
319         if k.is_a?(Sequel::SQL::AliasedExpression)
320           key_name = k.alias.to_s
321           k = k.expression
322         else
323           key_name = k.to_s
324         end
325 
326         v = v.empty? ? [] : [v]
327 
328         objs = public_send(k)
329 
330         is_array = if r = model.association_reflection(k)
331           r.returns_array?
332         else
333           objs.is_a?(Array)
334         end
335         
336         h[key_name] = if is_array
337           objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
338         else
339           Literal.new(Sequel.object_to_json(objs, *v))
340         end
341       end
342     else
343       Array(inc).each do |c|
344         if c.is_a?(Sequel::SQL::AliasedExpression)
345           key_name = c.alias.to_s
346           c = c.expression
347         else
348           key_name = c.to_s
349         end
350         h[key_name] = 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 block_given?
363   Sequel.object_to_json(h, *a)
364 end