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 178 178: def from_json(json, opts=OPTS) 179: from_json_node(Sequel.parse_json(json), opts) 180: 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 191 191: def from_json_node(hash, opts=OPTS) 192: unless hash.is_a?(Hash) 193: raise Error, "parsed json doesn't return a hash" 194: end 195: 196: populate_associations = {} 197: 198: if assocs = opts[:associations] 199: assocs = case assocs 200: when Symbol 201: {assocs=>{}} 202: when Array 203: assocs_tmp = {} 204: assocs.each{|v| assocs_tmp[v] = {}} 205: assocs_tmp 206: when Hash 207: assocs 208: else 209: raise Error, ":associations should be Symbol, Array, or Hash if present" 210: end 211: 212: assocs.each do |assoc, assoc_opts| 213: if assoc_values = hash.delete(assoc.to_s) 214: unless r = model.association_reflection(assoc) 215: raise Error, "Association #{assoc} is not defined for #{model}" 216: end 217: 218: populate_associations[assoc] = if r.returns_array? 219: raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array) 220: assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)} 221: else 222: raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array) 223: assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts) 224: end 225: end 226: end 227: end 228: 229: if fields = opts[:fields] 230: set_fields(hash, fields, opts) 231: else 232: set(hash) 233: end 234: 235: populate_associations.each do |assoc, values| 236: associations[assoc] = values 237: end 238: 239: self 240: 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. |
# File lib/sequel/plugins/json_serializer.rb, line 257 257: def to_json(*a) 258: if opts = a.first.is_a?(Hash) 259: opts = model.json_serializer_opts.merge(a.first) 260: a = [] 261: else 262: opts = model.json_serializer_opts 263: end 264: vals = values 265: cols = if only = opts[:only] 266: Array(only) 267: else 268: vals.keys - Array(opts[:except]) 269: end 270: 271: h = {} 272: 273: cols.each{|c| h[c.to_s] = send(c)} 274: if inc = opts[:include] 275: if inc.is_a?(Hash) 276: inc.each do |k, v| 277: v = v.empty? ? [] : [v] 278: h[k.to_s] = case objs = send(k) 279: when Array 280: objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))} 281: else 282: Literal.new(Sequel.object_to_json(objs, *v)) 283: end 284: end 285: else 286: Array(inc).each{|c| h[c.to_s] = send(c)} 287: end 288: end 289: h = {model.send(:underscore, model.to_s) => h} if opts[:root] 290: Sequel.object_to_json(h, *a) 291: end