forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenImplMap.java
More file actions
92 lines (88 loc) · 3.82 KB
/
CodegenImplMap.java
File metadata and controls
92 lines (88 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.jsoniter.output;
import com.jsoniter.spi.ClassInfo;
import com.jsoniter.spi.JsoniterSpi;
import java.lang.reflect.Type;
class CodegenImplMap {
public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
Type[] typeArgs = classInfo.typeArgs;
boolean isCollectionValueNullable = true;
if (cacheKey.endsWith("__value_not_nullable")) {
isCollectionValueNullable = false;
}
Type keyType = Object.class;
Type valueType = Object.class;
if (typeArgs.length == 2) {
keyType = typeArgs[0];
valueType = typeArgs[1];
}
CodegenResult ctx = new CodegenResult();
ctx.append("public static void encode_(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
ctx.append("if (obj == null) { stream.writeNull(); return; }");
ctx.append("java.util.Map map = (java.util.Map)obj;");
ctx.append("java.util.Iterator iter = map.entrySet().iterator();");
if (noIndention) {
ctx.append("if(!iter.hasNext()) { return; }");
} else {
ctx.append("if(!iter.hasNext()) { stream.write((byte)'{', (byte)'}'); return; }");
}
ctx.append("java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();");
if (noIndention) {
ctx.buffer('{');
} else {
ctx.append("stream.writeObjectStart(); stream.writeIndention();");
}
genWriteMapKey(ctx, keyType, noIndention);
if (isCollectionValueNullable) {
ctx.append("if (entry.getValue() == null) { stream.writeNull(); } else {");
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, true);
ctx.append("}");
} else {
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, false);
}
ctx.append("while(iter.hasNext()) {");
ctx.append("entry = (java.util.Map.Entry)iter.next();");
if (noIndention) {
ctx.append("stream.write(',');");
} else {
ctx.append("stream.writeMore();");
}
genWriteMapKey(ctx, keyType, noIndention);
if (isCollectionValueNullable) {
ctx.append("if (entry.getValue() == null) { stream.writeNull(); } else {");
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, true);
ctx.append("}");
} else {
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", valueType, false);
}
ctx.append("}");
if (noIndention) {
ctx.buffer('}');
} else {
ctx.append("stream.writeObjectEnd();");
}
ctx.append("}");
return ctx;
}
private static void genWriteMapKey(CodegenResult ctx, Type keyType, boolean noIndention) {
if (keyType == Object.class) {
ctx.append("stream.writeObjectField(entry.getKey());");
return;
}
if (keyType == String.class) {
ctx.append("stream.writeVal((java.lang.String)entry.getKey());");
} else if (CodegenImplNative.NATIVE_ENCODERS.containsKey(keyType)) {
ctx.append("stream.write('\"');");
ctx.append(String.format("stream.writeVal((%s)entry.getKey());", CodegenImplNative.getTypeName(keyType)));
ctx.append("stream.write('\"');");
} else {
String mapCacheKey = JsoniterSpi.getMapKeyEncoderCacheKey(keyType);
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeMapKey(\"%s\", entry.getKey(), stream);", mapCacheKey));
}
if (noIndention) {
ctx.append("stream.write(':');");
} else {
ctx.append("stream.write((byte)':', (byte)' ');");
}
}
}