diff --git a/CHANGES.md b/CHANGES.md index 3ebbe190..6befccdc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,8 +2,9 @@ ### Unreleased -* Fixed the parser to no longer ignore invalid escapes in strings. +* Fix the parser to no longer ignore invalid escapes in strings. Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes. +* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`. ### 2025-11-07 (2.16.0) diff --git a/lib/json/truffle_ruby/generator.rb b/lib/json/truffle_ruby/generator.rb index 9ac46cc8..5d815262 100644 --- a/lib/json/truffle_ruby/generator.rb +++ b/lib/json/truffle_ruby/generator.rb @@ -651,6 +651,9 @@ def to_json(state = nil, *args) if casted_value.equal?(self) raise GeneratorError.new("#{self} not allowed in JSON", self) end + unless Generator.native_type?(casted_value) + raise GeneratorError.new("#{casted_value.class} returned by #{state.as_json} not allowed in JSON", casted_value) + end state.check_max_nesting state.depth += 1 diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index c01ed678..9600f4be 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -883,6 +883,15 @@ def test_json_generate_as_json_convert_to_proc assert_equal object.object_id.to_json, JSON.generate(object, strict: true, as_json: -> (o, is_key) { o.object_id }) end + def test_as_json_nan_does_not_call_to_json + def (obj = Object.new).to_json(*) + "null" + end + assert_raise(JSON::GeneratorError) do + JSON.generate(Float::NAN, strict: true, as_json: proc { obj }) + end + end + def assert_float_roundtrip(expected, actual) assert_equal(expected, JSON.generate(actual)) assert_equal(actual, JSON.parse(JSON.generate(actual)), "JSON: #{JSON.generate(actual)}")