Skip to content

Commit 7a4e4b0

Browse files
committed
Merge remote-tracking branch 'origin/master' into pull/add-omit-zero
2 parents 85ae951 + 7cdbed6 commit 7a4e4b0

18 files changed

+357
-177
lines changed

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
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

11+
public static CodegenResult genCollection(String cacheKey, ClassInfo classInfo) {
12+
Type[] typeArgs = classInfo.typeArgs;
13+
Class clazz = classInfo.clazz;
14+
Type compType = Object.class;
15+
if (typeArgs.length == 0) {
16+
// default to List<Object>
17+
} else if (typeArgs.length == 1) {
18+
compType = typeArgs[0];
19+
} else {
20+
throw new IllegalArgumentException(
21+
"can not bind to generic collection without argument types, " +
22+
"try syntax like TypeLiteral<List<Integer>>{}");
23+
}
24+
if (clazz == List.class) {
25+
clazz = ArrayList.class;
26+
} else if (clazz == Set.class) {
27+
clazz = HashSet.class;
28+
}
29+
if (List.class.isAssignableFrom(clazz)) {
30+
return genList(cacheKey, clazz, compType);
31+
} else {
32+
return genCollection(cacheKey, clazz, compType);
33+
}
34+
}
35+
1036
public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
37+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
1138
Class clazz = classInfo.clazz;
1239
Class compType = clazz.getComponentType();
1340
if (compType.isArray()) {
@@ -23,8 +50,13 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
2350
CodegenResult ctx = new CodegenResult();
2451
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
2552
ctx.append(String.format("%s[] arr = (%s[])obj;", compType.getCanonicalName(), compType.getCanonicalName()));
26-
ctx.append("if (arr.length == 0) { return; }");
27-
ctx.buffer('[');
53+
if (noIndention) {
54+
ctx.append("if (arr.length == 0) { return; }");
55+
ctx.buffer('[');
56+
} else {
57+
ctx.append("if (arr.length == 0) { stream.write((byte)'[', (byte)']'); return; }");
58+
ctx.append("stream.writeArrayStart(); stream.writeIndention();");
59+
}
2860
ctx.append("int i = 0;");
2961
ctx.append(String.format("%s e = arr[i++];", compType.getCanonicalName()));
3062
if (isCollectionValueNullable) {
@@ -35,7 +67,11 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
3567
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
3668
}
3769
ctx.append("while (i < arr.length) {");
38-
ctx.append("stream.write(',');");
70+
if (noIndention) {
71+
ctx.append("stream.write(',');");
72+
} else {
73+
ctx.append("stream.writeMore();");
74+
}
3975
ctx.append("e = arr[i++];");
4076
if (isCollectionValueNullable) {
4177
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -45,37 +81,17 @@ public static CodegenResult genArray(String cacheKey, ClassInfo classInfo) {
4581
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
4682
}
4783
ctx.append("}"); // while
48-
ctx.buffer(']');
49-
ctx.append("}"); // public static void encode_
50-
return ctx;
51-
}
52-
53-
public static CodegenResult genCollection(String cacheKey, ClassInfo classInfo) {
54-
Type[] typeArgs = classInfo.typeArgs;
55-
Class clazz = classInfo.clazz;
56-
Type compType = Object.class;
57-
if (typeArgs.length == 0) {
58-
// default to List<Object>
59-
} else if (typeArgs.length == 1) {
60-
compType = typeArgs[0];
61-
} else {
62-
throw new IllegalArgumentException(
63-
"can not bind to generic collection without argument types, " +
64-
"try syntax like TypeLiteral<List<Integer>>{}");
65-
}
66-
if (clazz == List.class) {
67-
clazz = ArrayList.class;
68-
} else if (clazz == Set.class) {
69-
clazz = HashSet.class;
70-
}
71-
if (List.class.isAssignableFrom(clazz)) {
72-
return genList(cacheKey, clazz, compType);
84+
if (noIndention) {
85+
ctx.buffer(']');
7386
} else {
74-
return genCollection(cacheKey, clazz, compType);
87+
ctx.append("stream.writeArrayEnd();");
7588
}
89+
ctx.append("}"); // public static void encode_
90+
return ctx;
7691
}
7792

7893
private static CodegenResult genList(String cacheKey, Class clazz, Type compType) {
94+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
7995
boolean isCollectionValueNullable = true;
8096
if (cacheKey.endsWith("__value_not_nullable")) {
8197
isCollectionValueNullable = false;
@@ -84,8 +100,13 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
84100
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
85101
ctx.append("java.util.List list = (java.util.List)obj;");
86102
ctx.append("int size = list.size();");
87-
ctx.append("if (size == 0) { return; }");
88-
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+
}
89110
ctx.append("java.lang.Object e = list.get(0);");
90111
if (isCollectionValueNullable) {
91112
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -95,7 +116,11 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
95116
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
96117
}
97118
ctx.append("for (int i = 1; i < size; i++) {");
98-
ctx.append("stream.write(',');");
119+
if (noIndention) {
120+
ctx.append("stream.write(',');");
121+
} else {
122+
ctx.append("stream.writeMore();");
123+
}
99124
ctx.append("e = list.get(i);");
100125
if (isCollectionValueNullable) {
101126
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -105,21 +130,31 @@ private static CodegenResult genList(String cacheKey, Class clazz, Type compType
105130
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
106131
}
107132
ctx.append("}"); // for
108-
ctx.buffer(']');
133+
if (noIndention) {
134+
ctx.buffer(']');
135+
} else {
136+
ctx.append("stream.writeArrayEnd();");
137+
}
109138
ctx.append("}"); // public static void encode_
110139
return ctx;
111140
}
112141

113142
private static CodegenResult genCollection(String cacheKey, Class clazz, Type compType) {
143+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
114144
boolean isCollectionValueNullable = true;
115145
if (cacheKey.endsWith("__value_not_nullable")) {
116146
isCollectionValueNullable = false;
117147
}
118148
CodegenResult ctx = new CodegenResult();
119149
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
120150
ctx.append("java.util.Iterator iter = ((java.util.Collection)obj).iterator();");
121-
ctx.append("if (!iter.hasNext()) { return; }");
122-
ctx.buffer('[');
151+
if (noIndention) {
152+
ctx.append("if (!iter.hasNext()) { return; }");
153+
ctx.buffer('[');
154+
} else {
155+
ctx.append("if (!iter.hasNext()) { stream.write((byte)'[', (byte)']'); return; }");
156+
ctx.append("stream.writeArrayStart(); stream.writeIndention();");
157+
}
123158
ctx.append("java.lang.Object e = iter.next();");
124159
if (isCollectionValueNullable) {
125160
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -129,7 +164,11 @@ private static CodegenResult genCollection(String cacheKey, Class clazz, Type co
129164
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
130165
}
131166
ctx.append("while (iter.hasNext()) {");
132-
ctx.append("stream.write(',');");
167+
if (noIndention) {
168+
ctx.append("stream.write(',');");
169+
} else {
170+
ctx.append("stream.writeMore();");
171+
}
133172
ctx.append("e = iter.next();");
134173
if (isCollectionValueNullable) {
135174
ctx.append("if (e == null) { stream.writeNull(); } else {");
@@ -139,7 +178,11 @@ private static CodegenResult genCollection(String cacheKey, Class clazz, Type co
139178
CodegenImplNative.genWriteOp(ctx, "e", compType, false);
140179
}
141180
ctx.append("}"); // while
142-
ctx.buffer(']');
181+
if (noIndention) {
182+
ctx.buffer(']');
183+
} else {
184+
ctx.append("stream.writeArrayEnd();");
185+
}
143186
ctx.append("}"); // public static void encode_
144187
return ctx;
145188
}

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class CodegenImplMap {
99
public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
10+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
1011
Type[] typeArgs = classInfo.typeArgs;
1112
boolean isCollectionValueNullable = true;
1213
if (cacheKey.endsWith("__value_not_nullable")) {
@@ -24,15 +25,27 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
2425
ctx.append("if (obj == null) { stream.writeNull(); return; }");
2526
ctx.append("java.util.Map map = (java.util.Map)obj;");
2627
ctx.append("java.util.Iterator iter = map.entrySet().iterator();");
27-
ctx.append("if(!iter.hasNext()) { return; }");
28+
if (noIndention) {
29+
ctx.append("if(!iter.hasNext()) { return; }");
30+
} else {
31+
ctx.append("if(!iter.hasNext()) { stream.write((byte)'{', (byte)'}'); return; }");
32+
}
2833
ctx.append("java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();");
29-
ctx.buffer('{');
34+
if (noIndention) {
35+
ctx.buffer('{');
36+
} else {
37+
ctx.append("stream.writeObjectStart(); stream.writeIndention();");
38+
}
3039
if (keyType == String.class) {
3140
ctx.append("stream.writeVal((java.lang.String)entry.getKey());");
3241
} else {
3342
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeMapKey(\"%s\", entry.getKey(), stream);", mapCacheKey));
3443
}
35-
ctx.append("stream.write(':');");
44+
if (noIndention) {
45+
ctx.append("stream.write(':');");
46+
} else {
47+
ctx.append("stream.write((byte)':', (byte)' ');");
48+
}
3649
if (isCollectionValueNullable) {
3750
ctx.append("if (entry.getValue() == null) { stream.writeNull(); } else {");
3851
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, true);
@@ -42,13 +55,21 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
4255
}
4356
ctx.append("while(iter.hasNext()) {");
4457
ctx.append("entry = (java.util.Map.Entry)iter.next();");
45-
ctx.append("stream.write(',');");
58+
if (noIndention) {
59+
ctx.append("stream.write(',');");
60+
} else {
61+
ctx.append("stream.writeMore();");
62+
}
4663
if (keyType == String.class) {
4764
ctx.append("stream.writeVal((java.lang.String)entry.getKey());");
4865
} else {
4966
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeMapKey(\"%s\", entry.getKey(), stream);", mapCacheKey));
5067
}
51-
ctx.append("stream.write(':');");
68+
if (noIndention) {
69+
ctx.append("stream.write(':');");
70+
} else {
71+
ctx.append("stream.write((byte)':', (byte)' ');");
72+
}
5273
if (isCollectionValueNullable) {
5374
ctx.append("if (entry.getValue() == null) { stream.writeNull(); } else {");
5475
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, true);
@@ -57,7 +78,11 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
5778
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, false);
5879
}
5980
ctx.append("}");
60-
ctx.buffer('}');
81+
if (noIndention) {
82+
ctx.buffer('}');
83+
} else {
84+
ctx.append("stream.writeObjectEnd();");
85+
}
6186
ctx.append("}");
6287
return ctx;
6388
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ public static void genWriteOp(CodegenResult ctx, String code, Type valueType, bo
269269
}
270270

271271
public static void genWriteOp(CodegenResult ctx, String code, Type valueType, boolean isNullable, boolean isCollectionValueNullable) {
272-
boolean supportBuffer = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
272+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
273273
String cacheKey = TypeLiteral.create(valueType).getEncoderCacheKey();
274274
if (JsoniterSpi.getEncoder(cacheKey) == null) {
275-
if (supportBuffer && !isNullable && String.class == valueType) {
275+
if (noIndention && !isNullable && String.class == valueType) {
276276
ctx.buffer('"');
277277
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeStringWithoutQuote((java.lang.String)%s, stream);", code));
278278
ctx.buffer('"');
@@ -318,12 +318,21 @@ public static String getTypeName(Type fieldType) {
318318
}
319319
}
320320
public static CodegenResult genEnum(Class clazz) {
321+
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
321322
CodegenResult ctx = new CodegenResult();
322323
ctx.append(String.format("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {", clazz.getCanonicalName()));
323324
ctx.append("if (obj == null) { stream.writeNull(); return; }");
324-
ctx.buffer('"');
325+
if (noIndention) {
326+
ctx.buffer('"');
327+
} else {
328+
ctx.append("stream.write('\"');");
329+
}
325330
ctx.append("stream.writeRaw(obj.toString());");
326-
ctx.buffer('"');
331+
if (noIndention) {
332+
ctx.buffer('"');
333+
} else {
334+
ctx.append("stream.write('\"');");
335+
}
327336
ctx.append("}");
328337
return ctx;
329338
}

0 commit comments

Comments
 (0)