-
Notifications
You must be signed in to change notification settings - Fork 514
/
CodegenResult.java
130 lines (115 loc) · 4.16 KB
/
CodegenResult.java
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
package com.jsoniter.output;
import com.jsoniter.spi.JsoniterSpi;
class CodegenResult {
private final boolean supportBuffer;
String prelude = null; // first
String epilogue = null; // last
private StringBuilder lines = new StringBuilder();
private StringBuilder buffered = new StringBuilder();
public CodegenResult() {
supportBuffer = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
}
public void append(String str) {
if (str.contains("stream")) {
// maintain the order of write op
// must flush now
appendBuffer();
}
lines.append(str);
lines.append("\n");
}
public void buffer(char c) {
if (supportBuffer) {
buffered.append(c);
} else {
throw new UnsupportedOperationException("internal error: should not call buffer when indention step > 0");
}
}
public void buffer(String s) {
if (s == null) {
return;
}
if (supportBuffer) {
buffered.append(s);
} else {
throw new UnsupportedOperationException("internal error: should not call buffer when indention step > 0");
}
}
public void flushBuffer() {
if (buffered.length() == 0) {
return;
}
if (prelude == null) {
prelude = buffered.toString();
} else {
epilogue = buffered.toString();
}
buffered.setLength(0);
}
public String toString() {
return lines.toString();
}
public void appendBuffer() {
flushBuffer();
if (epilogue != null) {
lines.append(bufferToWriteOp(epilogue));
lines.append("\n");
epilogue = null;
}
}
public String generateWrapperCode(Class clazz) {
flushBuffer();
StringBuilder lines = new StringBuilder();
append(lines, "public void encode(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {");
append(lines, "if (obj == null) { stream.writeNull(); return; }");
if (prelude != null) {
append(lines, CodegenResult.bufferToWriteOp(prelude));
}
append(lines, String.format("encode_((%s)obj, stream);", clazz.getCanonicalName()));
if (epilogue != null) {
append(lines, CodegenResult.bufferToWriteOp(epilogue));
}
append(lines, "}");
return lines.toString();
}
private static void append(StringBuilder lines, String line) {
lines.append(line);
lines.append('\n');
}
public static String bufferToWriteOp(String buffered) {
if (buffered == null) {
return "";
}
if (buffered.length() == 1) {
return String.format("stream.write((byte)'%s');", escape(buffered.charAt(0)));
} else if (buffered.length() == 2) {
return String.format("stream.write((byte)'%s', (byte)'%s');",
escape(buffered.charAt(0)), escape(buffered.charAt(1)));
} else if (buffered.length() == 3) {
return String.format("stream.write((byte)'%s', (byte)'%s', (byte)'%s');",
escape(buffered.charAt(0)), escape(buffered.charAt(1)), escape(buffered.charAt(2)));
} else if (buffered.length() == 4) {
return String.format("stream.write((byte)'%s', (byte)'%s', (byte)'%s', (byte)'%s');",
escape(buffered.charAt(0)), escape(buffered.charAt(1)), escape(buffered.charAt(2)), escape(buffered.charAt(3)));
} else {
StringBuilder escaped = new StringBuilder();
for (int i = 0; i < buffered.length(); i++) {
char c = buffered.charAt(i);
if (c == '"') {
escaped.append('\\');
}
escaped.append(c);
}
return String.format("stream.writeRaw(\"%s\", %s);", escaped.toString(), buffered.length());
}
}
private static String escape(char c) {
if (c == '"') {
return "\\\"";
}
if (c == '\\') {
return "\\\\";
}
return String.valueOf(c);
}
}