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
193 def array_from_json(json, opts=OPTS)
194   v = Sequel.parse_json(json)
195   if v.is_a?(Array)
196     raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)}
197     v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)}
198   else
199     raise(Error, 'parsed json did not return an array')
200   end
201 end
freeze()

Freeze json serializier opts when freezing model class

[show source]
    # File lib/sequel/plugins/json_serializer.rb
169 def freeze
170   @json_serializer_opts.freeze.each_value do |v|
171     v.freeze if v.is_a?(Array) || v.is_a?(Hash)
172   end
173 
174   super
175 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
179 def from_json(json, opts=OPTS)
180   v = Sequel.parse_json(json)
181   case v
182   when self
183     v
184   when Hash
185     new.from_json_node(v, opts)
186   else
187     raise Error, "parsed json doesn't return a hash or instance of #{self}"
188   end
189 end