Module: Sequel::Plugins::JsonSerializer::ClassMethods

Defined in:
lib/sequel/plugins/json_serializer.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) json_serializer_opts (readonly)

The default opts to use when serializing model objects to JSON.



138
139
140
# File 'lib/sequel/plugins/json_serializer.rb', line 138

def json_serializer_opts
  @json_serializer_opts
end

Instance Method Details

- (Object) array_from_json(json, opts = OPTS)

Attempt to parse an array of instances from the given JSON string, with options passed to InstanceMethods#from_json_node.



156
157
158
159
160
161
162
163
164
# File 'lib/sequel/plugins/json_serializer.rb', line 156

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

- (Object) from_json(json, opts = OPTS)

Attempt to parse a single instance from the given JSON string, with options passed to InstanceMethods#from_json_node.



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/sequel/plugins/json_serializer.rb', line 142

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