Class/Module Index [+]

Quicksearch

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={}) click to toggle source

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 159
def array_from_json(json, 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
from_json(json, opts={}) click to toggle source

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
def from_json(json, 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
inherited(subclass) click to toggle source

Copy the current model object's default json options into the subclass.

# File lib/sequel/plugins/json_serializer.rb, line 184
def inherited(subclass)
  super
  opts = {}
  json_serializer_opts.each{|k, v| opts[k] = (v.is_a?(Array) || v.is_a?(Hash)) ? v.dup : v}
  subclass.instance_variable_set(:@json_serializer_opts, opts)
end
json_create(hash, opts={}) click to toggle source

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 174
def json_create(hash, opts={})
  new.from_json_node(hash, {:all_columns=>true, :all_associations=>true}.merge(opts))
end
to_json(*a) click to toggle source

Call the dataset to_json method.

# File lib/sequel/plugins/json_serializer.rb, line 179
def to_json(*a)
  dataset.to_json(*a)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.