| Module | Sequel::Plugins::JsonSerializer::InstanceMethods |
| In: |
lib/sequel/plugins/json_serializer.rb
|
Parse the provided JSON, which should return a hash, and process the hash with from_json_node.
# File lib/sequel/plugins/json_serializer.rb, line 207
207: def from_json(json, opts=OPTS)
208: from_json_node(Sequel.parse_json(json), opts)
209: end
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, line 220
220: def from_json_node(hash, opts=OPTS)
221: unless hash.is_a?(Hash)
222: raise Error, "parsed json doesn't return a hash"
223: end
224:
225: populate_associations = {}
226:
227: if assocs = opts[:associations]
228: assocs = case assocs
229: when Symbol
230: {assocs=>OPTS}
231: when Array
232: assocs_tmp = {}
233: assocs.each{|v| assocs_tmp[v] = OPTS}
234: assocs_tmp
235: when Hash
236: assocs
237: else
238: raise Error, ":associations should be Symbol, Array, or Hash if present"
239: end
240:
241: assocs.each do |assoc, assoc_opts|
242: if assoc_values = hash.delete(assoc.to_s)
243: unless r = model.association_reflection(assoc)
244: raise Error, "Association #{assoc} is not defined for #{model}"
245: end
246:
247: populate_associations[assoc] = if r.returns_array?
248: raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
249: assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
250: else
251: raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
252: assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
253: end
254: end
255: end
256: end
257:
258: if fields = opts[:fields]
259: set_fields(hash, fields, opts)
260: else
261: set(hash)
262: end
263:
264: populate_associations.each do |assoc, values|
265: associations[assoc] = values
266: end
267:
268: self
269: end
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, line 281
281: def json_serializer_opts(opts=OPTS)
282: @json_serializer_opts = Hash[@json_serializer_opts||OPTS].merge!(opts)
283: end
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, line 302
302: def to_json(*a)
303: opts = model.json_serializer_opts
304: opts = Hash[opts].merge!(@json_serializer_opts) if @json_serializer_opts
305: if (arg_opts = a.first).is_a?(Hash)
306: opts = Hash[opts].merge!(arg_opts)
307: a = []
308: end
309:
310: vals = values
311: cols = if only = opts[:only]
312: Array(only)
313: else
314: vals.keys - Array(opts[:except])
315: end
316:
317: h = {}
318:
319: cols.each{|c| h[c.to_s] = get_column_value(c)}
320: if inc = opts[:include]
321: if inc.is_a?(Hash)
322: inc.each do |k, v|
323: if k.is_a?(Sequel::SQL::AliasedExpression)
324: key_name = k.alias.to_s
325: k = k.expression
326: else
327: key_name = k.to_s
328: end
329:
330: v = v.empty? ? [] : [v]
331:
332: objs = public_send(k)
333:
334: is_array = if r = model.association_reflection(k)
335: r.returns_array?
336: else
337: objs.is_a?(Array)
338: end
339:
340: h[key_name] = if is_array
341: objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
342: else
343: Literal.new(Sequel.object_to_json(objs, *v))
344: end
345: end
346: else
347: Array(inc).each do |c|
348: if c.is_a?(Sequel::SQL::AliasedExpression)
349: key_name = c.alias.to_s
350: c = c.expression
351: else
352: key_name = c.to_s
353: end
354: h[key_name] = public_send(c)
355: end
356: end
357: end
358:
359: if root = opts[:root]
360: unless root.is_a?(String)
361: root = model.send(:underscore, model.send(:demodulize, model.to_s))
362: end
363: h = {root => h}
364: end
365:
366: h = yield h if block_given?
367: Sequel.object_to_json(h, *a)
368: end