Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bed324c6
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,发现更多精彩内容 >>
提交
bed324c6
编写于
11月 14, 2013
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12862 from chancancode/json_gem_compat
JSON gem compatibility
上级
07996ebc
0f33d70e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
80 addition
and
3 deletion
+80
-3
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+14
-0
activesupport/lib/active_support/core_ext/object/json.rb
activesupport/lib/active_support/core_ext/object/json.rb
+21
-3
activesupport/test/json/encoding_test.rb
activesupport/test/json/encoding_test.rb
+45
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
bed324c6
*
Improved compatibility with the stdlib JSON gem.
Previously, calling `::JSON.{generate,dump}` sometimes causes unexpected
failures such as intridea/multi_json#86.
`::JSON.{generate,dump}` now bypasses the ActiveSupport JSON encoder
completely and yields the same result with or without ActiveSupport. This
means that it will **not** call `as_json` and will ignore any options that
the JSON gem does not natively understand. To invoke ActiveSupport's JSON
encoder instead, use `obj.to_json(options)` or
`ActiveSupport::JSON.encode(obj, options)`.
*Godfrey Chan*
*
Fix Active Support
`Time#to_json`
and
`DateTime#to_json`
to return 3 decimal
places worth of fractional seconds, similar to
`TimeWithZone`
.
...
...
activesupport/lib/active_support/core_ext/object/json.rb
浏览文件 @
bed324c6
...
...
@@ -9,18 +9,36 @@
require
'active_support/core_ext/time/conversions'
require
'active_support/core_ext/date_time/conversions'
require
'active_support/core_ext/date/conversions'
require
'active_support/core_ext/module/aliasing'
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
# their default behavior. That said, we need to define the basic to_json method in all of them,
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
#
# On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
# JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
# passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
# calls to the original to_json method.
#
# It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
# should give exactly the same results with or without active support.
[
Object
,
Array
,
FalseClass
,
Float
,
Hash
,
Integer
,
NilClass
,
String
,
TrueClass
].
each
do
|
klass
|
klass
.
class_eval
do
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
def
to_json
(
options
=
nil
)
ActiveSupport
::
JSON
.
encode
(
self
,
options
)
def
to_json_with_active_support_encoder
(
options
=
nil
)
if
options
.
is_a?
(
::
JSON
::
State
)
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json
self
.
to_json_without_active_support_encoder
(
options
)
else
# to_json is being invoked directly, use ActiveSupport's encoder
ActiveSupport
::
JSON
.
encode
(
self
,
options
)
end
end
alias_method_chain
:to_json
,
:active_support_encoder
end
end
...
...
activesupport/test/json/encoding_test.rb
浏览文件 @
bed324c6
...
...
@@ -32,6 +32,19 @@ def as_json(options={})
end
end
class
HashWithAsJson
<
Hash
attr_accessor
:as_json_called
def
initialize
(
*
)
super
end
def
as_json
(
options
=
{})
@as_json_called
=
true
super
end
end
TrueTests
=
[[
true
,
%(true)
]]
FalseTests
=
[[
false
,
%(false)
]]
NilTests
=
[[
nil
,
%(null)
]]
...
...
@@ -367,6 +380,38 @@ def test_nil_true_and_false_represented_as_themselves
assert_equal
false
,
false
.
as_json
end
def
test_json_gem_dump_by_passing_active_support_encoder
h
=
HashWithAsJson
.
new
h
[
:foo
]
=
"hello"
h
[
:bar
]
=
"world"
assert_equal
%({"foo":"hello","bar":"world"})
,
JSON
.
dump
(
h
)
assert_nil
h
.
as_json_called
end
def
test_json_gem_generate_by_passing_active_support_encoder
h
=
HashWithAsJson
.
new
h
[
:foo
]
=
"hello"
h
[
:bar
]
=
"world"
assert_equal
%({"foo":"hello","bar":"world"})
,
JSON
.
generate
(
h
)
assert_nil
h
.
as_json_called
end
def
test_json_gem_pretty_generate_by_passing_active_support_encoder
h
=
HashWithAsJson
.
new
h
[
:foo
]
=
"hello"
h
[
:bar
]
=
"world"
assert_equal
<<
EXPECTED
.
chomp
,
JSON
.
pretty_generate
(
h
)
{
"foo": "hello",
"bar": "world"
}
EXPECTED
assert_nil
h
.
as_json_called
end
protected
def
object_keys
(
json_object
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录