Search in sources :

Example 66 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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 67 with JsonFactory

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

the class ResultSet method writeJson.

/**
 * Serialize this ResultSet to the given output stream as a JSON document.
 * properties: a list of the names of all the pointSet properties for which we have histograms.
 * data: for each property, a histogram of arrival times.
 */
public void writeJson(OutputStream output, PointSet ps) {
    try {
        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jgen = jsonFactory.createGenerator(output);
        jgen.setCodec(new ObjectMapper());
        jgen.writeStartObject();
        {
            if (ps == null) {
                jgen.writeObjectFieldStart("properties");
                {
                    if (id != null)
                        jgen.writeStringField("id", id);
                }
                jgen.writeEndObject();
            } else {
                ps.writeJsonProperties(jgen);
            }
            jgen.writeObjectFieldStart("data");
            {
                for (String propertyId : histograms.keySet()) {
                    jgen.writeObjectFieldStart(propertyId);
                    {
                        histograms.get(propertyId).writeJson(jgen);
                    }
                    jgen.writeEndObject();
                }
            }
            jgen.writeEndObject();
            if (times != null) {
                jgen.writeArrayFieldStart("times");
                for (int t : times) jgen.writeNumber(t);
                jgen.writeEndArray();
            }
        }
        jgen.writeEndObject();
        jgen.close();
    } catch (IOException ioex) {
        LOG.info("IOException, connection may have been closed while streaming JSON.");
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 68 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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 69 with JsonFactory

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

the class JsonReader method read.

/**
 * Process one inputstream and send all documents to feedclient.
 * @param inputStream source of array of json document.
 * @param feedClient where data is sent.
 * @param numSent counter to be incremented for every document streamed.
 */
public static void read(InputStream inputStream, FeedClient feedClient, AtomicInteger numSent) {
    try {
        final InputStreamJsonElementBuffer jsonElementBuffer = new InputStreamJsonElementBuffer(inputStream);
        final JsonFactory jfactory = new JsonFactory().disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
        final JsonParser jParser = jfactory.createParser(jsonElementBuffer);
        while (true) {
            String docId = parseOneDocument(jParser);
            if (docId == null) {
                break;
            }
            CharSequence data = jsonElementBuffer.getJsonAsArray(jParser.getCurrentLocation().getCharOffset());
            if (data == null) {
                continue;
            }
            feedClient.stream(docId, data);
            numSent.incrementAndGet();
        }
        jsonElementBuffer.close();
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        throw new RuntimeException(ioe);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 70 with JsonFactory

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

the class MockQueryHandler method getResponse.

private String getResponse(String query) throws IOException {
    List<MockQueryHit> hits = hitMap.get(query);
    if (hits == null) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator g = new JsonFactory().createGenerator(out, JsonEncoding.UTF8);
    writeResultStart(g, hits.size());
    for (MockQueryHit hit : hits) {
        writeHit(g, hit);
    }
    writeResultsEnd(g);
    g.close();
    return out.toString();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8