Skip to content

Commit e0257b9

Browse files
etiennebarriebyroot
authored andcommitted
Reproduce C ext behavior of ignoring mutated depth in arrays
1 parent 386b36f commit e0257b9

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

java/src/json/ext/Generator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,10 @@ static void generateArray(ThreadContext context, Session session, RubyArray<IRub
462462
generateFor(context, session, element, buffer);
463463
}
464464

465-
int oldDepth = state.decreaseDepth();
465+
state.depth = --depth;
466466
if (!arrayNLEmpty) {
467467
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
468-
Utils.repeatWrite(buffer, indentUnit, oldDepth);
468+
Utils.repeatWrite(buffer, indentUnit, depth);
469469
}
470470

471471
buffer.write((byte) ']');

java/src/json/ext/GeneratorState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class GeneratorState extends RubyObject {
107107
/**
108108
* The current depth (inside a #to_json call)
109109
*/
110-
private int depth = 0;
110+
protected int depth = 0;
111111

112112
static final ObjectAllocator ALLOCATOR = GeneratorState::new;
113113

lib/json/truffle_ruby/generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def json_transform(state)
626626
end
627627
first = false
628628
}
629-
depth = state.depth -= 1
629+
state.depth = depth -= 1
630630
result << state.array_nl
631631
result << state.indent * depth if indent
632632
result << ']'

test/json/json_generator_test.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ def obj.to_json(state)
329329
"#{state.indent * state.depth}\"foo\":#{state.space}1#{state.object_nl}"\
330330
"#{state.indent * (state.depth - 1)}}"
331331
end
332-
indent = " " * 2 if RUBY_ENGINE != "ruby"
333332
assert_equal <<~JSON.chomp, JSON.pretty_generate([obj] * 2)
334333
[
335334
{
@@ -338,11 +337,14 @@ def obj.to_json(state)
338337
{
339338
"foo": 1
340339
}
341-
#{indent}]
340+
]
342341
JSON
343342
state = JSON::State.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ")
344343
state.generate(obj)
345-
assert_equal 1, state.depth
344+
assert_equal 1, state.depth # FIXME
345+
state.depth = 0
346+
state.generate([obj])
347+
assert_equal 0, state.depth
346348
end
347349

348350
def test_depth

0 commit comments

Comments
 (0)