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
181 def array_from_json(json, opts=OPTS)
182   v = Sequel.parse_json(json)
183   if v.is_a?(Array)
184     raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)}
185     v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)}
186   else
187     raise(Error, 'parsed json did not return an array')
188   end
189 end
freeze() click to toggle source

Freeze json serializier opts when freezing model class

Calls superclass method
    # File lib/sequel/plugins/json_serializer.rb
157 def freeze
158   @json_serializer_opts.freeze.each_value do |v|
159     v.freeze if v.is_a?(Array) || v.is_a?(Hash)
160   end
161 
162   super
163 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
167 def from_json(json, opts=OPTS)
168   v = Sequel.parse_json(json)
169   case v
170   when self
171     v
172   when Hash
173     new.from_json_node(v, opts)
174   else
175     raise Error, "parsed json doesn't return a hash or instance of #{self}"
176   end
177 end