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
12 changes: 6 additions & 6 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)

if (RB_UNLIKELY(data->state->object_nl)) {
fbuffer_append_str(buffer, data->state->object_nl);
}
if (RB_UNLIKELY(data->state->indent)) {
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
if (data->state->indent) {
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
}
}

VALUE key_to_s;
Expand Down Expand Up @@ -1080,7 +1080,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
depth = --data->depth;
if (RB_UNLIKELY(data->state->object_nl)) {
fbuffer_append_str(buffer, data->state->object_nl);
if (RB_UNLIKELY(data->state->indent)) {
if (data->state->indent) {
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
}
}
Expand All @@ -1104,15 +1104,15 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
fbuffer_append_char(buffer, ',');
if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
}
if (RB_UNLIKELY(data->state->indent)) {
if (RB_UNLIKELY(data->state->array_nl && data->state->indent)) {
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
}
generate_json(buffer, data, RARRAY_AREF(obj, i));
}
data->depth = --depth;
if (RB_UNLIKELY(data->state->array_nl)) {
fbuffer_append_str(buffer, data->state->array_nl);
if (RB_UNLIKELY(data->state->indent)) {
if (data->state->indent) {
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
}
}
Expand Down
6 changes: 4 additions & 2 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ static void generateArray(ThreadContext context, Session session, RubyArray<IRub
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
}
}
Utils.repeatWrite(buffer, indentUnit, depth);
if (!arrayNLEmpty) {
Utils.repeatWrite(buffer, indentUnit, depth);
}
generateFor(context, session, element, buffer);
}

Expand Down Expand Up @@ -619,8 +621,8 @@ static void generateHash(ThreadContext context, Session session, RubyHash object
int oldDepth = state.decreaseDepth();
if (!firstPair && !objectNl.isEmpty()) {
buffer.write(objectNLBytes);
Utils.repeatWrite(buffer, state.getIndent(), oldDepth);
}
Utils.repeatWrite(buffer, state.getIndent(), oldDepth);
buffer.write('}');
}

Expand Down
8 changes: 8 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ def test_configure_keeps_as_json
assert_equal as_json, state.as_json
end

def test_indent_without_newlines
assert_equal '[1,[2]]', generate([1, [2]], indent: '**')
assert_equal '{"a":{"b":1}}', generate({ 'a' => { 'b' => 1 } }, indent: '**')
assert_equal "[\n**1\n]", generate([1], indent: '**', array_nl: "\n")
assert_equal "{\"a\":[\n****1\n**]}", generate({ 'a' => [1] }, indent: '**', array_nl: "\n")
assert_equal "{\n**\"a\":[1]\n}", generate({ 'a' => [1] }, indent: '**', object_nl: "\n")
end

def test_configure_writes_a_string_option_given_as_nil
state = JSON.state.new(indent: '1', space: '2')
state.configure(indent: nil)
Expand Down
Loading