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 83b89a3b13dea9..47e12ff919d660 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 = []