| 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 162
162: def array_from_json(json, opts={})
163: if opts[:all_associations] || opts[:all_columns]
164: Sequel::Deprecation.deprecate("The from_json :all_associations and :all_columns", 'You need to explicitly specify the associations and columns via the :associations and :fields options')
165: end
166: v = Sequel.parse_json(json)
167: if v.is_a?(Array)
168: raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)}
169: v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)}
170: else
171: raise(Error, 'parsed json did not return an array')
172: end
173: 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 145
145: def from_json(json, opts={})
146: if opts[:all_associations] || opts[:all_columns]
147: Sequel::Deprecation.deprecate("The from_json :all_associations and :all_columns", 'You need to explicitly specify the associations and columns via the :associations and :fields options')
148: end
149: v = Sequel.parse_json(json)
150: case v
151: when self
152: v
153: when Hash
154: new.from_json_node(v, opts)
155: else
156: raise Error, "parsed json doesn't return a hash or instance of #{self}"
157: end
158: end
Exists for compatibility with old json library which allows creation of arbitrary ruby objects by JSON.parse. Creates a new instance and populates it using InstanceMethods#from_json_node with the :all_columns and :all_associations options. Not recommended for usage in new code, consider calling the from_json method directly with the JSON string.
# File lib/sequel/plugins/json_serializer.rb, line 180
180: def json_create(hash, opts={})
181: Sequel::Deprecation.deprecate("Model.json_create", 'Switch to Model.from_json')
182: new.from_json_node(hash, {:all_columns=>true, :all_associations=>true}.merge(opts))
183: end