For creating large data, I think that the option to use Jackson or Gson is normal, I want to create small data with simple functions without relying on rich libraries. The problem at such times is the string escaping process.
Target character | Byte code | Escape notation |
---|---|---|
BS | 0x08 | \b |
HT | 0x09 | \t |
LF | 0x0A | \n |
FF | 0x0C | \f |
CR | 0x0D | \r |
" | 0x22 | " |
/ | 0x2F | / |
\ | 0x5C | \ \ |
This method replaces character strings byte by byte.
private static String escapeJsonString(CharSequence cs) {
final byte BACKSLASH = 0x5C;
final byte[] BS = new byte[]{BACKSLASH, 0x62}; /* \\b */
final byte[] HT = new byte[]{BACKSLASH, 0x74}; /* \\t */
final byte[] LF = new byte[]{BACKSLASH, 0x6E}; /* \\n */
final byte[] FF = new byte[]{BACKSLASH, 0x66}; /* \\f */
final byte[] CR = new byte[]{BACKSLASH, 0x72}; /* \\r */
try (
ByteArrayOutputStream strm = new ByteArrayOutputStream();
) {
byte[] bb = cs.toString().getBytes(StandardCharsets.UTF_8);
for (byte b : bb) {
if (b == 0x08 /* BS */) {
strm.write(BS);
} else if (b == 0x09 /* HT */) {
strm.write(HT);
} else if (b == 0x0A /* LF */) {
strm.write(LF);
} else if (b == 0x0C /* FF */) {
strm.write(FF);
} else if (b == 0x0D /* CR */) {
strm.write(CR);
} else if (
b == 0x22 /* " */
|| b == 0x2F /* / */
|| b == BACKSLASH /* \\ */
) {
strm.write(BACKSLASH);
strm.write(b);
} else {
strm.write(b);
}
}
return new String(strm.toByteArray(), StandardCharsets.UTF_8);
}
catch (IOException notHappen) {
throw new RuntimeException(notHappen);
}
}
public static void main(String[] args) {
String before = "abc \b \t \n \f \r / \\ \"";
String escaped = escapeJsonString(before);
System.out.println("{\"value\":\"" + escaped + "\"}");
/* {"value":"abc \b \t \n \f \r \/ \\ \""} */
}
Recommended Posts