Search in sources :

Example 26 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project moco by dreamhead.

the class TextContainerDeserializerHelper method textContainer.

public TextContainer textContainer(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
    JsonToken currentToken = jp.getCurrentToken();
    if (currentToken == JsonToken.FIELD_NAME) {
        TextContainer.Builder builder = builder();
        String operation = jp.getText().trim();
        builder.withOperation(operation);
        JsonToken token = jp.nextToken();
        if (isForTemplate(operation) && token == JsonToken.START_OBJECT) {
            return template(jp, builder);
        }
        if (token == JsonToken.VALUE_STRING) {
            String text = jp.getText().trim();
            jp.nextToken();
            return builder.withText(text).build();
        }
    }
    return (TextContainer) ctxt.handleUnexpectedToken(TextContainer.class, jp);
}
Also used : TextContainer(com.github.dreamhead.moco.parser.model.TextContainer) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 27 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project killbill by killbill.

the class PluginPropertySerializer method deserialize.

public static Iterable<PluginProperty> deserialize(final byte[] input) throws PluginPropertySerializerException {
    final List<PluginProperty> result = new ArrayList<PluginProperty>();
    try {
        final byte[] uncompressed = LZFDecoder.decode(input);
        final InputStream in = new ByteArrayInputStream(uncompressed);
        final JsonParser jsonParser = jsonFactory.createParser(in);
        PluginProperty prop = null;
        String key = null;
        JsonToken nextToken = jsonParser.nextToken();
        while (nextToken != null && nextToken != JsonToken.END_ARRAY) {
            if (nextToken != JsonToken.START_ARRAY) {
                if (nextToken == JsonToken.FIELD_NAME && key == null) {
                    key = jsonParser.getText();
                } else if (key != null) {
                    final Object value = mapper.readValue(jsonParser, Object.class);
                    prop = new PluginProperty(key, value, false);
                    key = null;
                } else if (nextToken == JsonToken.END_OBJECT) {
                    result.add(prop);
                    prop = null;
                }
            }
            nextToken = jsonParser.nextToken();
        }
        jsonParser.close();
        return result;
    } catch (final UnsupportedEncodingException e) {
        throw new PluginPropertySerializerException(e);
    } catch (final JsonParseException e) {
        throw new PluginPropertySerializerException(e);
    } catch (final IOException e) {
        throw new PluginPropertySerializerException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) PluginProperty(org.killbill.billing.payment.api.PluginProperty) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 28 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project openhab1-addons by openhab.

the class JsonWeatherParser method handleToken.

/**
     * Iterates through the JSON structure and collects weather data.
     */
private void handleToken(JsonParser jp, String property, Weather weather) throws Exception {
    JsonToken token = jp.getCurrentToken();
    String prop = PropertyResolver.add(property, jp.getCurrentName());
    if (token.isStructStart()) {
        boolean isObject = token == JsonToken.START_OBJECT || token == JsonToken.END_OBJECT;
        Weather forecast = !isObject ? weather : startIfForecast(weather, prop);
        while (!jp.nextValue().isStructEnd()) {
            handleToken(jp, prop, forecast);
        }
        if (isObject) {
            endIfForecast(weather, forecast, prop);
        }
    } else if (token != null) {
        try {
            setValue(weather, prop, jp.getValueAsString());
        } catch (Exception ex) {
            logger.error("Error setting property '{}' with value '{}' ({})", prop, jp.getValueAsString(), ex.getMessage());
        }
    }
}
Also used : Weather(org.openhab.binding.weather.internal.model.Weather) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 29 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 30 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)46 JsonParser (com.fasterxml.jackson.core.JsonParser)11 IOException (java.io.IOException)10 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 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)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