From 81f9b33e3adcecf3961549e6aea62e33c1528ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Mon, 24 Nov 2025 17:36:01 +0100 Subject: [PATCH 1/2] Test current behavior regarding depth for Coder Coder currently ignores its depth and always resets it to 0 when generating a new JSON document. --- test/json/json_coder_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/json/json_coder_test.rb b/test/json/json_coder_test.rb index 83b89a3b..1f9bc814 100755 --- a/test/json/json_coder_test.rb +++ b/test/json/json_coder_test.rb @@ -132,6 +132,11 @@ def test_json_coder_string_invalid_encoding assert_equal 2, calls end + def test_depth + coder = JSON::Coder.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ", depth: 1) + assert_equal %({\n "foo": 42\n}), coder.dump(foo: 42) + end + def test_nesting_recovery coder = JSON::Coder.new ary = [] From 0a4f3202883bc9487bb80c1454028f6ba474bc9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Mon, 24 Nov 2025 17:41:33 +0100 Subject: [PATCH 2/2] Respect Coder depth when generating --- ext/json/ext/generator/generator.c | 2 +- java/src/json/ext/GeneratorState.java | 6 ------ lib/json/truffle_ruby/generator.rb | 4 ---- test/json/json_coder_test.rb | 2 +- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 32a9b485..6ece9f05 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -1549,7 +1549,7 @@ static VALUE cState_generate_new(int argc, VALUE *argv, VALUE self) .buffer = &buffer, .vstate = Qfalse, .state = state, - .depth = 0, + .depth = state->depth, .obj = obj, .func = generate_json }; diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java index 19d6a134..b12ca2c3 100644 --- a/java/src/json/ext/GeneratorState.java +++ b/java/src/json/ext/GeneratorState.java @@ -259,14 +259,12 @@ public IRubyObject generate(ThreadContext context, IRubyObject obj) { @JRubyMethod public IRubyObject generate_new(ThreadContext context, IRubyObject obj, IRubyObject io) { GeneratorState newState = (GeneratorState)dup(); - newState.resetDepth(); return newState.generate(context, obj, io); } @JRubyMethod public IRubyObject generate_new(ThreadContext context, IRubyObject obj) { GeneratorState newState = (GeneratorState)dup(); - newState.resetDepth(); return newState.generate(context, obj, context.nil); } @@ -504,10 +502,6 @@ public IRubyObject depth_set(IRubyObject vDepth) { return vDepth; } - public void resetDepth() { - depth = 0; - } - private ByteList prepareByteList(ThreadContext context, IRubyObject value) { RubyString str = value.convertToString(); if (str.getEncoding() != UTF8Encoding.INSTANCE) { diff --git a/lib/json/truffle_ruby/generator.rb b/lib/json/truffle_ruby/generator.rb index 71a909dd..9ac46cc8 100644 --- a/lib/json/truffle_ruby/generator.rb +++ b/lib/json/truffle_ruby/generator.rb @@ -352,10 +352,6 @@ def generate_new(obj, anIO = nil) # :nodoc: dup.generate(obj, anIO) end - private def initialize_copy(_orig) - @depth = 0 - end - # Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above) private def generate_json(obj, buf) case obj diff --git a/test/json/json_coder_test.rb b/test/json/json_coder_test.rb index 1f9bc814..47e12ff9 100755 --- a/test/json/json_coder_test.rb +++ b/test/json/json_coder_test.rb @@ -134,7 +134,7 @@ def test_json_coder_string_invalid_encoding def test_depth coder = JSON::Coder.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ", depth: 1) - assert_equal %({\n "foo": 42\n}), coder.dump(foo: 42) + assert_equal %({\n "foo": 42\n }), coder.dump(foo: 42) end def test_nesting_recovery