diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index c35e86d9b..fbfa2c724 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -681,6 +681,13 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S if (max_nesting != 0 && depth > max_nesting) { rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth); } + + if (RHASH_SIZE(obj) == 0) { + fbuffer_append(buffer, "{}", 2); + --state->depth; + return; + } + fbuffer_append_char(buffer, '{'); arg.buffer = buffer; @@ -709,6 +716,13 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St if (max_nesting != 0 && depth > max_nesting) { rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth); } + + if (RARRAY_LEN(obj) == 0) { + fbuffer_append(buffer, "[]", 2); + --state->depth; + return; + } + fbuffer_append_char(buffer, '['); if (RB_UNLIKELY(state->array_nl)) fbuffer_append(buffer, state->array_nl, state->array_nl_len); for(i = 0; i < RARRAY_LEN(obj); i++) { diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java index 5a296c8f9..d0ac44586 100644 --- a/java/src/json/ext/Generator.java +++ b/java/src/json/ext/Generator.java @@ -269,6 +269,12 @@ void generate(Session session, RubyArray object, ByteList buffer) { GeneratorState state = session.getState(); int depth = state.increaseDepth(); + if (object.isEmpty()) { + buffer.append("[]".getBytes()); + state.decreaseDepth(); + return; + } + ByteList indentUnit = state.getIndent(); byte[] shift = Utils.repeat(indentUnit, depth); @@ -327,6 +333,12 @@ void generate(final Session session, RubyHash object, final GeneratorState state = session.getState(); final int depth = state.increaseDepth(); + if (object.isEmpty()) { + buffer.append("{}".getBytes()); + state.decreaseDepth(); + return; + } + final ByteList objectNl = state.getObjectNl(); final byte[] indent = Utils.repeat(state.getIndent(), depth); final ByteList spaceBefore = state.getSpaceBefore(); diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index fe481685e..8410013ff 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -399,9 +399,15 @@ def json_shift(state) end def json_transform(state) + depth = state.depth += 1 + + if empty? + state.depth -= 1 + return '{}' + end + delim = ",#{state.object_nl}" result = +"{#{state.object_nl}" - depth = state.depth += 1 first = true indent = !state.object_nl.empty? each { |key, value| @@ -441,6 +447,13 @@ def to_json(state = nil, *) private def json_transform(state) + depth = state.depth += 1 + + if empty? + state.depth -= 1 + return '[]' + end + result = '['.dup if state.array_nl.empty? delim = "," @@ -448,7 +461,7 @@ def json_transform(state) result << state.array_nl delim = ",#{state.array_nl}" end - depth = state.depth += 1 + first = true indent = !state.array_nl.empty? each { |value| diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 7dc45e3a5..32712c834 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -90,10 +90,17 @@ def test_dump_strict def test_generate_pretty json = pretty_generate({}) + assert_equal('{}', json) + + json = pretty_generate({1=>{}, 2=>[], 3=>4}) assert_equal(<<'EOT'.chomp, json) { + "1": {}, + "2": [], + "3": 4 } EOT + json = pretty_generate(@hash) # hashes aren't (insertion) ordered on every ruby implementation # assert_equal(@json3, json)