Skip to content

Commit 8ea29ce

Browse files
committed
support indention
1 parent c6bea75 commit 8ea29ce

7 files changed

Lines changed: 65 additions & 15 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ private static String genSource(String cacheKey, Class clazz, Type[] typeArgs) {
8282
if (Collection.class.isAssignableFrom(clazz)) {
8383
return CodegenImplArray.genCollection(clazz, typeArgs);
8484
}
85-
return CodegenImplObject.genObject(cacheKey, clazz);
85+
return CodegenImplObject.genObject(clazz);
8686
}
8787
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ public static String genArray(Class clazz) {
1616
append(lines, "{{comp}}[] arr = ({{comp}}[])obj;");
1717
append(lines, "if (arr.length == 0) { stream.writeEmptyArray(); return; }");
1818
append(lines, "stream.startArray();");
19-
append(lines, "for (int i = 0; i < arr.length; i++) {");
19+
append(lines, "int i = 0;");
2020
append(lines, "{{op}}");
21+
append(lines, "while (i < arr.length) {");
2122
append(lines, "stream.writeMore();");
23+
append(lines, "{{op}}");
2224
append(lines, "}");
2325
append(lines, "stream.endArray();");
2426
append(lines, "}");
2527
return lines.toString()
2628
.replace("{{comp}}", compType.getCanonicalName())
27-
.replace("{{op}}", CodegenImplNative.genWriteOp("arr[i]", compType));
29+
.replace("{{op}}", CodegenImplNative.genWriteOp("arr[i++]", compType));
2830
}
2931

3032
private static void append(StringBuilder lines, String str) {
@@ -59,10 +61,9 @@ private static String genCollection(Class clazz, Type compType) {
5961
append(lines, "if (!iter.hasNext()) { stream.writeEmptyArray(); return; }");
6062
append(lines, "stream.startArray();");
6163
append(lines, "{{op}}");
62-
append(lines, "stream.writeMore();");
6364
append(lines, "while (iter.hasNext()) {");
64-
append(lines, "{{op}}");
6565
append(lines, "stream.writeMore();");
66+
append(lines, "{{op}}");
6667
append(lines, "}");
6768
append(lines, "stream.endArray();");
6869
append(lines, "}");

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ public static String genMap(Class clazz, Type[] typeArgs) {
3434
append(lines, "stream.startObject();");
3535
append(lines, "stream.writeField((String)entry.getKey());");
3636
append(lines, "{{op}}");
37-
append(lines, "stream.writeMore();");
3837
append(lines, "while(iter.hasNext()) {");
3938
append(lines, "entry = iter.next();");
39+
append(lines, "stream.writeMore();");
4040
append(lines, "stream.writeField((String)entry.getKey());");
4141
append(lines, "{{op}}");
42-
append(lines, "stream.writeMore();");
4342
append(lines, "}");
4443
append(lines, "stream.endObject();");
4544
append(lines, "}");

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ public static String genWriteOp(String code, Type valueType) {
129129

130130
String cacheKey = TypeLiteral.generateEncoderCacheKey(valueType);
131131
Codegen.getEncoder(cacheKey, valueType);
132-
// Encoder encoder = Codegen.cache.get(cacheKey);
133-
return String.format("%s.encode_(%s, stream);", cacheKey, code);
132+
return String.format("%s.encode_((%s)%s, stream);", cacheKey, getTypeName(valueType), code);
134133
}
135134

136135
public static String getTypeName(Type fieldType) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
import com.jsoniter.spi.JsoniterSpi;
77

88
class CodegenImplObject {
9-
public static String genObject(String cacheKey, Class clazz) {
9+
public static String genObject(Class clazz) {
1010
ClassDescriptor desc = JsoniterSpi.getClassDescriptor(clazz, false);
1111
StringBuilder lines = new StringBuilder();
1212
append(lines, String.format("public static void encode_(%s obj, com.jsoniter.output.JsonStream stream) {", clazz.getCanonicalName()));
1313
append(lines, "if (obj == null) { stream.writeNull(); return; }");
1414
if (hasFieldOutput(desc)) {
15+
boolean notFirst = false;
1516
append(lines, "stream.startObject();");
1617
for (Binding field : desc.allEncoderBindings()) {
1718
for (String toName : field.toNames) {
19+
if (notFirst) {
20+
append(lines, "stream.writeMore();");
21+
} else {
22+
notFirst = true;
23+
}
1824
append(lines, String.format("stream.writeField(\"%s\");", toName));
1925
append(lines, genField(field));
20-
append(lines, "stream.writeMore();");
2126
}
2227
}
2328
append(lines, "stream.endObject();");

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
public class JsonStream extends OutputStream {
1010

11+
public int indentionStep = 0;
12+
public int indention = 0;
1113
private OutputStream out;
1214
private static final byte[] NULL = "null".getBytes();
1315
private static final byte[] TRUE = "true".getBytes();
@@ -183,20 +185,49 @@ public final void writeEmptyArray() throws IOException {
183185
}
184186

185187
public final void startArray() throws IOException {
188+
indention += indentionStep;
186189
write('[');
190+
writeIndention();
187191
}
188192

189193
public final void writeMore() throws IOException {
190194
write(',');
195+
writeIndention();
196+
}
197+
198+
private void writeIndention() throws IOException {
199+
writeIndention(0);
200+
}
201+
202+
private void writeIndention(int delta) throws IOException {
203+
if (indention == 0) {
204+
return;
205+
}
206+
write('\n');
207+
int toWrite = indention - delta;
208+
int i = 0;
209+
for (; ; ) {
210+
for (; i < toWrite && count < buf.length; i++) {
211+
buf[count++] = ' ';
212+
}
213+
if (i == toWrite) {
214+
break;
215+
} else {
216+
flushBuffer();
217+
}
218+
}
191219
}
192220

193221
public final void endArray() throws IOException {
194-
count--; // remove the last ,
222+
writeIndention(indentionStep);
223+
indention -= indentionStep;
195224
write(']');
196225
}
197226

198227
public final void startObject() throws IOException {
228+
indention += indentionStep;
199229
write('{');
230+
writeIndention();
200231
}
201232

202233
public final void writeField(String field) throws IOException {
@@ -205,7 +236,8 @@ public final void writeField(String field) throws IOException {
205236
}
206237

207238
public final void endObject() throws IOException {
208-
count--; // remove the last ,
239+
writeIndention(indentionStep);
240+
indention -= indentionStep;
209241
write('}');
210242
}
211243

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ public static class TestObject2 {
5151
}
5252

5353
public void test_object_of_array() throws IOException {
54+
stream.indentionStep = 2;
5455
TestObject2 obj = new TestObject2();
5556
obj.objs = new TestObject1[1];
5657
obj.objs[0] = new TestObject1();
5758
obj.objs[0].field1 = "1";
5859
obj.objs[0].field2 = "2";
5960
stream.writeVal(obj);
6061
stream.close();
61-
assertEquals("{'objs':[{'field1':'1','field2':'2'}]}".replace('\'', '"'), baos.toString());
62+
assertEquals("{\n" +
63+
" \"objs\":[\n" +
64+
" {\n" +
65+
" \"field1\":\"1\",\n" +
66+
" \"field2\":\"2\"\n" +
67+
" }\n" +
68+
" ]\n" +
69+
"}".replace('\'', '"'), baos.toString());
6270
}
6371

6472
public void test_map_of_objects() throws IOException {
73+
stream.indentionStep = 2;
6574
final TestObject1 obj1 = new TestObject1();
6675
obj1.field1 = "1";
6776
obj1.field2 = "2";
@@ -70,6 +79,11 @@ public void test_map_of_objects() throws IOException {
7079
put("hello", obj1);
7180
}});
7281
stream.close();
73-
assertEquals("{'hello':{'field1':'1','field2':'2'}}".replace('\'', '"'), baos.toString());
82+
assertEquals("{\n" +
83+
" \"hello\":{\n" +
84+
" \"field1\":\"1\",\n" +
85+
" \"field2\":\"2\"\n" +
86+
" }\n" +
87+
"}".replace('\'', '"'), baos.toString());
7488
}
7589
}

0 commit comments

Comments
 (0)