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

Really make include_root_in_json default to true [#3770 state:resolved]

上级 d6953cbf
require 'active_support/json'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/class/attribute'
module ActiveModel
module Serializers
......@@ -10,7 +10,8 @@ module JSON
included do
extend ActiveModel::Naming
cattr_accessor :include_root_in_json, :instance_writer => true
class_attribute :include_root_in_json
self.include_root_in_json = true
end
# Returns a JSON string representing the model. Some configuration is
......@@ -92,7 +93,9 @@ def as_json(options = nil)
end
def from_json(json)
self.attributes = ActiveSupport::JSON.decode(json)
hash = ActiveSupport::JSON.decode(json)
hash = hash.values.first if include_root_in_json
self.attributes = hash
self
end
end
......
......@@ -22,35 +22,41 @@ def setup
end
test "should include root in json" do
json = @contact.to_json
assert_match %r{^\{"contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should not include root in json" do
begin
Contact.include_root_in_json = true
Contact.include_root_in_json = false
json = @contact.to_json
assert_match %r{^\{"contact":\{}, json
assert_no_match %r{^\{"contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
ensure
Contact.include_root_in_json = false
Contact.include_root_in_json = true
end
end
test "should include custom root in json" do
begin
Contact.include_root_in_json = true
json = @contact.to_json(:root => 'json_contact')
json = @contact.to_json(:root => 'json_contact')
assert_match %r{^\{"json_contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
ensure
Contact.include_root_in_json = false
end
assert_match %r{^\{"json_contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should encode all encodable attributes" do
......
......@@ -8,7 +8,7 @@
class JsonSerializationTest < ActiveRecord::TestCase
class NamespacedContact < Contact
column :name, :string
column :name, :string
end
def setup
......@@ -23,16 +23,12 @@ def setup
end
def test_should_demodulize_root_in_json
NamespacedContact.include_root_in_json = true
@contact = NamespacedContact.new :name => 'whatever'
json = @contact.to_json
assert_match %r{^\{"namespaced_contact":\{}, json
ensure
NamespacedContact.include_root_in_json = false
end
def test_should_include_root_in_json
Contact.include_root_in_json = true
json = @contact.to_json
assert_match %r{^\{"contact":\{}, json
......@@ -41,8 +37,6 @@ def test_should_include_root_in_json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
ensure
Contact.include_root_in_json = false
end
def test_should_encode_all_encodable_attributes
......@@ -170,15 +164,19 @@ def @david.favorite_quote; "Constraints are liberating"; end
end
def test_should_allow_only_option_for_list_of_authors
ActiveRecord::Base.include_root_in_json = false
authors = [@david, @mary]
assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
ensure
ActiveRecord::Base.include_root_in_json = true
end
def test_should_allow_except_option_for_list_of_authors
ActiveRecord::Base.include_root_in_json = false
authors = [@david, @mary]
assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
ensure
ActiveRecord::Base.include_root_in_json = true
end
def test_should_allow_includes_for_list_of_authors
......@@ -201,7 +199,6 @@ def test_should_allow_options_for_hash_of_authors
1 => @david,
2 => @mary
}
assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册