Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
908be477
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,发现更多精彩内容 >>
提交
908be477
编写于
7月 01, 2011
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1938 from joefiorini/master
Make to_json take a parameter to exclude root
上级
36d289ad
ac5e6911
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
69 addition
and
6 deletion
+69
-6
activemodel/lib/active_model/serializers/json.rb
activemodel/lib/active_model/serializers/json.rb
+17
-5
activemodel/test/cases/serializers/json_serialization_test.rb
...vemodel/test/cases/serializers/json_serialization_test.rb
+52
-1
未找到文件。
activemodel/lib/active_model/serializers/json.rb
浏览文件 @
908be477
...
@@ -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
...
...
activemodel/test/cases/serializers/json_serialization_test.rb
浏览文件 @
908be477
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录