Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6e73cf6b
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,发现更多精彩内容 >>
提交
6e73cf6b
编写于
6月 08, 2009
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix AR json encoding
上级
99cf77be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
14 addition
and
31 deletion
+14
-31
activerecord/lib/active_record/serialization.rb
activerecord/lib/active_record/serialization.rb
+3
-2
activerecord/lib/active_record/serializers/json_serializer.rb
...verecord/lib/active_record/serializers/json_serializer.rb
+7
-25
activerecord/test/cases/json_serialization_test.rb
activerecord/test/cases/json_serialization_test.rb
+4
-4
未找到文件。
activerecord/lib/active_record/serialization.rb
浏览文件 @
6e73cf6b
...
...
@@ -3,8 +3,9 @@ module Serialization
class
Serializer
#:nodoc:
attr_reader
:options
def
initialize
(
record
,
options
=
{})
@record
,
@options
=
record
,
options
.
dup
def
initialize
(
record
,
options
=
nil
)
@record
=
record
@options
=
options
?
options
.
dup
:
{}
end
# To replicate the behavior in ActiveRecord#attributes,
...
...
activerecord/lib/active_record/serializers/json_serializer.rb
浏览文件 @
6e73cf6b
require
'active_support/json'
require
'active_support/core_ext/module/model_naming'
module
ActiveRecord
#:nodoc:
module
Serialization
...
...
@@ -74,36 +75,17 @@ module Serialization
# "title": "Welcome to the weblog"},
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
def
to_json
(
options
=
{})
json
=
JsonSerializer
.
new
(
self
,
options
).
to_s
if
include_root_in_json
"{
#{
self
.
class
.
json_class_name
}
:
#{
json
}
}"
else
json
end
def
encode_json
(
encoder
)
hash
=
Serializer
.
new
(
self
,
encoder
.
options
).
serializable_record
hash
=
{
self
.
class
.
model_name
.
element
=>
hash
}
if
include_root_in_json
ActiveSupport
::
JSON
.
encode
(
hash
)
end
def
as_json
(
options
=
nil
)
self
end
#:nodoc:
def
from_json
(
json
)
self
.
attributes
=
ActiveSupport
::
JSON
.
decode
(
json
)
self
end
private
# For compatibility with ActiveSupport::JSON.encode
def
rails_to_json
(
options
,
*
args
)
to_json
(
options
)
end
class
JsonSerializer
<
ActiveRecord
::
Serialization
::
Serializer
#:nodoc:
def
serialize
ActiveSupport
::
JSON
.
encode
(
serializable_record
)
end
end
module
ClassMethods
def
json_class_name
@json_class_name
||=
name
.
demodulize
.
underscore
.
inspect
end
end
end
end
activerecord/test/cases/json_serialization_test.rb
浏览文件 @
6e73cf6b
...
...
@@ -170,18 +170,18 @@ def @david.favorite_quote; "Constraints are liberating"; end
def
test_should_allow_only_option_for_list_of_authors
authors
=
[
@david
,
@mary
]
assert_equal
%([{"name":"David"},{"name":"Mary"}])
,
authors
.
to_json
(
:only
=>
:name
)
assert_equal
%([{"name":"David"},{"name":"Mary"}])
,
ActiveSupport
::
JSON
.
encode
(
authors
,
:only
=>
:name
)
end
def
test_should_allow_except_option_for_list_of_authors
authors
=
[
@david
,
@mary
]
assert_equal
%([{"id":1},{"id":2}])
,
authors
.
to_json
(
:except
=>
[
:name
,
:author_address_id
,
:author_address_extra_id
])
assert_equal
%([{"id":1},{"id":2}])
,
ActiveSupport
::
JSON
.
encode
(
authors
,
:except
=>
[
:name
,
:author_address_id
,
:author_address_extra_id
])
end
def
test_should_allow_includes_for_list_of_authors
authors
=
[
@david
,
@mary
]
json
=
authors
.
to_json
(
json
=
ActiveSupport
::
JSON
.
encode
(
authors
,
:only
=>
:name
,
:include
=>
{
:posts
=>
{
:only
=>
:id
}
...
...
@@ -200,6 +200,6 @@ def test_should_allow_options_for_hash_of_authors
2
=>
@mary
}
assert_equal
%({"1":{"name":"David"}})
,
authors_hash
.
to_json
(
:only
=>
[
1
,
:name
])
assert_equal
%({"1":{"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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录