Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a04060fb
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a04060fb
编写于
6月 08, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Really make include_root_in_json default to true [#3770 state:resolved]
上级
d6953cbf
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
29 deletion
+35
-29
activemodel/lib/active_model/serializers/json.rb
activemodel/lib/active_model/serializers/json.rb
+6
-3
activemodel/test/cases/serializeration/json_serialization_test.rb
...del/test/cases/serializeration/json_serialization_test.rb
+21
-15
activerecord/test/cases/json_serialization_test.rb
activerecord/test/cases/json_serialization_test.rb
+8
-11
未找到文件。
activemodel/lib/active_model/serializers/json.rb
浏览文件 @
a04060fb
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
...
...
activemodel/test/cases/serializeration/json_serialization_test.rb
浏览文件 @
a04060fb
...
...
@@ -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
=
tru
e
Contact
.
include_root_in_json
=
fals
e
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
=
fals
e
Contact
.
include_root_in_json
=
tru
e
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
...
...
activerecord/test/cases/json_serialization_test.rb
浏览文件 @
a04060fb
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录