Module Sequel::Plugins::JsonSerializer::InstanceMethods
In: lib/sequel/plugins/json_serializer.rb

Methods

Public Instance methods

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

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 186
186:         def from_json(json, opts=OPTS)
187:           from_json_node(Sequel.parse_json(json), opts)
188:         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.

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 199
199:         def from_json_node(hash, opts=OPTS)
200:           unless hash.is_a?(Hash)
201:             raise Error, "parsed json doesn't return a hash"
202:           end
203: 
204:           populate_associations = {}
205: 
206:           if assocs = opts[:associations]
207:             assocs = case assocs
208:             when Symbol
209:               {assocs=>{}}
210:             when Array
211:               assocs_tmp = {}
212:               assocs.each{|v| assocs_tmp[v] = {}}
213:               assocs_tmp
214:             when Hash
215:               assocs
216:             else
217:               raise Error, ":associations should be Symbol, Array, or Hash if present"
218:             end
219: 
220:             assocs.each do |assoc, assoc_opts|
221:               if assoc_values = hash.delete(assoc.to_s)
222:                 unless r = model.association_reflection(assoc)
223:                   raise Error, "Association #{assoc} is not defined for #{model}"
224:                 end
225: 
226:                 populate_associations[assoc] = if r.returns_array?
227:                   raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
228:                   assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
229:                 else
230:                   raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
231:                   assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
232:                 end
233:               end
234:             end
235:           end
236: 
237:           if fields = opts[:fields]
238:             set_fields(hash, fields, opts)
239:           else
240:             set(hash)
241:           end
242: 
243:           populate_associations.each do |assoc, values|
244:             associations[assoc] = values
245:           end
246: 
247:           self
248:         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":"..."}]'

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 260
260:         def json_serializer_opts(opts=OPTS)
261:           @json_serializer_opts = Hash[@json_serializer_opts||OPTS].merge!(opts)
262:         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.

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 281
281:         def to_json(*a)
282:           opts = model.json_serializer_opts
283:           opts = Hash[opts].merge!(@json_serializer_opts) if @json_serializer_opts
284:           if (arg_opts = a.first).is_a?(Hash)
285:             opts = Hash[opts].merge!(arg_opts)
286:             a = []
287:           end
288: 
289:           vals = values
290:           cols = if only = opts[:only]
291:             Array(only)
292:           else
293:             vals.keys - Array(opts[:except])
294:           end
295: 
296:           h = {}
297: 
298:           cols.each{|c| h[c.to_s] = get_column_value(c)}
299:           if inc = opts[:include]
300:             if inc.is_a?(Hash)
301:               inc.each do |k, v|
302:                 v = v.empty? ? [] : [v]
303: 
304:                 objs = send(k)
305: 
306:                 is_array = if r = model.association_reflection(k)
307:                   r.returns_array?
308:                 else
309:                   objs.is_a?(Array)
310:                 end
311:                 
312:                 h[k.to_s] = if is_array
313:                   objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
314:                 else
315:                   Literal.new(Sequel.object_to_json(objs, *v))
316:                 end
317:               end
318:             else
319:               Array(inc).each{|c| h[c.to_s] = send(c)}
320:             end
321:           end
322: 
323:           if root = opts[:root]
324:             unless root.is_a?(String)
325:               root = model.send(:underscore, model.send(:demodulize, model.to_s))
326:             end
327:             h = {root => h}
328:           end
329: 
330:           Sequel.object_to_json(h, *a)
331:         end

[Validate]