module Sequel::Plugins::JsonSerializer::ClassMethods

Attributes

json_serializer_opts[R]

The default opts to use when serializing model objects to JSON.

Public Instance Methods

array_from_json(json, opts=OPTS) click to toggle source

Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node.

    # File lib/sequel/plugins/json_serializer.rb
185 def array_from_json(json, opts=OPTS)
186   v = Sequel.parse_json(json)
187   if v.is_a?(Array)
188     raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)}
189     v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)}
190   else
191     raise(Error, 'parsed json did not return an array')
192   end
193 end
freeze() click to toggle source

Freeze json serializier opts when freezing model class

Calls superclass method
    # File lib/sequel/plugins/json_serializer.rb
161 def freeze
162   @json_serializer_opts.freeze.each_value do |v|
163     v.freeze if v.is_a?(Array) || v.is_a?(Hash)
164   end
165 
166   super
167 end
from_json(json, opts=OPTS) click to toggle source

Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node.

    # File lib/sequel/plugins/json_serializer.rb
171 def from_json(json, opts=OPTS)
172   v = Sequel.parse_json(json)
173   case v
174   when self
175     v
176   when Hash
177     new.from_json_node(v, opts)
178   else
179     raise Error, "parsed json doesn't return a hash or instance of #{self}"
180   end
181 end