提交 908be477 编写于 作者: J José Valim

Merge pull request #1938 from joefiorini/master

Make to_json take a parameter to exclude root
...@@ -32,8 +32,15 @@ module JSON ...@@ -32,8 +32,15 @@ module JSON
# # => {"id": 1, "name": "Konata Izumi", "age": 16, # # => {"id": 1, "name": "Konata Izumi", "age": 16,
# "created_at": "2006/08/01", "awesome": true} # "created_at": "2006/08/01", "awesome": true}
# #
# The remainder of the examples in this section assume +include_root_in_json+ # This behavior can also be achieved by setting the <tt>:root</tt> option to +false+ as in:
# is false. #
# user = User.find(1)
# user.as_json(root: false)
# # => {"id": 1, "name": "Konata Izumi", "age": 16,
# "created_at": "2006/08/01", "awesome": true}
#
# The remainder of the examples in this section assume include_root_in_json is set to
# <tt>false</tt>.
# #
# Without any +options+, the returned JSON string will include all the model's # Without any +options+, the returned JSON string will include all the model's
# attributes. For example: # attributes. For example:
...@@ -83,7 +90,12 @@ module JSON ...@@ -83,7 +90,12 @@ module JSON
def as_json(options = nil) def as_json(options = nil)
hash = serializable_hash(options) hash = serializable_hash(options)
if include_root_in_json include_root = include_root_in_json
if options.try(:key?, :root)
include_root = options[:root]
end
if include_root
custom_root = options && options[:root] custom_root = options && options[:root]
hash = { custom_root || self.class.model_name.element => hash } hash = { custom_root || self.class.model_name.element => hash }
end end
...@@ -91,9 +103,9 @@ def as_json(options = nil) ...@@ -91,9 +103,9 @@ def as_json(options = nil)
hash hash
end end
def from_json(json) def from_json(json, include_root=include_root_in_json)
hash = ActiveSupport::JSON.decode(json) hash = ActiveSupport::JSON.decode(json)
hash = hash.values.first if include_root_in_json hash = hash.values.first if include_root
self.attributes = hash self.attributes = hash
self self
end end
......
...@@ -8,6 +8,12 @@ class Contact ...@@ -8,6 +8,12 @@ class Contact
include ActiveModel::Serializers::JSON include ActiveModel::Serializers::JSON
include ActiveModel::Validations include ActiveModel::Validations
def attributes=(hash)
hash.each do |k, v|
instance_variable_set("@#{k}", v)
end
end
def attributes def attributes
instance_values instance_values
end unless method_defined?(:attributes) end unless method_defined?(:attributes)
...@@ -34,7 +40,7 @@ def setup ...@@ -34,7 +40,7 @@ def setup
assert_match %r{"preferences":\{"shows":"anime"\}}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json
end end
test "should not include root in json" do test "should not include root in json (class method)" do
begin begin
Contact.include_root_in_json = false Contact.include_root_in_json = false
json = @contact.to_json json = @contact.to_json
...@@ -50,6 +56,13 @@ def setup ...@@ -50,6 +56,13 @@ def setup
end end
end end
test "should not include root in json (option)" do
json = @contact.to_json(:root => false)
assert_no_match %r{^\{"contact":\{}, json
end
test "should include custom root in json" do test "should include custom root in json" do
json = @contact.to_json(:root => 'json_contact') json = @contact.to_json(:root => 'json_contact')
...@@ -135,6 +148,44 @@ def @contact.favorite_quote; "Constraints are liberating"; end ...@@ -135,6 +148,44 @@ def @contact.favorite_quote; "Constraints are liberating"; end
end end
end end
test "from_json should set the object's attributes" do
json = @contact.to_json
result = Contact.new.from_json(json)
assert_equal result.name, @contact.name
assert_equal result.age, @contact.age
assert_equal Time.parse(result.created_at), @contact.created_at
assert_equal result.awesome, @contact.awesome
assert_equal result.preferences, @contact.preferences
end
test "from_json should work without a root (method parameter)" do
json = @contact.to_json(:root => false)
result = Contact.new.from_json(json, false)
assert_equal result.name, @contact.name
assert_equal result.age, @contact.age
assert_equal Time.parse(result.created_at), @contact.created_at
assert_equal result.awesome, @contact.awesome
assert_equal result.preferences, @contact.preferences
end
test "from_json should work without a root (class attribute)" do
begin
Contact.include_root_in_json = false
json = @contact.to_json
result = Contact.new.from_json(json)
assert_equal result.name, @contact.name
assert_equal result.age, @contact.age
assert_equal Time.parse(result.created_at), @contact.created_at
assert_equal result.awesome, @contact.awesome
assert_equal result.preferences, @contact.preferences
ensure
Contact.include_root_in_json = true
end
end
test "custom as_json should be honored when generating json" do test "custom as_json should be honored when generating json" do
def @contact.as_json(options); { :name => name, :created_at => created_at }; end def @contact.as_json(options); { :name => name, :created_at => created_at }; end
json = @contact.to_json json = @contact.to_json
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册