2021-03-24 10:10:19 +00:00
|
|
|
/*
|
2021-03-25 20:21:46 +00:00
|
|
|
This code is from https://gist.github.com/jjfiv/2ac5c081e088779f49aa, which is BSD licensed:
|
|
|
|
http://lemurproject.org/galago-license
|
2021-03-24 10:10:19 +00:00
|
|
|
*/
|
2021-03-25 20:21:46 +00:00
|
|
|
package biz.nellemann.syslogd.parser;
|
2021-03-24 10:10:19 +00:00
|
|
|
|
|
|
|
public class JsonUtil {
|
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
public static String encode(String input) {
|
2021-03-24 10:10:19 +00:00
|
|
|
|
2023-02-05 13:38:54 +00:00
|
|
|
if(input == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
StringBuilder output = new StringBuilder();
|
2021-03-24 10:10:19 +00:00
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
for (int i = 0; i < input.length(); i++) {
|
|
|
|
char ch = input.charAt(i);
|
2021-03-24 10:10:19 +00:00
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
// let's not put any nulls in our strings
|
|
|
|
if ((int) ch == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-24 10:10:19 +00:00
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
if (ch == '\n') {
|
|
|
|
output.append("\\n");
|
|
|
|
} else if (ch == '\t') {
|
|
|
|
output.append("\\t");
|
|
|
|
} else if (ch == '\r') {
|
|
|
|
output.append("\\r");
|
|
|
|
} else if (ch == '\\') {
|
|
|
|
output.append("\\\\");
|
|
|
|
} else if (ch == '"') {
|
|
|
|
output.append("\\\"");
|
|
|
|
} else if (ch == '\b') {
|
|
|
|
output.append("\\b");
|
|
|
|
} else if (ch == '\f') {
|
|
|
|
output.append("\\f");
|
|
|
|
} else if ((int) ch > 127) {
|
|
|
|
output.append(String.format("\\u%04x", (int) ch));
|
|
|
|
} else {
|
|
|
|
output.append(ch);
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 10:10:19 +00:00
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
return output.toString();
|
|
|
|
}
|
2021-03-24 10:10:19 +00:00
|
|
|
|
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
public static String decode(String input) {
|
|
|
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < input.length()) {
|
|
|
|
char delimiter = input.charAt(i);
|
|
|
|
i++; // consume letter or backslash
|
|
|
|
|
|
|
|
if (delimiter == '\\' && i < input.length()) {
|
|
|
|
|
|
|
|
// consume first after backslash
|
|
|
|
char ch = input.charAt(i);
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (ch == '\\' || ch == '/' || ch == '"' || ch == '\'') {
|
|
|
|
builder.append(ch);
|
|
|
|
} else if (ch == 'n') builder.append('\n');
|
|
|
|
else if (ch == 'r') builder.append('\r');
|
|
|
|
else if (ch == 't') builder.append('\t');
|
|
|
|
else if (ch == 'b') builder.append('\b');
|
|
|
|
else if (ch == 'f') builder.append('\f');
|
|
|
|
else if (ch == 'u') {
|
|
|
|
|
|
|
|
StringBuilder hex = new StringBuilder();
|
|
|
|
|
|
|
|
// expect 4 digits
|
|
|
|
if (i + 4 > input.length()) {
|
|
|
|
throw new RuntimeException("Not enough unicode digits! ");
|
|
|
|
}
|
|
|
|
for (char x : input.substring(i, i + 4).toCharArray()) {
|
|
|
|
if (!Character.isLetterOrDigit(x)) {
|
|
|
|
throw new RuntimeException("Bad character in unicode escape.");
|
|
|
|
}
|
|
|
|
hex.append(Character.toLowerCase(x));
|
|
|
|
}
|
|
|
|
i += 4; // consume those four digits.
|
|
|
|
|
|
|
|
int code = Integer.parseInt(hex.toString(), 16);
|
|
|
|
builder.append((char) code);
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Illegal escape sequence: \\" + ch);
|
|
|
|
}
|
|
|
|
} else { // it's not a backslash, or it's the last character.
|
|
|
|
builder.append(delimiter);
|
2021-03-24 10:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:15:23 +00:00
|
|
|
return builder.toString();
|
|
|
|
}
|
2021-03-24 10:10:19 +00:00
|
|
|
|
|
|
|
}
|