Search in sources :

Example 21 with JsonToken

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

the class ArrayContainsKudf method jsonStringArrayContains.

private boolean jsonStringArrayContains(Object searchValue, String jsonArray) {
    JsonToken valueType = getType(searchValue);
    try (JsonParser parser = JSON_FACTORY.createParser(jsonArray)) {
        if (parser.nextToken() != START_ARRAY) {
            return false;
        }
        while (parser.currentToken() != null) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return searchValue == null;
            }
            if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            if (valueType == token) {
                final Matcher matcher = matchers.get(valueType);
                if (matcher != null && matcher.matches(parser, searchValue)) {
                    return true;
                }
            }
        }
    } catch (IOException e) {
        throw new KsqlException("Invalid JSON format: " + jsonArray, e);
    }
    return false;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) KsqlException(io.confluent.ksql.util.KsqlException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 22 with JsonToken

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

the class PointSet method validateGeoJson.

/**
 * Examines a JSON stream to see if it matches the expected OTPA format.
 * TODO improve the level of detail of validation. Many files pass the validation and then crash the load function.
 *
 * @return the number of features in the collection if it's valid, or -1 if
 *         it doesn't fit the OTPA format.
 */
public static int validateGeoJson(InputStream is) {
    int n = 0;
    JsonFactory f = new JsonFactory();
    try {
        JsonParser jp = f.createParser(is);
        JsonToken current = jp.nextToken();
        if (current != JsonToken.START_OBJECT) {
            LOG.error("Root of OTPA GeoJSON should be a JSON object.");
            return -1;
        }
        // Iterate over the key:value pairs in the top-level JSON object
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String key = jp.getCurrentName();
            current = jp.nextToken();
            if (key.equals("features")) {
                if (current != JsonToken.START_ARRAY) {
                    LOG.error("Error: GeoJSON features are not in an array.");
                    return -1;
                }
                // Iterate over the features in the array
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    n += 1;
                    jp.skipChildren();
                }
            } else {
                // ignore all other keys except features
                jp.skipChildren();
            }
        }
        if (n == 0)
            // JSON has no features
            return -1;
        return n;
    } catch (Exception ex) {
        LOG.error("Exception while validating GeoJSON: {}", ex);
        return -1;
    }
}
Also used : MappingJsonFactory(com.fasterxml.jackson.databind.MappingJsonFactory) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) FactoryException(org.opengis.referencing.FactoryException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) FileNotFoundException(java.io.FileNotFoundException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 23 with JsonToken

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

the class BitSetDeserializer method deserialize.

@Override
public BitSet deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    BitSet ret = new BitSet();
    int i = 0;
    JsonToken token;
    while (!JsonToken.END_ARRAY.equals(token = jsonParser.nextValue())) {
        if (JsonToken.VALUE_TRUE.equals(token))
            ret.set(i);
        i++;
    }
    return ret;
}
Also used : BitSet(java.util.BitSet) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 24 with JsonToken

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

the class JsonBenchmark method benchmarkJacksonStreaming.

private static long benchmarkJacksonStreaming(byte[] json, int numIterations) {
    long count = 0;
    JsonFactory jsonFactory = new JsonFactory();
    try {
        for (int i = 0; i < numIterations; i++) {
            JsonParser jsonParser = jsonFactory.createParser(json);
            JsonToken array = jsonParser.nextToken();
            for (JsonToken token = jsonParser.nextToken(); !JsonToken.END_ARRAY.equals(token); token = jsonParser.nextToken()) {
                if (JsonToken.FIELD_NAME.equals(token) && "weight".equals(jsonParser.getCurrentName())) {
                    token = jsonParser.nextToken();
                    count += jsonParser.getLongValue();
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return count;
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 25 with JsonToken

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

the class JsonReader method next.

public DocumentOperation next() {
    switch(state) {
        case AT_START:
            JsonToken t = nextToken(parser);
            expectArrayStart(t);
            state = ReaderState.READING;
            break;
        case END_OF_FEED:
            return null;
        case READING:
            break;
    }
    Optional<DocumentParseInfo> documentParseInfo;
    try {
        documentParseInfo = parseDocument();
    } catch (IOException r) {
        // Jackson is not able to recover from structural parse errors
        state = END_OF_FEED;
        throw new RuntimeException(r);
    }
    if (!documentParseInfo.isPresent()) {
        state = END_OF_FEED;
        return null;
    }
    VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader();
    DocumentOperation operation = vespaJsonDocumentReader.createDocumentOperation(getDocumentTypeFromString(documentParseInfo.get().documentId.getDocType(), typeManager), documentParseInfo.get());
    operation.setCondition(TestAndSetCondition.fromConditionString(documentParseInfo.get().condition));
    return operation;
}
Also used : DocumentOperation(com.yahoo.document.DocumentOperation) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader)

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