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 = OPTS)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node. 
- 
  
    
      #freeze  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Freeze json serializier opts when freezing model class. 
- 
  
    
      #from_json(json, opts = OPTS)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node. 
Instance Attribute Details
#json_serializer_opts ⇒ Object (readonly)
The default opts to use when serializing model objects to JSON.
| 166 167 168 | # File 'lib/sequel/plugins/json_serializer.rb', line 166 def json_serializer_opts @json_serializer_opts end | 
Instance Method Details
#array_from_json(json, opts = OPTS) ⇒ Object
Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node.
| 193 194 195 196 197 198 199 200 201 | # File 'lib/sequel/plugins/json_serializer.rb', line 193 def array_from_json(json, opts=OPTS) 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 | 
#freeze ⇒ Object
Freeze json serializier opts when freezing model class
| 169 170 171 172 173 174 175 | # File 'lib/sequel/plugins/json_serializer.rb', line 169 def freeze @json_serializer_opts.freeze.each_value do |v| v.freeze if v.is_a?(Array) || v.is_a?(Hash) end super end | 
#from_json(json, opts = OPTS) ⇒ Object
Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node.
| 179 180 181 182 183 184 185 186 187 188 189 | # File 'lib/sequel/plugins/json_serializer.rb', line 179 def from_json(json, opts=OPTS) 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 |