From 78aa8d5b1d62a04d7cb5492f653266c99f4c55a2 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] [ruby/json] Test current behavior regarding depth for Coder Coder currently ignores its depth and always resets it to 0 when generating a new JSON document. https://github.com/ruby/json/commit/cca1f38316 --- 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 83b89a3b13dea9..1f9bc814e3f08f 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 2f192c73cccdfd81e4ac2206feadc48b2757c19c 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] [ruby/json] Respect Coder depth when generating https://github.com/ruby/json/commit/9c36681b17 --- ext/json/generator/generator.c | 2 +- test/json/json_coder_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c index 32a9b485139041..6ece9f05385491 100644 --- a/ext/json/generator/generator.c +++ b/ext/json/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/test/json/json_coder_test.rb b/test/json/json_coder_test.rb index 1f9bc814e3f08f..47e12ff919d660 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