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
2 changes: 1 addition & 1 deletion ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
6 changes: 0 additions & 6 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/json/json_coder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down