Skip to content

Commit 560f386

Browse files
committed
optimize write true/false/null
1 parent a916e86 commit 560f386

5 files changed

Lines changed: 57 additions & 53 deletions

File tree

src/main/java/com/jsoniter/extra/Base64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static int encodeToBytes(byte[] sArr, JsonStream stream) throws IOException {
145145
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
146146

147147
// Set last four chars
148-
stream.write(BA[i >> 12], BA[(i >>> 6) & 0x3f], left == 2 ? BA[i & 0x3f] : (byte)'=', '=');
148+
stream.write(BA[i >> 12], BA[(i >>> 6) & 0x3f], left == 2 ? BA[i & 0x3f] : (byte)'=', (byte)'=');
149149
}
150150

151151
return dLen;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public static String bufferToWriteOp(String buffered) {
1212
return "";
1313
}
1414
if (buffered.length() == 1) {
15-
return String.format("stream.write('%s');", escape(buffered.charAt(0)));
15+
return String.format("stream.write((byte)'%s');", escape(buffered.charAt(0)));
1616
} else if (buffered.length() == 2) {
17-
return String.format("stream.write('%s', '%s');",
17+
return String.format("stream.write((byte)'%s', (byte)'%s');",
1818
escape(buffered.charAt(0)), escape(buffered.charAt(1)));
1919
} else if (buffered.length() == 3) {
20-
return String.format("stream.write('%s', '%s', '%s');",
20+
return String.format("stream.write((byte)'%s', (byte)'%s', (byte)'%s');",
2121
escape(buffered.charAt(0)), escape(buffered.charAt(1)), escape(buffered.charAt(2)));
22+
} else if (buffered.length() == 4) {
23+
return String.format("stream.write((byte)'%s', (byte)'%s', (byte)'%s', (byte)'%s');",
24+
escape(buffered.charAt(0)), escape(buffered.charAt(1)), escape(buffered.charAt(2)), escape(buffered.charAt(3)));
2225
} else {
2326
StringBuilder escaped = new StringBuilder();
2427
for (int i = 0; i < buffered.length(); i++) {

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

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public class JsonStream extends OutputStream {
1414
public int indentionStep = defaultIndentionStep;
1515
private int indention = 0;
1616
private OutputStream out;
17-
private static final byte[] NULL = "null".getBytes();
18-
private static final byte[] TRUE = "true".getBytes();
19-
private static final byte[] FALSE = "false".getBytes();
2017
byte buf[];
2118
int count;
2219

@@ -40,31 +37,54 @@ public final void write(int b) throws IOException {
4037
buf[count++] = (byte) b;
4138
}
4239

43-
public final void write(int b1, int b2) throws IOException {
40+
public final void write(byte b1, byte b2) throws IOException {
4441
if (count >= buf.length - 1) {
4542
flushBuffer();
4643
}
47-
buf[count++] = (byte) b1;
48-
buf[count++] = (byte) b2;
44+
buf[count++] = b1;
45+
buf[count++] = b2;
4946
}
5047

51-
public final void write(int b1, int b2, int b3) throws IOException {
48+
public final void write(byte b1, byte b2, byte b3) throws IOException {
5249
if (count >= buf.length - 2) {
5350
flushBuffer();
5451
}
55-
buf[count++] = (byte) b1;
56-
buf[count++] = (byte) b2;
57-
buf[count++] = (byte) b3;
52+
buf[count++] = b1;
53+
buf[count++] = b2;
54+
buf[count++] = b3;
5855
}
5956

60-
public final void write(int b1, int b2, int b3, int b4) throws IOException {
57+
public final void write(byte b1, byte b2, byte b3, byte b4) throws IOException {
6158
if (count >= buf.length - 3) {
6259
flushBuffer();
6360
}
64-
buf[count++] = (byte) b1;
65-
buf[count++] = (byte) b2;
66-
buf[count++] = (byte) b3;
67-
buf[count++] = (byte) b4;
61+
buf[count++] = b1;
62+
buf[count++] = b2;
63+
buf[count++] = b3;
64+
buf[count++] = b4;
65+
}
66+
67+
public final void write(byte b1, byte b2, byte b3, byte b4, byte b5) throws IOException {
68+
if (count >= buf.length - 4) {
69+
flushBuffer();
70+
}
71+
buf[count++] = b1;
72+
buf[count++] = b2;
73+
buf[count++] = b3;
74+
buf[count++] = b4;
75+
buf[count++] = b5;
76+
}
77+
78+
public final void write(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6) throws IOException {
79+
if (count >= buf.length - 5) {
80+
flushBuffer();
81+
}
82+
buf[count++] = b1;
83+
buf[count++] = b2;
84+
buf[count++] = b3;
85+
buf[count++] = b4;
86+
buf[count++] = b5;
87+
buf[count++] = b6;
6888
}
6989

7090
public final void write(byte b[], int off, int len) throws IOException {
@@ -117,7 +137,7 @@ public final void writeRaw(String val) throws IOException {
117137

118138
public final void writeRaw(String val, int remaining) throws IOException {
119139
int i = 0;
120-
for(;;) {
140+
for (; ; ) {
121141
int available = buf.length - count;
122142
if (available < remaining) {
123143
remaining -= available;
@@ -156,11 +176,11 @@ public final void writeVal(boolean val) throws IOException {
156176
}
157177

158178
public final void writeTrue() throws IOException {
159-
write(TRUE);
179+
write((byte) 't', (byte) 'r', (byte) 'u', (byte) 'e');
160180
}
161181

162182
public final void writeFalse() throws IOException {
163-
write(FALSE);
183+
write((byte) 'f', (byte) 'a', (byte) 'l', (byte) 's', (byte) 'e');
164184
}
165185

166186
public final void writeVal(Short val) throws IOException {
@@ -230,17 +250,15 @@ public final void writeVal(Any val) throws IOException {
230250
}
231251

232252
public final void writeNull() throws IOException {
233-
write(NULL, 0, NULL.length);
253+
write((byte) 'n', (byte) 'u', (byte) 'l', (byte) 'l');
234254
}
235255

236256
public final void writeEmptyObject() throws IOException {
237-
write('{');
238-
write('}');
257+
write((byte) '{', (byte) '}');
239258
}
240259

241260
public final void writeEmptyArray() throws IOException {
242-
write('[');
243-
write(']');
261+
write((byte) '[', (byte) ']');
244262
}
245263

246264
public final void writeArrayStart() throws IOException {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,33 @@ private static void writeStringSlowPath(JsonStream stream, String val, int i, in
8181
for (; i < valLen; i++) {
8282
int c = val.charAt(i);
8383
if (c > 125) {
84-
stream.write('\\', 'u');
8584
byte b4 = (byte) (c & 0xf);
8685
byte b3 = (byte) (c >> 4 & 0xf);
8786
byte b2 = (byte) (c >> 8 & 0xf);
8887
byte b1 = (byte) (c >> 12 & 0xf);
89-
stream.write(ITOA[b1], ITOA[b2], ITOA[b3], ITOA[b4]);
88+
stream.write((byte)'\\', (byte)'u', ITOA[b1], ITOA[b2], ITOA[b3], ITOA[b4]);
9089
} else {
9190
switch (c) {
9291
case '"':
93-
stream.write('\\', '"');
92+
stream.write((byte)'\\', (byte)'"');
9493
break;
9594
case '\\':
96-
stream.write('\\', '\\');
95+
stream.write((byte)'\\', (byte)'\\');
9796
break;
9897
case '\b':
99-
stream.write('\\', 'b');
98+
stream.write((byte)'\\', (byte)'b');
10099
break;
101100
case '\f':
102-
stream.write('\\', 'f');
101+
stream.write((byte)'\\', (byte)'f');
103102
break;
104103
case '\n':
105-
stream.write('\\', 'n');
104+
stream.write((byte)'\\', (byte)'n');
106105
break;
107106
case '\r':
108-
stream.write('\\', 'r');
107+
stream.write((byte)'\\', (byte)'r');
109108
break;
110109
case '\t':
111-
stream.write('\\', 't');
110+
stream.write((byte)'\\', (byte)'t');
112111
break;
113112
default:
114113
stream.write(c);

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class TestObject extends TestCase {
1414

1515
static {
1616
JsoniterAnnotationSupport.enable();
17-
JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
17+
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
1818
}
1919

2020
private ByteArrayOutputStream baos;
@@ -233,20 +233,4 @@ public void test_omit_null() {
233233
obj.field3 = "hello";
234234
assertEquals("{\"field3\":\"hello\"}", JsonStream.serialize(obj));
235235
}
236-
237-
public static class User {
238-
@JsonProperty(nullable = false)
239-
public String firstName;
240-
@JsonProperty(nullable = false)
241-
public String lastName;
242-
public int score;
243-
}
244-
245-
public void test() throws IOException {
246-
JsoniterAnnotationSupport.enable();
247-
User user = new User();
248-
user.firstName = "a";
249-
user.lastName = "b";
250-
assertEquals("", JsonStream.serialize(user));
251-
}
252236
}

0 commit comments

Comments
 (0)