提交 1e335ad1 编写于 作者: E Erich Menge

True, False, and Nil should be represented in as_json as themselves.

上级 669694fe
......@@ -159,18 +159,18 @@ def as_json(options = nil)
end
class TrueClass
AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
def as_json(options = nil) AS_JSON end #:nodoc:
def as_json(options = nil) self end #:nodoc:
def encode_json(encoder) to_s end #:nodoc:
end
class FalseClass
AS_JSON = ActiveSupport::JSON::Variable.new('false').freeze
def as_json(options = nil) AS_JSON end #:nodoc:
def as_json(options = nil) self end #:nodoc:
def encode_json(encoder) to_s end #:nodoc:
end
class NilClass
AS_JSON = ActiveSupport::JSON::Variable.new('null').freeze
def as_json(options = nil) AS_JSON end #:nodoc:
def as_json(options = nil) self end #:nodoc:
def encode_json(encoder) 'null' end #:nodoc:
end
class String
......@@ -189,8 +189,8 @@ def encode_json(encoder) to_s end #:nodoc:
class Float
# Encoding Infinity or NaN to JSON should return "null". The default returns
# "Infinity" or "NaN" what breaks parsing the JSON. E.g. JSON.parse('[NaN]').
def as_json(options = nil) finite? ? self : NilClass::AS_JSON end #:nodoc:
# "Infinity" or "NaN" breaks parsing the JSON. E.g. JSON.parse('[NaN]').
def as_json(options = nil) finite? ? self : nil end #:nodoc:
end
class BigDecimal
......@@ -208,7 +208,7 @@ def as_json(options = nil) #:nodoc:
if finite?
ActiveSupport.encode_big_decimal_as_string ? to_s : self
else
NilClass::AS_JSON
nil
end
end
end
......
......@@ -285,6 +285,12 @@ def test_opt_out_big_decimal_string_serialization
end
end
def test_nil_true_and_false_represented_as_themselves
assert_equal nil, nil.as_json
assert_equal true, true.as_json
assert_equal false, false.as_json
end
protected
def object_keys(json_object)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册