Skip to content

Commit 9a9d94d

Browse files
committed
#93 some control character should be esacped specially
1 parent e2d2d76 commit 9a9d94d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ private static void writeStringSlowPath(JsonStream stream, String val, int i, in
132132
if (escapeUnicode) {
133133
for (; i < valLen; i++) {
134134
int c = val.charAt(i);
135-
if (c > 125 || c < 32) {
136-
byte b4 = (byte) (c & 0xf);
137-
byte b3 = (byte) (c >> 4 & 0xf);
138-
byte b2 = (byte) (c >> 8 & 0xf);
139-
byte b1 = (byte) (c >> 12 & 0xf);
140-
stream.write((byte) '\\', (byte) 'u', ITOA[b1], ITOA[b2], ITOA[b3], ITOA[b4]);
135+
if (c > 125) {
136+
writeAsSlashU(stream, c);
141137
} else {
142138
writeAsciiChar(stream, c);
143139
}
@@ -223,7 +219,19 @@ private static void writeAsciiChar(JsonStream stream, int c) throws IOException
223219
stream.write((byte) '\\', (byte) 't');
224220
break;
225221
default:
226-
stream.write(c);
222+
if (c < 32) {
223+
writeAsSlashU(stream, c);
224+
} else {
225+
stream.write(c);
226+
}
227227
}
228228
}
229+
230+
private static void writeAsSlashU(JsonStream stream, int c) throws IOException {
231+
byte b4 = (byte) (c & 0xf);
232+
byte b3 = (byte) (c >> 4 & 0xf);
233+
byte b2 = (byte) (c >> 8 & 0xf);
234+
byte b1 = (byte) (c >> 12 & 0xf);
235+
stream.write((byte) '\\', (byte) 'u', ITOA[b1], ITOA[b2], ITOA[b3], ITOA[b4]);
236+
}
229237
}

0 commit comments

Comments
 (0)