List of usage examples for com.fasterxml.jackson.core JsonParser nextToken
public abstract JsonToken nextToken() throws IOException, JsonParseException;
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses a GeoJSON geometry and returns its JTS representation. * * Syntax:/*from www . jav a 2 s.com*/ * * "geometry":{"type": "Point", "coordinates": [102.0,0.5]} * * @param jsParser * @throws IOException * @return Geometry */ private Geometry parseGeometry(JsonParser jsParser) throws IOException, SQLException { jsParser.nextToken(); // FIELD_NAME type jsParser.nextToken(); //VALUE_STRING Point String geomType = jsParser.getText(); if (geomType.equalsIgnoreCase(GeoJsonField.POINT)) { return parsePoint(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTIPOINT)) { return parseMultiPoint(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.LINESTRING)) { return parseLinestring(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTILINESTRING)) { return parseMultiLinestring(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.POLYGON)) { return parsePolygon(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.MULTIPOLYGON)) { return parseMultiPolygon(jsParser); } else if (geomType.equalsIgnoreCase(GeoJsonField.GEOMETRYCOLLECTION)) { return parseGeometryCollection(jsParser); } else { throw new SQLException("Unsupported geometry : " + geomType); } }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Features in GeoJSON contain a geometry object and additional properties * * Syntax:/*from w ww . j a v a 2s . c o m*/ * * { "type": "Feature", "geometry":{"type": "Point", "coordinates": [102.0, * 0.5]}, "properties": {"prop0": "value0"} } * * @param jsParser */ private void parseFeature(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME geometry String firstField = jp.getText(); fieldIndex = 1; if (firstField.equalsIgnoreCase(GeoJsonField.GEOMETRY)) { jp.nextToken(); //START_OBJECT { getPreparedStatement().setObject(fieldIndex, parseGeometry(jp)); fieldIndex++; } else if (firstField.equalsIgnoreCase(GeoJsonField.PROPERTIES)) { parseProperties(jp, fieldIndex); } //If there is only one geometry field in the feature them the next //token corresponds to the end object of the feature jp.nextToken(); if (jp.getCurrentToken() != JsonToken.END_OBJECT) { String secondParam = jp.getText();// field name if (secondParam.equalsIgnoreCase(GeoJsonField.GEOMETRY)) { jp.nextToken(); //START_OBJECT { getPreparedStatement().setObject(fieldIndex, parseGeometry(jp)); fieldIndex++; } else if (secondParam.equalsIgnoreCase(GeoJsonField.PROPERTIES)) { parseProperties(jp, fieldIndex); } jp.nextToken(); //END_OBJECT } feature } if (!hasProperties) { getPreparedStatement().setObject(fieldIndex, featureCounter); } getPreparedStatement().execute(); }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Coordinates of a Polygon are an array of LinearRing coordinate arrays. * The first element in the array represents the exterior ring. Any * subsequent elements represent interior rings (or holes). * * Syntax:/*w w w.j a va 2s. c o m*/ * * No holes: * * { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] } * * With holes: * * { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], [ [100.2, 0.2], [100.8, 0.2], * [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] ] } * * * * @param jp * @return Polygon */ private Polygon parsePolygon(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME coordinates String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) { jp.nextToken(); // START_ARRAY [ coordinates jp.nextToken(); //Start the RING int linesIndex = 0; LinearRing linearRing = null; ArrayList<LinearRing> holes = new ArrayList<LinearRing>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { if (linesIndex == 0) { linearRing = GF.createLinearRing(parseCoordinates(jp)); } else { holes.add(GF.createLinearRing(parseCoordinates(jp))); } jp.nextToken();//END RING linesIndex++; } if (linesIndex > 1) { jp.nextToken();//END_OBJECT } geometry return GF.createPolygon(linearRing, holes.toArray(new LinearRing[holes.size()])); } else { jp.nextToken();//END_OBJECT } geometry return GF.createPolygon(linearRing, null); } } else { throw new SQLException( "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'"); } }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses the GeoJSON data and set the values to the table. * * @throws IOException// ww w. ja va2 s .c om * @throws SQLException */ private void parseData() throws IOException, SQLException { FileInputStream fis = null; try { fis = new FileInputStream(fileName); JsonParser jp = jsFactory.createParser(fis); jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) String geomType = jp.getText(); if (geomType.equalsIgnoreCase(GeoJsonField.FEATURECOLLECTION)) { parseFeatures(jp); } else { throw new SQLException( "Malformed GeoJSON file. Expected 'FeatureCollection', found '" + geomType + "'"); } jp.close(); } catch (FileNotFoundException ex) { throw new SQLException(ex); } finally { try { if (fis != null) { fis.close(); } } catch (IOException ex) { throw new SQLException(ex); } } }
From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java
@Override public List<Relative> parseRelatives(final InputStream inputStream) { checkNotNull(inputStream);// ww w.jav a2 s . c om JsonParser parser = null; try { parser = jsonFactory.createParser(inputStream); parser.nextToken(); List<Relative> relatives = new ArrayList<Relative>(); String profileId = null; String matchId = null; double similarity = 0.0d; int sharedSegments = 0; Relationship relationship = null; Relationship userRelationship = null; Set<Relationship> range = new HashSet<Relationship>(); while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { profileId = parser.getText(); } else if ("relatives".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { String relativeField = parser.getCurrentName(); parser.nextToken(); if ("match_id".equals(relativeField)) { matchId = parser.getText(); } else if ("similarity".equals(relativeField)) { similarity = Double.parseDouble(parser.getText()); } else if ("shared_segments".equals(relativeField)) { sharedSegments = parser.getIntValue(); } else if ("relationship".equals(relativeField)) { relationship = Relationship.fromDescription(parser.getText()); } else if ("user_relationship_code".equals(relativeField)) { String code = parser.getText(); userRelationship = code == "null" ? null : Relationship.fromCode(Integer.parseInt(code)); } else if ("predicted_relationship_code".equals(relativeField)) { if (relationship == null) { String code = parser.getText(); relationship = code == "null" ? null : Relationship.fromCode(Integer.parseInt(code)); } } else if ("range".equals(relativeField)) { while (parser.nextToken() != JsonToken.END_ARRAY) { range.add(Relationship.fromDescription(parser.getText())); } } // ignored nested fields else if ("family_locations".equals(relativeField)) { while (parser.nextToken() != JsonToken.END_ARRAY) { // ignore } } else if ("family_surnames".equals(relativeField)) { while (parser.nextToken() != JsonToken.END_ARRAY) { // ignore } } else if ("profile_picture_urls".equals(relativeField)) { while (parser.nextToken() != JsonToken.END_OBJECT) { // ignore } } } } relatives.add(new Relative(profileId, matchId, similarity, sharedSegments, relationship, userRelationship, range)); matchId = null; similarity = 0.0d; sharedSegments = 0; relationship = null; userRelationship = null; range.clear(); } } return relatives; } catch (IOException e) { logger.warn("could not parse relatives", e); } finally { try { inputStream.close(); } catch (Exception e) { // ignored } try { parser.close(); } catch (Exception e) { // ignored } } return null; }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses the featureCollection/*from w w w. j ava2s. c o m*/ * * @param jp * @throws IOException * @throws SQLException */ private void parseFeatures(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME features String firstParam = jp.getText(); if (firstParam.equalsIgnoreCase(GeoJsonField.CRS)) { firstParam = skipCRS(jp); } if (firstParam.equalsIgnoreCase(GeoJsonField.FEATURES)) { jp.nextToken(); // START_ARRAY [ JsonToken token = jp.nextToken(); // START_OBJECT { while (token != JsonToken.END_ARRAY) { jp.nextToken(); // FIELD_NAME type jp.nextToken(); // VALUE_STRING Feature String geomType = jp.getText(); if (geomType.equalsIgnoreCase(GeoJsonField.FEATURE)) { if (progress.isCanceled()) { throw new SQLException("Canceled by user"); } parseFeature(jp); token = jp.nextToken(); //START_OBJECT new feature featureCounter++; if (nodeCountProgress++ % readFileSizeEachNode == 0) { // Update Progress try { progress.setStep((int) (((double) fc.position() / fileSize) * 100)); } catch (IOException ex) { // Ignore } } } else { throw new SQLException("Malformed GeoJSON file. Expected 'Feature', found '" + geomType + "'"); } } //LOOP END_ARRAY ] } else { throw new SQLException("Malformed GeoJSON file. Expected 'features', found '" + firstParam + "'"); } }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Coordinates of a MultiPolygon are an array of Polygon coordinate arrays: * * { "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], * [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], * [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] } * * @param jp/*from w w w . j a va2 s . c o m*/ * @throws IOException * @throws SQLException * @return MultiPolygon */ private MultiPolygon parseMultiPolygon(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME coordinates String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.COORDINATES)) { ArrayList<Polygon> polygons = new ArrayList<Polygon>(); jp.nextToken(); // START_ARRAY [ coordinates jp.nextToken(); //Start the polygon while (jp.getCurrentToken() != JsonToken.END_ARRAY) { //Parse the polygon jp.nextToken(); //Start the RING int linesIndex = 0; LinearRing linearRing = null; ArrayList<LinearRing> holes = new ArrayList<LinearRing>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { if (linesIndex == 0) { linearRing = GF.createLinearRing(parseCoordinates(jp)); } else { holes.add(GF.createLinearRing(parseCoordinates(jp))); } jp.nextToken();//END RING linesIndex++; } if (linesIndex > 1) { jp.nextToken();//END_OBJECT polygons.add(GF.createPolygon(linearRing, holes.toArray(new LinearRing[holes.size()]))); } else { jp.nextToken();//END_OBJECT polygons.add(GF.createPolygon(linearRing, null)); } } jp.nextToken();//END_OBJECT } geometry return GF.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()])); } else { throw new SQLException( "Malformed GeoJSON file. Expected 'coordinates', found '" + coordinatesField + "'"); } }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses the properties of a feature/*from w w w . jav a2 s . c o m*/ * * Syntax: * * "properties": {"prop0": "value0"} * * @param jsParser */ private void parseProperties(JsonParser jp, int fieldIndex) throws IOException, SQLException { jp.nextToken();//START_OBJECT { while (jp.nextToken() != JsonToken.END_OBJECT) { JsonToken value = jp.nextToken(); if (value == JsonToken.VALUE_STRING) { getPreparedStatement().setObject(fieldIndex, jp.getText()); fieldIndex++; } else if (value == JsonToken.VALUE_TRUE) { getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean()); fieldIndex++; } else if (value == JsonToken.VALUE_FALSE) { getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean()); fieldIndex++; } else if (value == JsonToken.VALUE_NUMBER_FLOAT) { getPreparedStatement().setObject(fieldIndex, jp.getValueAsDouble()); fieldIndex++; } else if (value == JsonToken.VALUE_NUMBER_INT) { getPreparedStatement().setObject(fieldIndex, jp.getValueAsInt()); fieldIndex++; } else if (value == JsonToken.VALUE_NULL) { getPreparedStatement().setObject(fieldIndex, null); fieldIndex++; } else { //ignore other value } } }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Read the CRS element and return the database SRID. * /*from w ww. j av a2 s. co m*/ * Parsed syntax: * * "crs":{ * "type":"name", * "properties": * {"name":"urn:ogc:def:crs:EPSG::4326" * } * } * * @param jp * @return */ private int readCRS(JsonParser jp) throws IOException, SQLException { int srid = 0; jp.nextToken(); //START_OBJECT { jp.nextToken();// crs type jp.nextToken(); // crs name String firstField = jp.getText(); if (firstField.equalsIgnoreCase(GeoJsonField.NAME)) { jp.nextToken(); // crs properties jp.nextToken(); //START_OBJECT { jp.nextToken(); // crs name jp.nextToken(); // crs value String crsURI = jp.getText(); String[] split = crsURI.toLowerCase().split(GeoJsonField.CRS_URN_EPSG); if (split != null) { srid = Integer.valueOf(split[1]); } else { log.warn("The CRS URN " + crsURI + " is not supported."); } jp.nextToken(); //END_OBJECT } jp.nextToken(); //END_OBJECT } jp.nextToken(); //Go to features } else if (firstField.equalsIgnoreCase(GeoJsonField.LINK)) { log.warn("Linked CRS is not supported."); jp.nextToken(); jp.nextToken(); jp.nextToken(); //END_OBJECT } jp.nextToken(); //END_OBJECT } jp.nextToken(); //Go to features } else { throw new SQLException("Malformed GeoJSON CRS element."); } return srid; }
From source file:com.bazaarvoice.jsonpps.PrettyPrintJson.java
private void copyCurrentStructure(JsonParser parser, ObjectMapper mapper, int depth, JsonGenerator generator) throws IOException { // Avoid using the mapper to parse the entire input until we absolutely must. This allows pretty // printing huge top-level arrays (that wouldn't fit in memory) containing smaller objects (that // individually do fit in memory) where the objects are printed with sorted keys. JsonToken t = parser.getCurrentToken(); if (t == null) { generator.copyCurrentStructure(parser); // Will report the error of a null token. return;/*ww w . j a va 2 s .c o m*/ } int id = t.id(); if (id == ID_FIELD_NAME) { if (depth > flatten) { generator.writeFieldName(parser.getCurrentName()); } t = parser.nextToken(); id = t.id(); } switch (id) { case ID_START_OBJECT: if (sortKeys && depth >= flatten) { // Load the entire object in memory so we can sort its keys and serialize it back out. mapper.writeValue(generator, parser.readValueAs(Map.class)); } else { // Don't load the whole object into memory. Copy it in a memory-efficient streaming fashion. if (depth >= flatten) { generator.writeStartObject(); } while (parser.nextToken() != JsonToken.END_OBJECT) { copyCurrentStructure(parser, mapper, depth + 1, generator); } if (depth >= flatten) { generator.writeEndObject(); } } break; case ID_START_ARRAY: // Don't load the whole array into memory. Copy it in a memory-efficient streaming fashion. if (depth >= flatten) { generator.writeStartArray(); } while (parser.nextToken() != JsonToken.END_ARRAY) { copyCurrentStructure(parser, mapper, depth + 1, generator); } if (depth >= flatten) { generator.writeEndArray(); } break; default: generator.copyCurrentEvent(parser); break; } }