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 197
197:         def from_json(json, opts={})
198:           if opts[:all_associations] || opts[:all_columns]
199:             Sequel::Deprecation.deprecate("The from_json :all_associations and :all_columns", 'You need to explicitly specify the associations and columns via the :associations and :fields options')
200:           end
201:           from_json_node(Sequel.parse_json(json), opts)
202:         end

Using the provided hash, update the instance with data contained in the hash. By default, just calls set with the hash values.

Options:

:all_associations :Indicates that all associations supported by the model should be tried. This option also cascades to associations if used. It is better to use the :associations option instead of this option. This option only exists for backwards compatibility.
:all_columns :Overrides the setting logic allowing all setter methods be used, even if access to the setter method is restricted. This option cascades to associations if used, and can be reset in those associations using the :all_columns=>false or :fields options. This option is considered a security risk, and only exists for backwards compatibility. It is better to use the :fields option appropriately instead of this option, or no option at all.
: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 223
223:         def from_json_node(hash, opts={})
224:           unless hash.is_a?(Hash)
225:             raise Error, "parsed json doesn't return a hash"
226:           end
227:           if hash.has_key?(JSON.create_id)
228:             Sequel::Deprecation.deprecate('Attempting to use Model#from_json with a hash value that includes JSON.create_id is deprecated.  Starting in Sequel 4.0, the create_id will not be removed automatically.')
229:             hash.delete(JSON.create_id)
230:           end
231: 
232:           unless assocs = opts[:associations]
233:             if opts[:all_associations]
234:               assocs = {}
235:               model.associations.each{|v| assocs[v] = {:all_associations=>true}}
236:             end
237:           end
238: 
239:           if assocs
240:             assocs = case assocs
241:             when Symbol
242:               {assocs=>{}}
243:             when Array
244:               assocs_tmp = {}
245:               assocs.each{|v| assocs_tmp[v] = {}}
246:               assocs_tmp
247:             when Hash
248:               assocs
249:             else
250:               raise Error, ":associations should be Symbol, Array, or Hash if present"
251:             end
252: 
253:             if opts[:all_columns]
254:               assocs.each_value do |assoc_opts|
255:                 assoc_opts[:all_columns] = true unless assoc_opts.has_key?(:fields) || assoc_opts.has_key?(:all_columns)
256:               end
257:             end
258: 
259:             assocs.each do |assoc, assoc_opts|
260:               if assoc_values = hash.delete(assoc.to_s)
261:                 unless r = model.association_reflection(assoc)
262:                   raise Error, "Association #{assoc} is not defined for #{model}"
263:                 end
264: 
265:                 associations[assoc] = if r.returns_array?
266:                   raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
267:                   assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
268:                 else
269:                   raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
270:                   assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
271:                 end
272:               end
273:             end
274:           end
275: 
276:           if fields = opts[:fields]
277:             set_fields(hash, fields, opts)
278:           elsif opts[:all_columns]
279:             meths = methods.collect{|x| x.to_s}.grep(Model::SETTER_METHOD_REGEXP) - Model::RESTRICTED_SETTER_METHODS
280:             hash.each do |k, v|
281:               if meths.include?(setter_meth = "#{k}=")
282:                 send(setter_meth, v)
283:               else
284:                 raise Error, "Entry in JSON does not have a matching setter method: #{k}"
285:               end
286:             end
287:           else
288:             set(hash)
289:           end
290: 
291:           self
292:         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. Implies :naked since the object name is explicit.

[Source]

     # File lib/sequel/plugins/json_serializer.rb, line 310
310:         def to_json(*a)
311:           if opts = a.first.is_a?(Hash)
312:             opts = model.json_serializer_opts.merge(a.first)
313:             a = []
314:           else
315:             opts = model.json_serializer_opts
316:           end
317:           vals = values
318:           cols = if only = opts[:only]
319:             Array(only)
320:           else
321:             vals.keys - Array(opts[:except])
322:           end
323: 
324:           h = {}
325:           if  JSON.create_id && !opts[:naked] && !opts[:root]
326:             Sequel::Deprecation.deprecate('The :naked and :root options have not been used, so adding JSON.create_id to the to_json output.  This is deprecated, starting in Sequel 4, the JSON.create_id will never be added.')
327:             h[JSON.create_id] = model.name
328:           end
329: 
330:           cols.each{|c| h[c.to_s] = send(c)}
331:           if inc = opts[:include]
332:             if inc.is_a?(Hash)
333:               inc.each do |k, v|
334:                 v = v.empty? ? [] : [v]
335:                 h[k.to_s] = case objs = send(k)
336:                 when 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{|c| h[c.to_s] = send(c)}
344:             end
345:           end
346:           h = {model.send(:underscore, model.to_s) => h} if opts[:root]
347:           Sequel.object_to_json(h, *a)
348:         end

[Validate]