Search in sources :

Example 56 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project spring-security-oauth by spring-projects.

the class Jackson2ArrayOrStringDeserializer method deserialize.

@Override
public Set<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonToken token = jp.getCurrentToken();
    if (token.isScalarValue()) {
        String list = jp.getText();
        list = list.replaceAll("\\s+", ",");
        return new LinkedHashSet<String>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(list)));
    }
    return jp.readValueAs(new TypeReference<Set<String>>() {
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 57 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project drill by apache.

the class CountingJsonReader method write.

@Override
public ReadState write(BaseWriter.ComplexWriter writer) throws IOException {
    try {
        JsonToken token = lastSeenJsonToken;
        if (token == null || token == JsonToken.END_OBJECT) {
            token = parser.nextToken();
        }
        lastSeenJsonToken = null;
        if (!parser.hasCurrentToken()) {
            return ReadState.END_OF_STREAM;
        } else if (token != JsonToken.START_OBJECT) {
            throw new com.fasterxml.jackson.core.JsonParseException(parser, String.format("Cannot read from the middle of a record. Current token was %s ", token));
        // throw new
        // IllegalStateException(String.format("Cannot read from the middle of a record. Current token was %s",
        // token));
        }
        writer.rootAsMap().bit("count").writeBit(1);
        parser.skipChildren();
    } 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.WRITE_SUCCEED;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 58 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project logging-log4j2 by apache.

the class Log4jStackTraceElementDeserializer method deserialize.

@Override
public StackTraceElement deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();
    // Must get an Object
    if (t == JsonToken.START_OBJECT) {
        String className = null, methodName = null, fileName = null;
        int lineNumber = -1;
        while ((t = jp.nextValue()) != JsonToken.END_OBJECT) {
            final String propName = jp.getCurrentName();
            if ("class".equals(propName)) {
                className = jp.getText();
            } else if ("file".equals(propName)) {
                fileName = jp.getText();
            } else if ("line".equals(propName)) {
                if (t.isNumeric()) {
                    lineNumber = jp.getIntValue();
                } else {
                    // An XML number always comes in a string since there is no syntax help as with JSON.
                    try {
                        lineNumber = Integer.parseInt(jp.getText().trim());
                    } catch (final NumberFormatException e) {
                        throw JsonMappingException.from(jp, "Non-numeric token (" + t + ") for property 'line'", e);
                    }
                }
            } else if ("method".equals(propName)) {
                methodName = jp.getText();
            } else if ("nativeMethod".equals(propName)) {
            // no setter, not passed via constructor: ignore
            } else {
                this.handleUnknownProperty(jp, ctxt, this._valueClass, propName);
            }
        }
        return new StackTraceElement(className, methodName, fileName, lineNumber);
    }
    throw ctxt.mappingException(this._valueClass, t);
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 59 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project qi4j-sdk by Qi4j.

the class JacksonValueDeserializer method readMapInMap.

@Override
protected <K, V> Map<K, V> readMapInMap(JsonParser input, Function<JsonParser, K> keyDeserializer, Function<JsonParser, V> valueDeserializer, Map<K, V> map) throws Exception {
    JsonToken token = input.getCurrentToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        token = input.nextToken();
    }
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
    }
    JsonToken currentToken = input.nextToken();
    while (currentToken != JsonToken.END_ARRAY) {
        if (currentToken != JsonToken.START_OBJECT) {
            throw new ValueSerializationException("Expected an object start at " + input.getCurrentLocation().toString());
        }
        currentToken = input.nextToken();
        K key = null;
        V value = null;
        while (currentToken != JsonToken.END_OBJECT) {
            String objectKey = input.getCurrentName();
            input.nextToken();
            if ("key".equals(objectKey)) {
                key = keyDeserializer.map(input);
            } else if ("value".equals(objectKey)) {
                value = valueDeserializer.map(input);
            } else {
                //input.nextToken();
                input.skipChildren();
            }
            currentToken = input.nextToken();
        }
        if (key != null) {
            map.put(key, value);
        }
        currentToken = input.nextToken();
    }
    return map;
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 60 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project qi4j-sdk by Qi4j.

the class JacksonValueDeserializer method readArrayInCollection.

@Override
protected <T> Collection<T> readArrayInCollection(JsonParser input, Function<JsonParser, T> deserializer, Collection<T> collection) throws Exception {
    JsonToken token = input.getCurrentToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        token = input.nextToken();
    }
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
    }
    while (input.nextToken() != JsonToken.END_ARRAY) {
        T element = deserializer.map(input);
        collection.add(element);
    }
    return collection;
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)72 JsonParser (com.fasterxml.jackson.core.JsonParser)14 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)8 SqlNullable (com.facebook.presto.spi.function.SqlNullable)7 SqlType (com.facebook.presto.spi.function.SqlType)7 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)7 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)2 RpcHint (com.navercorp.pinpoint.web.filter.RpcHint)2 InputStream (java.io.InputStream)2 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)2 Company (com.alibaba.json.test.entity.Company)1 Department (com.alibaba.json.test.entity.Department)1 Employee (com.alibaba.json.test.entity.Employee)1 Group (com.alibaba.json.test.entity.Group)1 LayoutInstance (com.alibaba.json.test.entity.pagemodel.LayoutInstance)1 PageInstance (com.alibaba.json.test.entity.pagemodel.PageInstance)1