Skip to content

Commit 28c57df

Browse files
etiennebarriebyroot
authored andcommitted
Don't call to_json on the return value of as_json for Float::NAN
1 parent 9c36681 commit 28c57df

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
### Unreleased
44

5-
* Fixed the parser to no longer ignore invalid escapes in strings.
5+
* Fix the parser to no longer ignore invalid escapes in strings.
66
Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes.
7+
* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`.
78

89
### 2025-11-07 (2.16.0)
910

lib/json/truffle_ruby/generator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ def to_json(state = nil, *args)
651651
if casted_value.equal?(self)
652652
raise GeneratorError.new("#{self} not allowed in JSON", self)
653653
end
654+
unless Generator.native_type?(casted_value)
655+
raise GeneratorError.new("#{casted_value.class} returned by #{state.as_json} not allowed in JSON", casted_value)
656+
end
654657

655658
state.check_max_nesting
656659
state.depth += 1

test/json/json_generator_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,15 @@ def test_json_generate_as_json_convert_to_proc
883883
assert_equal object.object_id.to_json, JSON.generate(object, strict: true, as_json: -> (o, is_key) { o.object_id })
884884
end
885885

886+
def test_as_json_nan_does_not_call_to_json
887+
def (obj = Object.new).to_json(*)
888+
"null"
889+
end
890+
assert_raise(JSON::GeneratorError) do
891+
JSON.generate(Float::NAN, strict: true, as_json: proc { obj })
892+
end
893+
end
894+
886895
def assert_float_roundtrip(expected, actual)
887896
assert_equal(expected, JSON.generate(actual))
888897
assert_equal(actual, JSON.parse(JSON.generate(actual)), "JSON: #{JSON.generate(actual)}")

0 commit comments

Comments
 (0)