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;
}
}
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.");
}
}
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;
}
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);
}
}
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();
}
Aggregations