module ActiveModel::Serializers::JSON
def as_json(options = nil)
# { "comments" => [ { "body" => "Don't think too hard" } ],
# "title" => "Welcome to the weblog" },
# "posts" => [ { "comments" => [ { "body" => "1st post!" }, { "body" => "Second!" } ],
# "created_at" => "2006/08/01", "awesome" => true,
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
only: :title } })
only: :body } },
include: { comments: {
user.as_json(include: { posts: {
Second level and higher order associations work as well:
# { "id" => 2, "author_id" => 1, "title" => "So I was thinking" } ] }
# "posts" => [ { "id" => 1, "author_id" => 1, "title" => "Welcome to the weblog" },
# "created_at" => "2006/08/01", "awesome" => true,
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json(include: :posts)
To include associations use :include:
# "permalink" => "1-konata-izumi" }
# "created_at" => "2006/08/01", "awesome" => true,
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json(methods: :permalink)
To include the result of some method calls on the model use :methods:
# => { "name" => "Konata Izumi", "awesome" => true }
user.as_json(except: [:id, :created_at, :age])
# => { "id" => 1, "name" => "Konata Izumi" }
user.as_json(only: [:id, :name])
the attributes included, and work similar to the +attributes+ method.
The :only and :except options can be used to limit
# "created_at" => "2006/08/01", "awesome" => true}
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json
user = User.find(1)
attributes.
Without any +options+, the returned Hash will include all the model's
# "created_at" => "2006/08/01", "awesome" => true } }
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json(root: true)
user = User.find(1)
to +true+ as in:
This behavior can also be achieved by setting the :root option
# "created_at" => "2006/08/01", "awesome" => true } }
# => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json
ActiveRecord::Base.include_root_in_json = true
# "created_at" => "2006/08/01", "awesome" => true}
# => { "id" => 1, "name" => "Konata Izumi", "age" => 16,
user.as_json
user = User.find(1)
option is +false+.
after the object's type. The default value for include_root_in_json
of +as_json+. If +true+, +as_json+ will emit a single root node named
The option include_root_in_json controls the top-level behavior
passed through +options+.
Returns a hash representing the model. Some configuration can be
def as_json(options = nil) root = if options && options.key?(:root) options[:root] else include_root_in_json end if root root = model_name.element if root == true { root => serializable_hash(options) } else serializable_hash(options) end end
def from_json(json, include_root = include_root_in_json)
person.age # => 22
person.name # => "bob"
person.from_json(json, true) # => #
person = Person.new
json = { person: { name: 'bob', age: 22, awesome:true } }.to_json
+true+ if the given JSON string includes a single root node.
The default value for +include_root+ is +false+. You can change it to
person.awesome # => true
person.age # => 22
person.name # => "bob"
person.from_json(json) # => #
person = Person.new
json = { name: 'bob', age: 22, awesome:true }.to_json
end
end
instance_values
def attributes
end
end
send("#{key}=", value)
hash.each do |key, value|
def attributes=(hash)
attr_accessor :name, :age, :awesome
include ActiveModel::Serializers::JSON
class Person
Sets the model +attributes+ from a JSON string. Returns +self+.
def from_json(json, include_root = include_root_in_json) hash = ActiveSupport::JSON.decode(json) hash = hash.values.first if include_root self.attributes = hash self end