Skip to content

Commit 1f920d8

Browse files
committed
#81 support indention in dynamic codegen, part 6: list
1 parent 100bff1 commit 1f920d8

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
9191
}
9292

9393
private static CodegenResult genList(String cacheKey, Class clazz, Type compType) {
94+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
9495
boolean isCollectionValueNullable = true;
9596
if (cacheKey.endsWith("__value_not_nullable")) {
9697
isCollectionValueNullable = false;
@@ -99,8 +100,13 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
99100
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
100101
ctx.append("java.util.List list = (java.util.List)obj;");
101102
ctx.append("int size = list.size();");
102-
ctx.append("if (size == 0) { return; }");
103-
ctx.buffer('[');
103+
if (noIndention) {
104+
ctx.append("if (size == 0) { return; }");
105+
ctx.buffer('[');
106+
} else {
107+
ctx.append("if (size == 0) { stream.write((byte)'[', (byte)']'); return; }");
108+
ctx.append("stream.writeArrayStart(); stream.writeIndention();");
109+
}
104110
ctx.append("java.lang.Object e = list.get(0);");
105111
if (isCollectionValueNullable) {
106112
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -110,7 +116,11 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
110116
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
111117
}
112118
ctx.append("for (int i = 1; i < size; i++) {");
113-
ctx.append("stream.write(',');");
119+
if (noIndention) {
120+
ctx.append("stream.write(',');");
121+
} else {
122+
ctx.append("stream.writeMore();");
123+
}
114124
ctx.append("e = list.get(i);");
115125
if (isCollectionValueNullable) {
116126
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -120,7 +130,11 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
120130
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
121131
}
122132
ctx.append("}"); // for
123-
ctx.buffer(']');
133+
if (noIndention) {
134+
ctx.buffer(']');
135+
} else {
136+
ctx.append("stream.writeArrayEnd();");
137+
}
124138
ctx.append("}"); // public static void encode_
125139
return ctx;
126140
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void encode(Object obj, JsonStream stream) throws IOException {
3232
return;
3333
}
3434
stream.writeArrayStart();
35+
stream.writeIndention();
3536
stream.writeVal(compTypeLiteral, list.get(0));
3637
for (int i = 1; i < list.size(); i++) {
3738
stream.writeMore();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.jsoniter.output;
2+
3+
import com.jsoniter.spi.Config;
4+
import junit.framework.TestCase;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
9+
public class TestList extends TestCase {
10+
11+
public void test_indention() {
12+
Config cfg = new Config.Builder()
13+
.encodingMode(EncodingMode.REFLECTION_MODE)
14+
.indentionStep(2)
15+
.build();
16+
assertEquals("[\n" +
17+
" 1,\n" +
18+
" 2\n" +
19+
"]", JsonStream.serialize(cfg, Arrays.asList(1, 2)));
20+
cfg = new Config.Builder()
21+
.encodingMode(EncodingMode.DYNAMIC_MODE)
22+
.indentionStep(2)
23+
.build();
24+
assertEquals("[\n" +
25+
" 1,\n" +
26+
" 2\n" +
27+
"]", JsonStream.serialize(cfg, Arrays.asList(1, 2)));
28+
}
29+
30+
public void test_indention_with_empty_array() {
31+
Config cfg = new Config.Builder()
32+
.encodingMode(EncodingMode.REFLECTION_MODE)
33+
.indentionStep(2)
34+
.build();
35+
assertEquals("[]", JsonStream.serialize(cfg, new ArrayList<Integer>()));
36+
cfg = new Config.Builder()
37+
.encodingMode(EncodingMode.DYNAMIC_MODE)
38+
.indentionStep(2)
39+
.build();
40+
assertEquals("[]", JsonStream.serialize(cfg, new ArrayList<Integer>()));
41+
}
42+
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
TestGson.class,
5050
com.jsoniter.output.TestGson.class,
5151
TestStreamBuffer.class,
52-
TestCollection.class})
52+
TestCollection.class,
53+
TestList.class})
5354
public abstract class AllTestCases {
5455
}

0 commit comments

Comments
 (0)