Example usage for com.fasterxml.jackson.core.io JsonStringEncoder getInstance

List of usage examples for com.fasterxml.jackson.core.io JsonStringEncoder getInstance

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.io JsonStringEncoder getInstance.

Prototype

public static JsonStringEncoder getInstance() 

Source Link

Document

Factory method for getting an instance; this is either recycled per-thread instance, or a newly constructed one.

Usage

From source file:com.tectonica.thirdparty.Jackson2.java

public static String escape(String text) {
    return new String(JsonStringEncoder.getInstance().quoteAsUTF8(text));
}

From source file:com.fotonauts.lackr.TestESI.java

public String quoteJson(String text) {
    return new String(JsonStringEncoder.getInstance().quoteAsUTF8(text)).replaceAll("\\/", "\\\\/");
}

From source file:ameba.websocket.sockjs.frame.Jackson2SockJsMessageCodec.java

@Override
protected char[] applyJsonQuoting(String content) {
    return JsonStringEncoder.getInstance().quoteAsString(content);
}

From source file:net.sf.jasperreports.engine.export.JsonExporter.java

public static void writeParts(JasperPrint jasperPrint, Writer writer) throws IOException {
    PrintParts parts = jasperPrint.getParts();
    writer.write("{");

    writer.write("\"id\": \"parts_" + (parts.hashCode() & 0x7FFFFFFF) + "\",");
    writer.write("\"type\": \"reportparts\",");
    writer.write("\"parts\": [");

    if (!parts.startsAtZero()) {
        writer.write("{\"idx\": 0, \"name\": \"");
        writer.write(JsonStringEncoder.getInstance().quoteAsString(jasperPrint.getName()));
        writer.write("\"}");
        if (parts.partCount() > 1) {
            writer.write(",");
        }// ww w . ja  va2 s.  c  o m
    }

    Iterator<Map.Entry<Integer, PrintPart>> it = parts.partsIterator();

    while (it.hasNext()) {
        Map.Entry<Integer, PrintPart> partsEntry = it.next();
        int idx = partsEntry.getKey();
        PrintPart part = partsEntry.getValue();

        writer.write("{\"idx\": " + idx + ", \"name\": \"");
        writer.write(JsonStringEncoder.getInstance().quoteAsString(part.getName()));
        writer.write("\"}");
        if (it.hasNext()) {
            writer.write(",");
        }
    }

    writer.write("]");
    writer.write("}");
}

From source file:com.jaspersoft.studio.community.utils.CommunityAPIUtils.java

/**
 * Sanitize a string to be used in JSON data.
 *///from  w  w w  . j  a va  2 s  . c om
public static String jsonStringSanitize(String inputString) {
    char[] stringChars = JsonStringEncoder.getInstance().quoteAsString(inputString);
    return new String(stringChars);
}

From source file:org.brutusin.json.impl.JacksonCodec.java

@Override
public String quoteAsUTF8(String s) {
    return new String(JsonStringEncoder.getInstance().quoteAsUTF8(s));
}

From source file:org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.java

private static String toJson(String value) {
    return "\"" + new String(JsonStringEncoder.getInstance().quoteAsString(value)) + "\"";
}

From source file:net.sf.jasperreports.engine.export.JsonMetadataExporter.java

private void writeValue(Object value) throws IOException {
    if (value != null) {
        if (value instanceof Number || value instanceof Boolean) {
            writer.write(value.toString());
        } else if (value instanceof Date) {
            writer.write("\"");
            writer.write(isoDateFormat.format((Date) value));
            writer.write("\"");
        } else {//from w w  w. j  a  v a 2 s  . c  o m
            writer.write("\"");
            writer.write(JsonStringEncoder.getInstance().quoteAsString(value.toString()));
            writer.write("\"");
        }
    } else {
        writer.write("null"); // FIXMEJSONMETA: how to treat null values?
    }
}

From source file:org.apache.logging.log4j.core.layout.GelfLayoutTest.java

private void testCompressedLayout(final CompressionType compressionType, final boolean includeStacktrace,
        final boolean includeThreadContext, String host, final boolean includeNullDelimiter)
        throws IOException {
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);/*from   ww w  .j  a v a2 s  .com*/
    }
    // set up appenders
    final GelfLayout layout = GelfLayout.newBuilder().setConfiguration(ctx.getConfiguration()).setHost(host)
            .setAdditionalFields(new KeyValuePair[] { new KeyValuePair(KEY1, VALUE1),
                    new KeyValuePair(KEY2, "${java:runtime}"), })
            .setCompressionType(compressionType).setCompressionThreshold(1024)
            .setIncludeStacktrace(includeStacktrace).setIncludeThreadContext(includeThreadContext)
            .setIncludeNullDelimiter(includeNullDelimiter).build();
    final ListAppender eventAppender = new ListAppender("Events", null, null, true, false);
    final ListAppender rawAppender = new ListAppender("Raw", null, layout, true, true);
    final ListAppender formattedAppender = new ListAppender("Formatted", null, layout, true, false);
    final EncodingListAppender encodedAppender = new EncodingListAppender("Encoded", null, layout, false, true);
    eventAppender.start();
    rawAppender.start();
    formattedAppender.start();
    encodedAppender.start();

    if (host == null)
        host = NetUtils.getLocalHostname();

    final JavaLookup javaLookup = new JavaLookup();

    // set appenders on root and set level to debug
    root.addAppender(eventAppender);
    root.addAppender(rawAppender);
    root.addAppender(formattedAppender);
    root.addAppender(encodedAppender);
    root.setLevel(Level.DEBUG);

    root.debug(LINE1);

    ThreadContext.put(MDCKEY1, MDCVALUE1);
    ThreadContext.put(MDCKEY2, MDCVALUE2);

    root.info(LINE2);

    final Exception exception = new RuntimeException("some error");
    root.error(LINE3, exception);

    formattedAppender.stop();

    final List<LogEvent> events = eventAppender.getEvents();
    final List<byte[]> raw = rawAppender.getData();
    final List<String> messages = formattedAppender.getMessages();
    final List<byte[]> raw2 = encodedAppender.getData();
    final String threadName = Thread.currentThread().getName();

    //@formatter:off
    assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": "
            + GelfLayout.formatTimestamp(events.get(0).getTimeMillis()) + "," + "\"level\": 7,"
            + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE1
            + "\"," + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \""
            + javaLookup.getRuntime() + "\"" + "}", messages.get(0));

    assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": "
            + GelfLayout.formatTimestamp(events.get(1).getTimeMillis()) + "," + "\"level\": 6,"
            + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE2
            + "\","
            + (includeThreadContext
                    ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2
                            + "\","
                    : "")
            + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime()
            + "\"" + "}", messages.get(1));
    //@formatter:on
    final byte[] compressed = raw.get(2);
    final byte[] compressed2 = raw2.get(2);
    final ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
    final ByteArrayInputStream bais2 = new ByteArrayInputStream(compressed2);
    InputStream inflaterStream;
    InputStream inflaterStream2;
    switch (compressionType) {
    case GZIP:
        inflaterStream = new GZIPInputStream(bais);
        inflaterStream2 = new GZIPInputStream(bais2);
        break;
    case ZLIB:
        inflaterStream = new InflaterInputStream(bais);
        inflaterStream2 = new InflaterInputStream(bais2);
        break;
    case OFF:
        inflaterStream = bais;
        inflaterStream2 = bais2;
        break;
    default:
        throw new IllegalStateException("Missing test case clause");
    }
    final byte[] uncompressed = IOUtils.toByteArray(inflaterStream);
    final byte[] uncompressed2 = IOUtils.toByteArray(inflaterStream2);
    inflaterStream.close();
    inflaterStream2.close();
    final String uncompressedString = new String(uncompressed, layout.getCharset());
    final String uncompressedString2 = new String(uncompressed2, layout.getCharset());
    //@formatter:off
    final String expected = "{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": "
            + GelfLayout.formatTimestamp(events.get(2).getTimeMillis()) + "," + "\"level\": 3,"
            + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE3
            + "\"," + "\"full_message\": \""
            + String.valueOf(JsonStringEncoder.getInstance()
                    .quoteAsString(includeStacktrace ? GelfLayout.formatThrowable(exception).toString()
                            : exception.toString()))
            + "\","
            + (includeThreadContext
                    ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2
                            + "\","
                    : "")
            + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime()
            + "\"" + "}";
    //@formatter:on
    assertJsonEquals(expected, uncompressedString);
    assertJsonEquals(expected, uncompressedString2);
}

From source file:org.finra.herd.service.helper.notification.AbstractNotificationMessageBuilder.java

/**
 * JSON escapes a specified string. This method is null-safe.
 *
 * @param input the input string//  w w w .j av a2 s .  co  m
 *
 * @return the XML escaped string
 */
protected String escapeJson(final String input) {
    if (input == null) {
        return null;
    } else {
        return String.valueOf(JsonStringEncoder.getInstance().quoteAsString(input));
    }
}