module Sequel::Plugins::JsonSerializer::InstanceMethods

  1. lib/sequel/plugins/json_serializer.rb
Parent: JsonSerializer

Methods

Public Instance

  1. from_json
  2. from_json_node
  3. to_json

Public Instance methods

from_json (json, opts=OPTS)

Parse the provided JSON, which should return a hash, and process the hash with from_json_node.

[show source]
# File lib/sequel/plugins/json_serializer.rb, line 178
def from_json(json, opts=OPTS)
  from_json_node(Sequel.parse_json(json), opts)
end
from_json_node (hash, opts=OPTS)

Using the provided hash, update the instance with data contained in the hash. By default, just calls set with the hash values.

Options:

:associations

Indicates that the associations cache should be updated by creating a new associated object using data from the hash. Should be a Symbol for a single association, an array of symbols for multiple associations, or a hash with symbol keys and dependent association option hash values.

:fields

Changes the behavior to call set_fields using the provided fields, instead of calling set.

[show source]
# File lib/sequel/plugins/json_serializer.rb, line 191
def from_json_node(hash, opts=OPTS)
  unless hash.is_a?(Hash)
    raise Error, "parsed json doesn't return a hash"
  end

  populate_associations = {}

  if assocs = opts[:associations]
    assocs = case assocs
    when Symbol
      {assocs=>{}}
    when Array
      assocs_tmp = {}
      assocs.each{|v| assocs_tmp[v] = {}}
      assocs_tmp
    when Hash
      assocs
    else
      raise Error, ":associations should be Symbol, Array, or Hash if present"
    end

    assocs.each do |assoc, assoc_opts|
      if assoc_values = hash.delete(assoc.to_s)
        unless r = model.association_reflection(assoc)
          raise Error, "Association #{assoc} is not defined for #{model}"
        end

        populate_associations[assoc] = if r.returns_array?
          raise Error, "Attempt to populate array association with a non-array" unless assoc_values.is_a?(Array)
          assoc_values.map{|v| v.is_a?(r.associated_class) ? v : r.associated_class.new.from_json_node(v, assoc_opts)}
        else
          raise Error, "Attempt to populate non-array association with an array" if assoc_values.is_a?(Array)
          assoc_values.is_a?(r.associated_class) ? assoc_values : r.associated_class.new.from_json_node(assoc_values, assoc_opts)
        end
      end
    end
  end

  if fields = opts[:fields]
    set_fields(hash, fields, opts)
  else
    set(hash)
  end

  populate_associations.each do |assoc, values|
    associations[assoc] = values
  end

  self
end
to_json (*a)

Return a string in JSON format. Accepts the following options:

:except

Symbol or Array of Symbols of columns not to include in the JSON output.

:include

Symbol, Array of Symbols, or a Hash with Symbol keys and Hash values specifying associations or other non-column attributes to include in the JSON output. Using a nested hash, you can pass options to associations to affect the JSON used for associated objects.

:only

Symbol or Array of Symbols of columns to only include in the JSON output, ignoring all other columns.

:root

Qualify the JSON with the name of the object.

[show source]
# File lib/sequel/plugins/json_serializer.rb, line 257
def to_json(*a)
  if opts = a.first.is_a?(Hash)
    opts = model.json_serializer_opts.merge(a.first)
    a = []
  else
    opts = model.json_serializer_opts
  end
  vals = values
  cols = if only = opts[:only]
    Array(only)
  else
    vals.keys - Array(opts[:except])
  end

  h = {}

  cols.each{|c| h[c.to_s] = send(c)}
  if inc = opts[:include]
    if inc.is_a?(Hash)
      inc.each do |k, v|
        v = v.empty? ? [] : [v]
        h[k.to_s] = case objs = send(k)
        when Array
          objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
        else
          Literal.new(Sequel.object_to_json(objs, *v))
        end
      end
    else
      Array(inc).each{|c| h[c.to_s] = send(c)}
    end
  end
  h = {model.send(:underscore, model.to_s) => h} if opts[:root]
  Sequel.object_to_json(h, *a)
end