Search in sources :

Example 1 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project elasticsearch by elastic.

the class ElasticsearchHostsSniffer method readHosts.

private List<HttpHost> readHosts(HttpEntity entity) throws IOException {
    try (InputStream inputStream = entity.getContent()) {
        JsonParser parser = jsonFactory.createParser(inputStream);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("expected data to start with an object");
        }
        List<HttpHost> hosts = new ArrayList<>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                if ("nodes".equals(parser.getCurrentName())) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        JsonToken token = parser.nextToken();
                        assert token == JsonToken.START_OBJECT;
                        String nodeId = parser.getCurrentName();
                        HttpHost sniffedHost = readHost(nodeId, parser, this.scheme);
                        if (sniffedHost != null) {
                            logger.trace("adding node [" + nodeId + "]");
                            hosts.add(sniffedHost);
                        }
                    }
                } else {
                    parser.skipChildren();
                }
            }
        }
        return hosts;
    }
}
Also used : InputStream(java.io.InputStream) HttpHost(org.apache.http.HttpHost) ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 2 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project java-chassis by ServiceComb.

the class AbstractDeserializer method deserialize.

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Object result = createResult();
    for (String fieldName = p.nextFieldName(); fieldName != null; fieldName = p.nextFieldName()) {
        // p实际是ProtobufParser,其内部是可以直接取到proto field的,理论上可以根据id来索引
        // 可是field默认没暴露出来,所以,直接用name索引了
        AbstractFieldCodec.ReaderHelpData helpData = readerHelpDataMap.get(fieldName);
        if (helpData == null) {
            continue;
        }
        JsonToken t = p.nextToken();
        // Note: must handle null explicitly here; value deserializers won't
        Object value = null;
        if (t == JsonToken.VALUE_NULL) {
            value = helpData.getDeser().getNullValue(ctxt);
        } else {
            value = helpData.getDeser().deserialize(p, ctxt);
        }
        result = updateResult(result, value, helpData);
    }
    return result;
}
Also used : AbstractFieldCodec(io.servicecomb.codec.protobuf.codec.AbstractFieldCodec) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 3 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project jackson-core by FasterXML.

the class TrailingCommasTest method assertEnd.

private void assertEnd(JsonParser p) throws IOException {
    // Issue #325
    if (!(p instanceof UTF8DataInputJsonParser)) {
        JsonToken next = p.nextToken();
        assertNull("expected end of stream but found " + next, next);
    }
}
Also used : UTF8DataInputJsonParser(com.fasterxml.jackson.core.json.UTF8DataInputJsonParser) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 4 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project drill by apache.

the class JsonReader method write.

@Override
public ReadState write(ComplexWriter writer) throws IOException {
    ReadState readState = null;
    try {
        JsonToken t = lastSeenJsonToken;
        if (t == null || t == JsonToken.END_OBJECT) {
            t = parser.nextToken();
        }
        while (!parser.hasCurrentToken() && !parser.isClosed()) {
            t = parser.nextToken();
        }
        lastSeenJsonToken = null;
        if (parser.isClosed()) {
            return ReadState.END_OF_STREAM;
        }
        readState = writeToVector(writer, t);
        switch(readState) {
            case END_OF_STREAM:
                break;
            case WRITE_SUCCEED:
                break;
            default:
                throw getExceptionWithContext(UserException.dataReadError(), currentFieldName, null).message("Failure while reading JSON. (Got an invalid read state %s )", readState.toString()).build(logger);
        }
    } catch (com.fasterxml.jackson.core.JsonParseException ex) {
        if (ignoreJSONParseError()) {
            if (processJSONException() == JsonExceptionProcessingState.END_OF_STREAM) {
                return ReadState.JSON_RECORD_PARSE_EOF_ERROR;
            } else {
                return ReadState.JSON_RECORD_PARSE_ERROR;
            }
        } else {
            throw ex;
        }
    }
    return readState;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 5 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project alfresco-remote-api by Alfresco.

the class RestApiStringDeserializer method deserializeImpl.

private String deserializeImpl(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.hasToken(JsonToken.VALUE_STRING)) {
        return p.getText();
    }
    JsonToken t = p.getCurrentToken();
    // [databind#381]
    if (t == JsonToken.START_ARRAY) {
        return _deserializeFromArray(p, ctxt);
    }
    // need to gracefully handle byte[] data, as base64
    if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
        Object ob = p.getEmbeddedObject();
        if (ob == null) {
            return null;
        }
        if (ob instanceof byte[]) {
            return ctxt.getBase64Variant().encode((byte[]) ob, false);
        }
        // otherwise, try conversion using toString()...
        return ob.toString();
    }
    // allow coercions for other scalar types
    String text = p.getValueAsString();
    if (text != null) {
        return text;
    }
    return (String) ctxt.handleUnexpectedToken(_valueClass, p);
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)251 IOException (java.io.IOException)65 JsonParser (com.fasterxml.jackson.core.JsonParser)44 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 JsonFactory (com.fasterxml.jackson.core.JsonFactory)18 JsonParseException (com.fasterxml.jackson.core.JsonParseException)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 SqlNullable (com.facebook.presto.spi.function.SqlNullable)6 SqlType (com.facebook.presto.spi.function.SqlType)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 InputStream (java.io.InputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ByteString (com.google.protobuf.ByteString)4 HashSet (java.util.HashSet)4 MappingJsonFactory (com.fasterxml.jackson.databind.MappingJsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3