Search in sources :

Example 96 with JsonParser

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

the class JsonOperators method castToDouble.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(DOUBLE)
public static Double castToDouble(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Double result;
        switch(parser.getCurrentToken()) {
            case VALUE_NULL:
                result = null;
                break;
            case VALUE_STRING:
                result = VarcharOperators.castToDouble(Slices.utf8Slice(parser.getText()));
                break;
            case VALUE_NUMBER_FLOAT:
                result = parser.getDoubleValue();
                break;
            case VALUE_NUMBER_INT:
                // An alternative is calling getLongValue and then BigintOperators.castToDouble.
                // It doesn't work as well because it can result in overflow and underflow exceptions for large integral numbers.
                result = parser.getDoubleValue();
                break;
            case VALUE_TRUE:
                result = BooleanOperators.castToDouble(true);
                break;
            case VALUE_FALSE:
                result = BooleanOperators.castToDouble(false);
                break;
            default:
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE));
        }
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DOUBLE");
        return result;
    } catch (IOException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE));
    }
}
Also used : 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) SqlType(com.facebook.presto.spi.function.SqlType)

Example 97 with JsonParser

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

the class TestTupleDomain method testJsonSerialization.

@Test
public void testJsonSerialization() throws Exception {
    TestingTypeManager typeManager = new TestingTypeManager();
    TestingBlockEncodingSerde blockEncodingSerde = new TestingBlockEncodingSerde(typeManager);
    ObjectMapper mapper = new ObjectMapperProvider().get().registerModule(new SimpleModule().addDeserializer(ColumnHandle.class, new JsonDeserializer<ColumnHandle>() {

        @Override
        public ColumnHandle deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            return new ObjectMapperProvider().get().readValue(jsonParser, TestingColumnHandle.class);
        }
    }).addDeserializer(Type.class, new TestingTypeDeserializer(typeManager)).addSerializer(Block.class, new TestingBlockJsonSerde.Serializer(blockEncodingSerde)).addDeserializer(Block.class, new TestingBlockJsonSerde.Deserializer(blockEncodingSerde)));
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.all();
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
    tupleDomain = TupleDomain.none();
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
    tupleDomain = TupleDomain.fromFixedValues(ImmutableMap.of(A, NullableValue.of(BIGINT, 1L), B, NullableValue.asNull(VARCHAR)));
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
}
Also used : TestingColumnHandle(com.facebook.presto.spi.TestingColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TestingBlockEncodingSerde(com.facebook.presto.spi.block.TestingBlockEncodingSerde) IOException(java.io.IOException) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) TestingColumnHandle(com.facebook.presto.spi.TestingColumnHandle) Type(com.facebook.presto.spi.type.Type) TestingTypeDeserializer(com.facebook.presto.spi.type.TestingTypeDeserializer) TestingTypeDeserializer(com.facebook.presto.spi.type.TestingTypeDeserializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Block(com.facebook.presto.spi.block.Block) TestingBlockJsonSerde(com.facebook.presto.spi.block.TestingBlockJsonSerde) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TestingTypeManager(com.facebook.presto.spi.type.TestingTypeManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Example 98 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project json-android-compare by martinadamek.

the class JacksonJson method parsePublicTimeline.

public List<Map> parsePublicTimeline(InputStream inputStream) {
    List<Map> result = new ArrayList<Map>();
    try {
        Map map;
        String key;
        String key2;
        JsonParser p = sJsonFactory.createJsonParser(inputStream);
        p.nextToken();
        while (p.nextToken() != JsonToken.END_ARRAY) {
            map = new HashMap();
            while (p.nextToken() != JsonToken.END_OBJECT) {
                key = p.getCurrentName();
                // move to value, or START_OBJECT/START_ARRAY
                p.nextToken();
                if (p.getCurrentToken() == JsonToken.START_OBJECT) {
                    while (p.nextToken() != JsonToken.END_OBJECT) {
                        key2 = p.getCurrentName();
                        // move to value, or START_OBJECT/START_ARRAY
                        p.nextToken();
                        map.put("user." + key2, p.getText());
                    }
                } else {
                    map.put(key, p.getText());
                }
            }
            result.add(map);
        }
        p.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 99 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project bagheera by mozilla-metrics.

the class Validator method isValidJson.

public boolean isValidJson(String json) {
    boolean isValid = false;
    JsonParser parser = null;
    try {
        parser = jsonFactory.createJsonParser(json);
        while (parser.nextToken() != null) {
        // noop
        }
        isValid = true;
    } catch (JsonParseException ex) {
        LOG.error("JSON parse error");
    } catch (IOException e) {
        LOG.error("JSON IO error");
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException e) {
                LOG.error("Error closing JSON parser", e);
            }
        }
    }
    return isValid;
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 100 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project bagheera by mozilla-metrics.

the class JsonValidator method isValid.

@Override
public boolean isValid(byte[] data) {
    boolean isValid = false;
    JsonParser parser = null;
    try {
        parser = jsonFactory.createJsonParser(data);
        while (parser.nextToken() != null) {
        // noop
        }
        isValid = true;
    } catch (JsonParseException ex) {
        LOG.error("JSON parse error");
    } catch (IOException e) {
        LOG.error("JSON IO error");
    } catch (Exception e) {
        LOG.error("Generic error during validation: ", e);
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException e) {
                LOG.error("Error closing JSON parser", e);
            }
        }
    }
    return isValid;
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)137 IOException (java.io.IOException)37 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)24 StringWriter (java.io.StringWriter)17 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 JsonToken (com.fasterxml.jackson.core.JsonToken)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5