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. 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 259 259: def to_json(*a) 260: if opts = a.first.is_a?(Hash) 261: opts = model.json_serializer_opts.merge(a.first) 262: a = [] 263: else 264: opts = model.json_serializer_opts 265: end 266: vals = values 267: cols = if only = opts[:only] 268: Array(only) 269: else 270: vals.keys - Array(opts[:except]) 271: end 272: 273: h = {} 274: 275: cols.each{|c| h[c.to_s] = get_column_value(c)} 276: if inc = opts[:include] 277: if inc.is_a?(Hash) 278: inc.each do |k, v| 279: v = v.empty? ? [] : [v] 280: h[k.to_s] = case objs = send(k) 281: when Array 282: objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))} 283: else 284: Literal.new(Sequel.object_to_json(objs, *v)) 285: end 286: end 287: else 288: Array(inc).each{|c| h[c.to_s] = send(c)} 289: end 290: end 291: 292: if root = opts[:root] 293: root = model.send(:underscore, model.to_s) unless root.is_a?(String) 294: h = {root => h} 295: end 296: 297: Sequel.object_to_json(h, *a) 298: end