Fix JSON::Coder to call as_json proc for NaN and Infinity

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
This commit is contained in:
Étienne Barrié 2025-02-05 12:40:07 +01:00 committed by Hiroshi SHIBATA
parent dd1fe03b8a
commit f865148e19
No known key found for this signature in database
GPG key ID: F9CF13417264FAC2
2 changed files with 36 additions and 13 deletions

View file

@ -35,4 +35,19 @@ class JSONCoderTest < Test::Unit::TestCase
coder = JSON::Coder.new(symbolize_names: true)
assert_equal({a: 1}, coder.load('{"a":1}'))
end
def test_json_coder_dump_NaN_or_Infinity
coder = JSON::Coder.new(&:inspect)
assert_equal "NaN", coder.load(coder.dump(Float::NAN))
assert_equal "Infinity", coder.load(coder.dump(Float::INFINITY))
assert_equal "-Infinity", coder.load(coder.dump(-Float::INFINITY))
end
def test_json_coder_dump_NaN_or_Infinity_loop
coder = JSON::Coder.new(&:itself)
error = assert_raise JSON::GeneratorError do
coder.dump(Float::NAN)
end
assert_include error.message, "NaN not allowed in JSON"
end
end