1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #26933 from prathamesh-sonpatki/fix-26877

Fix an issue with JSON encoding of "Infinity" and "NaN" values
This commit is contained in:
Andrew White 2016-11-13 14:46:21 +00:00 committed by GitHub
commit c85d305a67
2 changed files with 23 additions and 1 deletions

View file

@ -85,7 +85,7 @@ module ActiveSupport
when String
EscapedString.new(value)
when Numeric, NilClass, TrueClass, FalseClass
value
value.as_json
when Hash
Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
when Array

View file

@ -432,6 +432,28 @@ EXPECTED
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
end
class InfiniteNumber
def as_json(options = nil)
{ "number" => 1.0 / 0 }
end
end
def test_to_json_works_when_as_json_returns_infinite_number
expected = { number: nil }.to_json
assert_equal expected, InfiniteNumber.new.to_json
end
class NaNNumber
def as_json(options = nil)
{ "number" => 0.0 / 0 }
end
end
def test_to_json_works_when_as_json_returns_NaN_number
expected = { number: nil }.to_json
assert_equal expected, NaNNumber.new.to_json
end
protected
def object_keys(json_object)