Skip to content

Commit b2c4480

Browse files
committed
pretty_generate: don't apply object_nl / array_nl for empty containers
Fix: #437 Before: ```json { "foo": { }, "bar": [ ] } ``` After: ```json { "foo": {}, "bar": [] } ```
1 parent 843195b commit b2c4480

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

ext/json/ext/generator/generator.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,13 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
681681
if (max_nesting != 0 && depth > max_nesting) {
682682
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
683683
}
684+
685+
if (RHASH_SIZE(obj) == 0) {
686+
fbuffer_append(buffer, "{}", 2);
687+
--state->depth;
688+
return;
689+
}
690+
684691
fbuffer_append_char(buffer, '{');
685692

686693
arg.buffer = buffer;
@@ -709,6 +716,13 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
709716
if (max_nesting != 0 && depth > max_nesting) {
710717
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
711718
}
719+
720+
if (RARRAY_LEN(obj) == 0) {
721+
fbuffer_append(buffer, "[]", 2);
722+
--state->depth;
723+
return;
724+
}
725+
712726
fbuffer_append_char(buffer, '[');
713727
if (RB_UNLIKELY(state->array_nl)) fbuffer_append(buffer, state->array_nl, state->array_nl_len);
714728
for(i = 0; i < RARRAY_LEN(obj); i++) {

java/src/json/ext/Generator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ void generate(Session session, RubyArray object, ByteList buffer) {
269269
GeneratorState state = session.getState();
270270
int depth = state.increaseDepth();
271271

272+
if (object.isEmpty()) {
273+
buffer.append("[]".getBytes());
274+
state.decreaseDepth();
275+
return;
276+
}
277+
272278
ByteList indentUnit = state.getIndent();
273279
byte[] shift = Utils.repeat(indentUnit, depth);
274280

@@ -327,6 +333,12 @@ void generate(final Session session, RubyHash object,
327333
final GeneratorState state = session.getState();
328334
final int depth = state.increaseDepth();
329335

336+
if (object.isEmpty()) {
337+
buffer.append("{}".getBytes());
338+
state.decreaseDepth();
339+
return;
340+
}
341+
330342
final ByteList objectNl = state.getObjectNl();
331343
final byte[] indent = Utils.repeat(state.getIndent(), depth);
332344
final ByteList spaceBefore = state.getSpaceBefore();

lib/json/pure/generator.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,15 @@ def json_shift(state)
399399
end
400400

401401
def json_transform(state)
402+
depth = state.depth += 1
403+
404+
if empty?
405+
state.depth -= 1
406+
return '{}'
407+
end
408+
402409
delim = ",#{state.object_nl}"
403410
result = +"{#{state.object_nl}"
404-
depth = state.depth += 1
405411
first = true
406412
indent = !state.object_nl.empty?
407413
each { |key, value|
@@ -441,14 +447,21 @@ def to_json(state = nil, *)
441447
private
442448

443449
def json_transform(state)
450+
depth = state.depth += 1
451+
452+
if empty?
453+
state.depth -= 1
454+
return '[]'
455+
end
456+
444457
result = '['.dup
445458
if state.array_nl.empty?
446459
delim = ","
447460
else
448461
result << state.array_nl
449462
delim = ",#{state.array_nl}"
450463
end
451-
depth = state.depth += 1
464+
452465
first = true
453466
indent = !state.array_nl.empty?
454467
each { |value|

test/json/json_generator_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ def test_dump_strict
9090

9191
def test_generate_pretty
9292
json = pretty_generate({})
93+
assert_equal('{}', json)
94+
95+
json = pretty_generate({1=>{}, 2=>[], 3=>4})
9396
assert_equal(<<'EOT'.chomp, json)
9497
{
98+
"1": {},
99+
"2": [],
100+
"3": 4
95101
}
96102
EOT
103+
97104
json = pretty_generate(@hash)
98105
# hashes aren't (insertion) ordered on every ruby implementation
99106
# assert_equal(@json3, json)

0 commit comments

Comments
 (0)