module Sequel::Plugins::JsonSerializer::ClassMethods

Attributes

json_serializer_opts[R]

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

Public Instance Methods

array_from_json(json, opts=OPTS) click to toggle source

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

# File lib/sequel/plugins/json_serializer.rb, line 180
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() click to toggle source

Freeze json serializier opts when freezing model class

Calls superclass method
# File lib/sequel/plugins/json_serializer.rb, line 156
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) click to toggle source

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

# File lib/sequel/plugins/json_serializer.rb, line 166
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