org.apache.logging.log4j.core.util.JsonUtils Java Examples

The following examples show how to use org.apache.logging.log4j.core.util.JsonUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiscordService.java    From Game with GNU General Public License v3.0 6 votes vote down vote up
private static void sendToDiscord(final String webhookUrl, final String message) throws Exception {
	final StringBuilder sb = new StringBuilder();
	JsonUtils.quoteAsString(message, sb);

	final String jsonPostBody = String.format("{\"content\": \"%s\"}", sb);

	final java.net.URL url = new java.net.URL(webhookUrl);

	final URLConnection con = url.openConnection();
	final HttpURLConnection http = (HttpURLConnection) con;
	http.setRequestMethod("POST");
	http.setDoOutput(true);

	final byte[] out = jsonPostBody.getBytes(StandardCharsets.UTF_8);
	final int length = out.length;

	http.setFixedLengthStreamingMode(length);
	http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
	try {
		http.connect();
		try (OutputStream os = http.getOutputStream()) {
			os.write(out);
		}
	}
	catch (Exception e) {
		LOGGER.error(e);
	}
}
 
Example #2
Source File: GelfLayout.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final String key, final Object value, final StringBuilder stringBuilder) {
    if (checker.check(key)) {
        stringBuilder.append(QU);
        JsonUtils.quoteAsString(key, stringBuilder);
        stringBuilder.append("\":\"");
        JsonUtils.quoteAsString(toNullSafeString(String.valueOf(value)), stringBuilder);
        stringBuilder.append(QC);
    }
}
 
Example #3
Source File: StringUtils.java    From demo with MIT License 2 votes vote down vote up
/**
 * Given a string, replaces characters so that it is safe to use
 * as JSON
 * @param value String value to convert
 * @return a properly escaped string, usable in JSON
 */
public static String escapeForJson(String value) {
    StringBuilder sb = new StringBuilder();
    JsonUtils.quoteAsString(value, sb);
    return sb.toString();
}