Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand Down