Module: Sequel::Plugins::JsonSerializer::ClassMethods
- Defined in:
- lib/sequel/plugins/json_serializer.rb
Instance Attribute Summary collapse
-
#json_serializer_opts ⇒ Object
readonly
The default opts to use when serializing model objects to JSON.
Instance Method Summary collapse
-
#array_from_json(json, opts = {}) ⇒ Object
Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node.
-
#from_json(json, opts = {}) ⇒ Object
Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node.
-
#json_create(hash, opts = {}) ⇒ Object
Exists for compatibility with old json library which allows creation of arbitrary ruby objects by JSON.parse.
Instance Attribute Details
#json_serializer_opts ⇒ Object (readonly)
The default opts to use when serializing model objects to JSON.
141 142 143 |
# File 'lib/sequel/plugins/json_serializer.rb', line 141 def json_serializer_opts @json_serializer_opts end |
Instance Method Details
#array_from_json(json, opts = {}) ⇒ Object
Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node.
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sequel/plugins/json_serializer.rb', line 162 def array_from_json(json, opts={}) if opts[:all_associations] || opts[:all_columns] 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') end v = Sequel.parse_json(json) if v.is_a?(Array) raise(Error, 'parsed json returned an array containing non-hashes') unless v.all?{|ve| ve.is_a?(Hash) || ve.is_a?(self)} v.map{|ve| ve.is_a?(self) ? ve : new.from_json_node(ve, opts)} else raise(Error, 'parsed json did not return an array') end end |
#from_json(json, opts = {}) ⇒ Object
Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sequel/plugins/json_serializer.rb', line 145 def from_json(json, opts={}) if opts[:all_associations] || opts[:all_columns] 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') end v = Sequel.parse_json(json) case v when self v when Hash new.from_json_node(v, opts) else raise Error, "parsed json doesn't return a hash or instance of #{self}" end end |
#json_create(hash, opts = {}) ⇒ Object
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.
180 181 182 183 |
# File 'lib/sequel/plugins/json_serializer.rb', line 180 def json_create(hash, opts={}) Sequel::Deprecation.deprecate("Model.json_create", 'Switch to Model.from_json') new.from_json_node(hash, {:all_columns=>true, :all_associations=>true}.merge(opts)) end |