Skip to content

Commit 266fa97

Browse files
committed
Don't emit indent when the matching newline option is empty
Reported-By: Ryan Lopopolo <rjl@hyperbo.la>
1 parent 9396810 commit 266fa97

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎ext/json/ext/generator/generator.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
10261026

10271027
if (RB_UNLIKELY(data->state->object_nl)) {
10281028
fbuffer_append_str(buffer, data->state->object_nl);
1029-
}
1030-
if (RB_UNLIKELY(data->state->indent)) {
1031-
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1029+
if (data->state->indent) {
1030+
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
1031+
}
10321032
}
10331033

10341034
VALUE key_to_s;
@@ -1127,7 +1127,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
11271127
depth = --data->depth;
11281128
if (RB_UNLIKELY(data->state->object_nl)) {
11291129
fbuffer_append_str(buffer, data->state->object_nl);
1130-
if (RB_UNLIKELY(data->state->indent)) {
1130+
if (data->state->indent) {
11311131
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
11321132
}
11331133
}
@@ -1151,15 +1151,15 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
11511151
fbuffer_append_char(buffer, ',');
11521152
if (RB_UNLIKELY(data->state->array_nl)) fbuffer_append_str(buffer, data->state->array_nl);
11531153
}
1154-
if (RB_UNLIKELY(data->state->indent)) {
1154+
if (RB_UNLIKELY(data->state->array_nl && data->state->indent)) {
11551155
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
11561156
}
11571157
generate_json(buffer, data, RARRAY_AREF(obj, i));
11581158
}
11591159
data->depth = --depth;
11601160
if (RB_UNLIKELY(data->state->array_nl)) {
11611161
fbuffer_append_str(buffer, data->state->array_nl);
1162-
if (RB_UNLIKELY(data->state->indent)) {
1162+
if (data->state->indent) {
11631163
fbuffer_append_str_repeat(buffer, data->state->indent, depth);
11641164
}
11651165
}

‎java/src/json/ext/Generator.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,9 @@ static void generateArray(ThreadContext context, Session session, RubyArray<IRub
492492
buffer.write(arrayNLBytes, arrayNLBegin, arrayNLSize);
493493
}
494494
}
495-
Utils.repeatWrite(buffer, indentUnit, depth);
495+
if (!arrayNLEmpty) {
496+
Utils.repeatWrite(buffer, indentUnit, depth);
497+
}
496498
generateFor(context, session, element, buffer);
497499
}
498500

@@ -619,8 +621,8 @@ static void generateHash(ThreadContext context, Session session, RubyHash object
619621
int oldDepth = state.decreaseDepth();
620622
if (!firstPair && !objectNl.isEmpty()) {
621623
buffer.write(objectNLBytes);
624+
Utils.repeatWrite(buffer, state.getIndent(), oldDepth);
622625
}
623-
Utils.repeatWrite(buffer, state.getIndent(), oldDepth);
624626
buffer.write('}');
625627
}
626628

‎test/json/json_generator_test.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ def test_configure_keeps_as_json
643643
assert_equal as_json, state.as_json
644644
end
645645

646+
def test_indent_without_newlines
647+
assert_equal '[1,[2]]', generate([1, [2]], indent: '**')
648+
assert_equal '{"a":{"b":1}}', generate({ 'a' => { 'b' => 1 } }, indent: '**')
649+
assert_equal "[\n**1\n]", generate([1], indent: '**', array_nl: "\n")
650+
assert_equal "{\"a\":[\n****1\n**]}", generate({ 'a' => [1] }, indent: '**', array_nl: "\n")
651+
assert_equal "{\n**\"a\":[1]\n}", generate({ 'a' => [1] }, indent: '**', object_nl: "\n")
652+
end
653+
646654
def test_configure_writes_a_string_option_given_as_nil
647655
state = JSON.state.new(indent: '1', space: '2')
648656
state.configure(indent: nil)

0 commit comments

Comments
 (0)