forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenImplObject.java
More file actions
164 lines (154 loc) · 6.72 KB
/
CodegenImplObject.java
File metadata and controls
164 lines (154 loc) · 6.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.jsoniter.output;
import com.jsoniter.spi.*;
import java.util.*;
class CodegenImplObject {
public static CodegenResult genObject(ClassInfo classInfo) {
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
CodegenResult ctx = new CodegenResult();
ClassDescriptor desc = ClassDescriptor.getEncodingClassDescriptor(classInfo, false);
List<EncodeTo> encodeTos = desc.encodeTos();
ctx.append(String.format("public static void encode_(%s obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {", classInfo.clazz.getCanonicalName()));
if (hasFieldOutput(desc)) {
int notFirst = 0;
if (noIndention) {
ctx.buffer('{');
} else {
ctx.append("stream.writeObjectStart();");
}
for (EncodeTo encodeTo : encodeTos) {
notFirst = genField(ctx, encodeTo.binding, encodeTo.toName, notFirst);
}
for (UnwrapperDescriptor unwrapper : desc.unwrappers) {
if (unwrapper.isMap) {
ctx.append(String.format("java.util.Map map = (java.util.Map)obj.%s();", unwrapper.method.getName()));
ctx.append("java.util.Iterator iter = map.entrySet().iterator();");
ctx.append("while(iter.hasNext()) {");
ctx.append("java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();");
notFirst = appendComma(ctx, notFirst);
ctx.append("stream.writeObjectField(entry.getKey().toString());");
ctx.append("if (entry.getValue() == null) { stream.writeNull(); } else {");
CodegenImplNative.genWriteOp(ctx, "entry.getValue()", unwrapper.mapValueTypeLiteral.getType(), true);
ctx.append("}");
ctx.append("}");
} else {
notFirst = appendComma(ctx, notFirst);
ctx.append(String.format("obj.%s(stream);", unwrapper.method.getName()));
}
}
if (noIndention) {
ctx.buffer('}');
} else {
if (notFirst == 1) { // definitely not first
ctx.append("stream.writeObjectEnd();");
} else if (notFirst == 2) { // // maybe not first, previous field is omitNull
ctx.append("if (notFirst) { stream.writeObjectEnd(); } else { stream.write('}'); }");
} else { // this is the first
ctx.append("stream.write('}');");
}
}
} else {
ctx.buffer("{}");
}
ctx.append("}");
return ctx;
}
private static boolean hasFieldOutput(ClassDescriptor desc) {
if (!desc.unwrappers.isEmpty()) {
return true;
}
return !desc.encodeTos().isEmpty();
}
private static int genField(CodegenResult ctx, Binding binding, String toName, int notFirst) {
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
String fieldCacheKey = binding.encoderCacheKey();
Encoder encoder = JsoniterSpi.getEncoder(fieldCacheKey);
boolean isCollectionValueNullable = binding.isCollectionValueNullable;
Class valueClazz;
String valueAccessor;
if (binding.field != null) {
valueClazz = binding.field.getType();
valueAccessor = "obj." + binding.field.getName();
} else {
valueClazz = binding.method.getReturnType();
valueAccessor = "obj." + binding.method.getName() + "()";
}
if (!supportCollectionValueNullable(valueClazz)) {
isCollectionValueNullable = true;
}
boolean nullable = !valueClazz.isPrimitive();
boolean omitZero = JsoniterSpi.getCurrentConfig().omitDefaultValue();
if (!binding.isNullable) {
nullable = false;
}
if (binding.defaultValueToOmit != null) {
if (notFirst == 0) { // no previous field
notFirst = 2; // maybe
ctx.append("boolean notFirst = false;");
}
ctx.append("if (!(" + String.format(binding.defaultValueToOmit.code(), valueAccessor)+ ")) {");
notFirst = appendComma(ctx, notFirst);
if (noIndention) {
ctx.append(CodegenResult.bufferToWriteOp("\"" + toName + "\":"));
} else {
ctx.append(String.format("stream.writeObjectField(\"%s\");", toName));
}
} else {
notFirst = appendComma(ctx, notFirst);
if (noIndention) {
ctx.buffer('"');
ctx.buffer(toName);
ctx.buffer('"');
ctx.buffer(':');
} else {
ctx.append(String.format("stream.writeObjectField(\"%s\");", toName));
}
if (nullable) {
ctx.append(String.format("if (%s == null) { stream.writeNull(); } else {", valueAccessor));
}
}
if (encoder == null) {
CodegenImplNative.genWriteOp(ctx, valueAccessor, binding.valueType, nullable, isCollectionValueNullable);
} else {
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeVal(\"%s\", %s, stream);",
fieldCacheKey, valueAccessor));
}
if (nullable || omitZero) {
ctx.append("}");
}
return notFirst;
}
private static int appendComma(CodegenResult ctx, int notFirst) {
boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
if (notFirst == 1) { // definitely not first
if (noIndention) {
ctx.buffer(',');
} else {
ctx.append("stream.writeMore();");
}
} else if (notFirst == 2) { // maybe not first, previous field is omitNull
if (noIndention) {
ctx.append("if (notFirst) { stream.write(','); } else { notFirst = true; }");
} else {
ctx.append("if (notFirst) { stream.writeMore(); } else { stream.writeIndention(); notFirst = true; }");
}
} else { // this is the first, do not write comma
notFirst = 1;
if (!noIndention) {
ctx.append("stream.writeIndention();");
}
}
return notFirst;
}
private static boolean supportCollectionValueNullable(Class clazz) {
if (clazz.isArray()) {
return true;
}
if (Map.class.isAssignableFrom(clazz)) {
return true;
}
if (Collection.class.isAssignableFrom(clazz)) {
return true;
}
return false;
}
}