| Module | Sequel::Plugins::JsonSerializer::ClassMethods |
| In: |
lib/sequel/plugins/json_serializer.rb
|
| json_serializer_opts | [R] | The default opts to use when serializing model objects to JSON. |
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, line 185
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 json serializier opts when freezing model class
# File lib/sequel/plugins/json_serializer.rb, line 161
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
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, line 171
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