Example usage for com.fasterxml.jackson.core JsonEncoding getJavaName

List of usage examples for com.fasterxml.jackson.core JsonEncoding getJavaName

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonEncoding getJavaName.

Prototype

public String getJavaName() 

Source Link

Document

Method for accessing encoding name that JDK will support.

Usage

From source file:sys.core.jackson.MappingJacksonHttpMessageConverter.java

protected JsonEncoding getJsonEncoding(MediaType contentType) {
    if (contentType != null && contentType.getCharSet() != null) {
        Charset charset = contentType.getCharSet();
        for (JsonEncoding encoding : JsonEncoding.values()) {
            if (charset.name().equals(encoding.getJavaName())) {
                return encoding;
            }/*  w w  w  . ja  v  a 2s .co m*/
        }
    }
    return JsonEncoding.UTF8;
}

From source file:org.springframework.http.converter.json.replacement.MappingJacksonHttpMessageConverter.java

private JsonEncoding getEncoding(MediaType contentType) {
    if (contentType != null && contentType.getCharSet() != null) {
        Charset charset = contentType.getCharSet();
        for (JsonEncoding encoding : JsonEncoding.values()) {
            if (charset.name().equals(encoding.getJavaName())) {
                return encoding;
            }// w  ww  .  j a  va  2  s . c om
        }
    }
    return JsonEncoding.UTF8;
}

From source file:tuwien.aic.crowdsourcing.util.MappingJackson2HttpMessageConverter.java

/**
 * Determine the JSON encoding to use for the given content type.
 * // w  ww  .  ja  v  a2  s  .  co  m
 * @param contentType the media type as requested by the caller
 * @return the JSON encoding to use (never <code>null</code>)
 */
protected JsonEncoding getJsonEncoding(MediaType contentType) {
    if ((contentType != null) && (contentType.getCharSet() != null)) {
        Charset charset = contentType.getCharSet();
        for (JsonEncoding encoding : JsonEncoding.values()) {
            if (charset.name().equals(encoding.getJavaName())) {
                return encoding;
            }
        }
    }
    return JsonEncoding.UTF8;
}

From source file:net.logstash.logback.composite.CompositeJsonFormatter.java

public void setEncoding(String encodingName) {
    for (JsonEncoding encoding : JsonEncoding.values()) {
        if (encoding.getJavaName().equals(encodingName) || encoding.name().equals(encodingName)) {
            this.encoding = encoding;
            return;
        }//from   w  w w  . j av  a  2  s .  co  m
    }
    throw new IllegalArgumentException("Unknown encoding " + encodingName);
}

From source file:org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcDecoder.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }//  w w w  .  j  a v  a  2s  . c o  m

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int i = lastRecordBytes + buf.readerIndex();

    for (; i < buf.writerIndex(); i++) {
        switch (buf.getByte(i)) {
        case '{':
            if (!inS)
                leftCurlies++;
            break;
        case '}':
            if (!inS)
                rightCurlies++;
            break;
        case '"': {
            if (buf.getByte(i - 1) != '\\')
                inS = !inS;
            break;
        }
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + i - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (i - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, i - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (i >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }/*w w w.j  a  v a2  s.  c o m*/

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int index = lastRecordBytes + buf.readerIndex();

    for (; index < buf.writerIndex(); index++) {
        switch (buf.getByte(index)) {
        case '{':
            if (!inS) {
                leftCurlies++;
            }
            break;
        case '}':
            if (!inS) {
                rightCurlies++;
            }
            break;
        case '"':
            if (buf.getByte(index - 1) != '\\') {
                inS = !inS;
            }
            break;
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (index - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, index - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (index >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}

From source file:com.fiadot.springjsoncrypt.json.CryptMappingJacson2HttpMessageConverter.java

/**
 * Determine the JSON encoding to use for the given content type.
 * @param contentType the media type as requested by the caller
 * @return the JSON encoding to use (never {@code null})
 *//*from w  w w .jav a2 s. co  m*/
protected JsonEncoding getJsonEncoding(MediaType contentType) {
    if (contentType != null && contentType.getCharSet() != null) {
        Charset charset = contentType.getCharSet();
        for (JsonEncoding encoding : JsonEncoding.values()) {
            if (charset.name().equals(encoding.getJavaName())) {
                return encoding;
            }
        }
    }
    return JsonEncoding.UTF8;
}