提交 ac5e6911 编写于 作者: J Joe Fiorini

Enable passing root: false to #to_json

上级 36d289ad
......@@ -32,8 +32,15 @@ module JSON
# # => {"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 false.
# This behavior can also be achieved by setting the <tt>:root</tt> option to +false+ as in:
#
# 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
# attributes. For example:
......@@ -83,7 +90,12 @@ module JSON
def as_json(options = nil)
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]
hash = { custom_root || self.class.model_name.element => hash }
end
......@@ -91,9 +103,9 @@ def as_json(options = nil)
hash
end
def from_json(json)
def from_json(json, include_root=include_root_in_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
end
......
......@@ -8,6 +8,12 @@ class Contact
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
def attributes=(hash)
hash.each do |k, v|
instance_variable_set("@#{k}", v)
end
end
def attributes
instance_values
end unless method_defined?(:attributes)
......@@ -34,7 +40,7 @@ def setup
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should not include root in json" do
test "should not include root in json (class method)" do
begin
Contact.include_root_in_json = false
json = @contact.to_json
......@@ -50,6 +56,13 @@ def setup
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
json = @contact.to_json(:root => 'json_contact')
......@@ -135,6 +148,44 @@ def @contact.favorite_quote; "Constraints are liberating"; 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
def @contact.as_json(options); { :name => name, :created_at => created_at }; end
json = @contact.to_json
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册