Search in sources :

Example 41 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 42 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 43 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project presto by prestodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.DOUBLE) double value) {
    if (!Doubles.isFinite(value)) {
        return false;
    }
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            // noinspection FloatingPointEquality
            if ((token == VALUE_NUMBER_FLOAT) && (parser.getDoubleValue() == value) && (Doubles.isFinite(parser.getDoubleValue()))) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 44 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project presto by prestodb.

the class JsonFunctions method jsonArrayGet.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.JSON)
public static Slice jsonArrayGet(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long index) {
    // this value cannot be converted to positive number
    if (index == Long.MIN_VALUE) {
        return null;
    }
    try (JsonParser parser = createJsonParser(MAPPING_JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        List<String> tokens = null;
        if (index < 0) {
            tokens = new LinkedList<>();
        }
        long count = 0;
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                if (tokens != null && count >= index * -1) {
                    return utf8Slice(tokens.get(0));
                }
                return null;
            }
            String arrayElement;
            if (token == START_OBJECT || token == START_ARRAY) {
                arrayElement = parser.readValueAsTree().toString();
            } else {
                arrayElement = parser.getValueAsString();
            }
            if (count == index) {
                return arrayElement == null ? null : utf8Slice(arrayElement);
            }
            if (tokens != null) {
                tokens.add(arrayElement);
                if (count >= index * -1) {
                    tokens.remove(0);
                }
            }
            count++;
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 45 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project presto by prestodb.

the class JsonOperators method castToVarchar.

@ScalarOperator(CAST)
@SqlNullable
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        JsonToken nextToken = parser.nextToken();
        Slice result;
        switch(nextToken) {
            case VALUE_NULL:
                result = null;
                break;
            case VALUE_STRING:
                result = Slices.utf8Slice(parser.getText());
                break;
            case VALUE_NUMBER_FLOAT:
                // Avoidance of loss of precision does not seem to be possible here because of Jackson implementation.
                result = DoubleOperators.castToVarchar(parser.getDoubleValue());
                break;
            case VALUE_NUMBER_INT:
                // An alternative is calling getLongValue and then BigintOperators.castToVarchar.
                // It doesn't work as well because it can result in overflow and underflow exceptions for large integral numbers.
                result = Slices.utf8Slice(parser.getText());
                break;
            case VALUE_TRUE:
                result = BooleanOperators.castToVarchar(true);
                break;
            case VALUE_FALSE:
                result = BooleanOperators.castToVarchar(false);
                break;
            default:
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR));
        }
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to VARCHAR");
        return result;
    } catch (IOException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR));
    }
}
Also used : Slice(io.airlift.slice.Slice) JsonToken(com.fasterxml.jackson.core.JsonToken) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

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