module Sequel::Plugins::JsonSerializer::ClassMethods

  1. lib/sequel/plugins/json_serializer.rb

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)

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

[show source]
    # File lib/sequel/plugins/json_serializer.rb
199 def array_from_json(json, opts=OPTS)
200   v = Sequel.parse_json(json)
201   if v.is_a?(Array)
202     raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)}
203     v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)}
204   else
205     raise(Error, 'parsed json did not return an array')
206   end
207 end
freeze()

Freeze json serializier opts when freezing model class

[show source]
    # File lib/sequel/plugins/json_serializer.rb
175 def freeze
176   @json_serializer_opts.freeze.each_value do |v|
177     v.freeze if v.is_a?(Array) || v.is_a?(Hash)
178   end
179 
180   super
181 end
from_json(json, opts=OPTS)

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

[show source]
    # File lib/sequel/plugins/json_serializer.rb
185 def from_json(json, opts=OPTS)
186   v = Sequel.parse_json(json)
187   case v
188   when self
189     v
190   when Hash
191     new.from_json_node(v, opts)
192   else
193     raise Error, "parsed json doesn't return a hash or instance of #{self}"
194   end
195 end