Escape only the required characters when emitting JSON. This means that instead of emitting
["foo\nbar", "baz"] we'll emit this: ["foo bar", baz"] This simple change measured about ~35% faster for in-memory writes!
This commit is contained in:
parent
7e1e4eab07
commit
ff7aa3f331
@ -384,43 +384,14 @@ public final class JsonWriter implements Closeable {
|
|||||||
* quotation mark, reverse solidus, and the control characters
|
* quotation mark, reverse solidus, and the control characters
|
||||||
* (U+0000 through U+001F)."
|
* (U+0000 through U+001F)."
|
||||||
*/
|
*/
|
||||||
switch (c) {
|
if (c == '"' || c == '\\') {
|
||||||
case '"':
|
|
||||||
case '\\':
|
|
||||||
case '/':
|
|
||||||
out.write('\\');
|
out.write('\\');
|
||||||
out.write(c);
|
out.write(c);
|
||||||
break;
|
} else if (c <= 0x1F) {
|
||||||
|
|
||||||
case '\t':
|
|
||||||
out.write("\\t");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\b':
|
|
||||||
out.write("\\b");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\n':
|
|
||||||
out.write("\\n");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\r':
|
|
||||||
out.write("\\r");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\f':
|
|
||||||
out.write("\\f");
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
if (c <= 0x1F) {
|
|
||||||
out.write(String.format("\\u%04x", (int) c));
|
out.write(String.format("\\u%04x", (int) c));
|
||||||
} else {
|
} else {
|
||||||
out.write(c);
|
out.write(c);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
out.write("\"");
|
out.write("\"");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user