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 195 def array_from_json(json, opts=OPTS) 196 v = Sequel.parse_json(json) 197 if v.is_a?(Array) 198 raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)} 199 v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)} 200 else 201 raise(Error, 'parsed json did not return an array') 202 end 203 end
freeze()
click to toggle source
Freeze json serializier opts when freezing model class
Calls superclass method
# File lib/sequel/plugins/json_serializer.rb 171 def freeze 172 @json_serializer_opts.freeze.each_value do |v| 173 v.freeze if v.is_a?(Array) || v.is_a?(Hash) 174 end 175 176 super 177 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 181 def from_json(json, opts=OPTS) 182 v = Sequel.parse_json(json) 183 case v 184 when self 185 v 186 when Hash 187 new.from_json_node(v, opts) 188 else 189 raise Error, "parsed json doesn't return a hash or instance of #{self}" 190 end 191 end