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

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

Introduction

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

Prototype

public char[] quoteAsString(String input) 

Source Link

Document

Method that will quote text contents using JSON standard quoting, and return results as a character array

Usage

From source file:org.jboss.aerogear.io.netty.handler.codec.sockjs.protocol.MessageFrame.java

private static ByteBuf generateContent(final List<String> messages) {
    final JsonStringEncoder jsonEndocder = new JsonStringEncoder();
    final ByteBuf content = Unpooled.buffer();
    content.writeByte('a').writeByte('[');
    final int size = messages.size();
    for (int i = 0; i < size; i++) {
        content.writeByte('"');
        final String element = messages.get(i);
        if (element == null) {
            messages.subList(i, size).clear();
            break;
        }//  www.ja  v a  2 s .  c om
        final String escaped = escapeCharacters(jsonEndocder.quoteAsString(element));
        final ByteBuf escapedBuf = Unpooled.copiedBuffer(escaped, CharsetUtil.UTF_8);
        content.writeBytes(escapedBuf).writeByte('"');
        escapedBuf.release();
        if (i < size - 1) {
            content.writeByte(',');
        }
    }
    return content.writeByte(']');
}

From source file:org.jboss.pnc.environment.openshift.OpenshiftStartedEnvironment.java

/**
 * Return an escaped string of the JSON representation of the object
 *
 * By 'escaped', it means that strings like '"' are escaped to '\"'
 * @param object object to marshall//  w ww .  j a v a 2 s. c  o  m
 * @return Escaped Json String
 */
private String toEscapedJsonString(Object object) {
    ObjectMapper mapper = new ObjectMapper();
    JsonStringEncoder jsonStringEncoder = JsonStringEncoder.getInstance();
    try {
        return new String(jsonStringEncoder.quoteAsString(mapper.writeValueAsString(object)));
    } catch (JsonProcessingException e) {
        logger.error("Could not parse object: " + object, e);
        throw new RuntimeException(e);
    }
}