方法
包含的模块
实例公共方法
as_json(options = nil) 链接
返回一个表示模型的哈希。可以通过 options 传递一些配置。
include_root_in_json 选项控制 as_json 的顶层行为。如果设置为 true,as_json 将会输出一个以对象类型命名的单一根节点。include_root_in_json 选项的默认值为 false。
user = User.find(1) user.as_json # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:133.000Z", "awesome" => true} ActiveRecord::Base.include_root_in_json = true user.as_json # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
通过设置 :root 选项为 true 也可以实现此行为,例如:
user = User.find(1) user.as_json(root: true) # => { "user" => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
如果愿意,:root 也可以设置为一个自定义字符串键,例如:
user = User.find(1) user.as_json(root: "author") # => { "author" => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true } }
在没有任何 options 的情况下,返回的 Hash 将包含模型的所有属性。
user = User.find(1) user.as_json # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true}
:only 和 :except 选项可用于限制包含的属性,其工作方式类似于 attributes 方法。
user.as_json(only: [:id, :name]) # => { "id" => 1, "name" => "Konata Izumi" } user.as_json(except: [:id, :created_at, :age]) # => { "name" => "Konata Izumi", "awesome" => true }
要包含模型上某些方法调用的结果,请使用 :methods
user.as_json(methods: :permalink) # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true, # "permalink" => "1-konata-izumi" }
要包含关联,请使用 :include
user.as_json(include: :posts) # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true, # "posts" => [ { "id" => 1, "author_id" => 1, "title" => "Welcome to the weblog" }, # { "id" => 2, "author_id" => 1, "title" => "So I was thinking" } ] }
第二层及更高阶的关联也同样适用
user.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } }) # => { "id" => 1, "name" => "Konata Izumi", "age" => 16, # "created_at" => "2006-08-01T17:27:13.000Z", "awesome" => true, # "posts" => [ { "comments" => [ { "body" => "1st post!" }, { "body" => "Second!" } ], # "title" => "Welcome to the weblog" }, # { "comments" => [ { "body" => "Don't think too hard" } ], # "title" => "So I was thinking" } ] }
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/serializers/json.rb, line 96 def as_json(options = nil) root = if options && options.key?(:root) options[:root] else include_root_in_json end hash = serializable_hash(options).as_json if root root = model_name.element if root == true { root => hash } else hash end end
from_json(json, include_root = include_root_in_json) 链接
从 JSON 字符串设置模型的 attributes。返回 self。
class Person include ActiveModel::Serializers::JSON attr_accessor :name, :age, :awesome def attributes=(hash) hash.each do |key, value| send("#{key}=", value) end end def attributes instance_values end end json = { name: 'bob', age: 22, awesome:true }.to_json person = Person.new person.from_json(json) # => #<Person:0x007fec5e7a0088 @age=22, @awesome=true, @name="bob"> person.name # => "bob" person.age # => 22 person.awesome # => true
include_root 的默认值为 false。如果给定的 JSON 字符串包含一个单一的根节点,您可以将其更改为 true。
json = { person: { name: 'bob', age: 22, awesome:true } }.to_json person = Person.new person.from_json(json, true) # => #<Person:0x007fec5e7a0088 @age=22, @awesome=true, @name="bob"> person.name # => "bob" person.age # => 22 person.awesome # => true
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/serializers/json.rb, line 146 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