Skip to content

Commit 0b2329f

Browse files
committed
#81 support indention in dynamic codegen, part 4: array
1 parent 1f347c9 commit 0b2329f

5 files changed

Lines changed: 57 additions & 5 deletions

File tree

src/main/java/com/jsoniter/output/CodegenImplArray.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.jsoniter.output;
22

33
import com.jsoniter.spi.ClassInfo;
4+
import com.jsoniter.spi.JsoniterSpi;
45

56
import java.lang.reflect.Type;
67
import java.util.*;
78

89
class CodegenImplArray {
910

1011
public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
12+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
1113
Class clazz = classInfo.clazz;
1214
Class compType = clazz.getComponentType();
1315
if (compType.isArray()) {
@@ -23,8 +25,16 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
2325
CodegenResult ctx = new CodegenResult();
2426
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
2527
ctx.append(String.format("%s[] arr = (%s[])obj;", compType.getCanonicalName(), compType.getCanonicalName()));
26-
ctx.append("if (arr.length == 0) { return; }");
27-
ctx.buffer('[');
28+
if (noIndention) {
29+
ctx.append("if (arr.length == 0) { return; }");
30+
} else {
31+
ctx.append("if (arr.length == 0) { stream.write((byte)'[', (byte)']'); return; }");
32+
}
33+
if (noIndention) {
34+
ctx.buffer('[');
35+
} else {
36+
ctx.append("stream.writeArrayStart(); stream.writeIndention();");
37+
}
2838
ctx.append("int i = 0;");
2939
ctx.append(String.format("%s e = arr[i++];", compType.getCanonicalName()));
3040
if (isCollectionValueNullable) {
@@ -35,7 +45,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
3545
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
3646
}
3747
ctx.append("while (i < arr.length) {");
38-
ctx.append("stream.write(',');");
48+
if (noIndention) {
49+
ctx.append("stream.write(',');");
50+
} else {
51+
ctx.append("stream.writeMore();");
52+
}
3953
ctx.append("e = arr[i++];");
4054
if (isCollectionValueNullable) {
4155
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -45,7 +59,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
4559
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
4660
}
4761
ctx.append("}"); // while
48-
ctx.buffer(']');
62+
if (noIndention) {
63+
ctx.buffer(']');
64+
} else {
65+
ctx.append("stream.writeArrayEnd();");
66+
}
4967
ctx.append("}"); // public static void encode_
5068
return ctx;
5169
}

src/main/java/com/jsoniter/output/JsonStream.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ public final void writeEmptyArray() throws IOException {
286286
public final void writeArrayStart() throws IOException {
287287
indention += currentConfig().indentionStep();
288288
write('[');
289-
writeIndention();
290289
}
291290

292291
public final void writeMore() throws IOException {

src/main/java/com/jsoniter/output/ReflectionArrayEncoder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void encode(Object obj, JsonStream stream) throws IOException {
2828
return;
2929
}
3030
stream.writeArrayStart();
31+
stream.writeIndention();
3132
stream.writeVal(compTypeLiteral, Array.get(obj, 0));
3233
for (int i = 1; i < len; i++) {
3334
stream.writeMore();

src/test/java/com/jsoniter/output/TestArray.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jsoniter.output;
22

3+
import com.jsoniter.spi.Config;
34
import com.jsoniter.spi.TypeLiteral;
45
import junit.framework.TestCase;
56

@@ -116,4 +117,36 @@ public void test_arrays_as_list() throws IOException {
116117
public void test_default_empty_collection() throws IOException {
117118
assertEquals("[]", JsonStream.serialize(Collections.emptySet()));
118119
}
120+
121+
public void test_indention() {
122+
Config cfg = new Config.Builder()
123+
.encodingMode(EncodingMode.REFLECTION_MODE)
124+
.indentionStep(2)
125+
.build();
126+
assertEquals("[\n" +
127+
" 1,\n" +
128+
" 2\n" +
129+
"]", JsonStream.serialize(cfg, new int[]{1, 2}));
130+
cfg = new Config.Builder()
131+
.encodingMode(EncodingMode.DYNAMIC_MODE)
132+
.indentionStep(2)
133+
.build();
134+
assertEquals("[\n" +
135+
" 1,\n" +
136+
" 2\n" +
137+
"]", JsonStream.serialize(cfg, new int[]{1, 2}));
138+
}
139+
140+
public void test_indention_with_empty_array() {
141+
Config cfg = new Config.Builder()
142+
.encodingMode(EncodingMode.REFLECTION_MODE)
143+
.indentionStep(2)
144+
.build();
145+
assertEquals("[]", JsonStream.serialize(cfg, new int[]{}));
146+
cfg = new Config.Builder()
147+
.encodingMode(EncodingMode.DYNAMIC_MODE)
148+
.indentionStep(2)
149+
.build();
150+
assertEquals("[]", JsonStream.serialize(cfg, new int[]{}));
151+
}
119152
}

src/test/java/com/jsoniter/output/TestStreamBuffer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void test_write_indention() throws IOException {
4141
JsoniterSpi.setCurrentConfig(new Config.Builder().indentionStep(32).build());
4242
JsonStream jsonStream = new JsonStream(null, 32);
4343
jsonStream.writeArrayStart();
44+
jsonStream.writeIndention();
4445
assertEquals(34, jsonStream.buffer().len());
4546
} finally {
4647
JsoniterSpi.setCurrentConfig(oldConfig);

0 commit comments

Comments
 (0)