List of usage examples for com.fasterxml.jackson.core JsonToken END_ARRAY
JsonToken END_ARRAY
To view the source code for com.fasterxml.jackson.core JsonToken END_ARRAY.
Click Source Link
From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java
@Override public List<DrugResponse> parseDrugResponses(final InputStream inputStream) { checkNotNull(inputStream);/*from w ww. j ava 2 s .c o m*/ JsonParser parser = null; try { parser = jsonFactory.createParser(inputStream); parser.nextToken(); String id = null; String reportId = null; String description = null; String status = null; List<DrugResponse> drugResponses = new ArrayList<DrugResponse>(); while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { id = parser.getText(); } else if ("drug_responses".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { String drugResponseField = parser.getCurrentName(); parser.nextToken(); if ("report_id".equals(drugResponseField)) { reportId = parser.getText(); } else if ("description".equals(drugResponseField)) { description = parser.getText(); } else if ("status".equals(drugResponseField)) { status = parser.getText(); } } drugResponses.add(new DrugResponse(id, reportId, description, status)); reportId = null; description = null; status = null; } } } return drugResponses; } catch (IOException e) { logger.warn("could not parse drug responses", 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
/** * 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//ww w . ja va 2 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:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
protected void extractEvent(final JsonParser jsonParser, final Resource resource) throws IOException { String author = null;//from w ww . j a v a 2s . co m final JSONObject requestParams = new JSONObject(); try { jsonParser.nextToken(); JsonToken token = jsonParser.getCurrentToken(); while (token.equals(JsonToken.FIELD_NAME)) { String field = jsonParser.getCurrentName(); jsonParser.nextToken(); if (field.equals(PN_START) || field.equals(PN_END)) { final Long value = jsonParser.getValueAsLong(); final Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(value); final Date date = calendar.getTime(); final TimeZone tz = TimeZone.getTimeZone("UTC"); // this is the ISO-8601 format expected by the CalendarOperations object final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+00:00"); df.setTimeZone(tz); if (field.equals(PN_START)) { requestParams.put(CalendarRequestConstants.START_DATE, df.format(date)); } else { requestParams.put(CalendarRequestConstants.END_DATE, df.format(date)); } } else if (field.equals(CalendarRequestConstants.TAGS)) { List<String> tags = new ArrayList<String>(); if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) { token = jsonParser.nextToken(); while (!token.equals(JsonToken.END_ARRAY)) { tags.add(URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8")); token = jsonParser.nextToken(); } requestParams.put(CalendarRequestConstants.TAGS, tags); } else { LOG.warn("Tags field came in without an array of tags in it - not processed"); // do nothing, just log the error } } else if (field.equals("jcr:createdBy")) { author = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"); } else if (field.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) { jsonParser.skipChildren(); // we do nothing with these because they're going to be put into json } else { try { final String value = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"); requestParams.put(field, value); } catch (NullPointerException e) { throw e; } } token = jsonParser.nextToken(); } } catch (final JSONException e) { throw new IOException("Unable to build a JSON object with the inputs provided", e); } try { Map<String, Object> eventParams = new HashMap<String, Object>(); eventParams.put("event", requestParams.toString()); calendarOperations.create(resource, author, eventParams, null, resource.getResourceResolver().adaptTo(Session.class)); } catch (final OperationException e) { //probably caused by creating a folder that already exists. We ignore it, but still log the event. LOG.info("There was an operation exception while creating an event"); } }
From source file:com.github.heuermh.personalgenome.client.converter.JacksonPersonalGenomeConverter.java
@Override public List<Trait> parseTraits(final InputStream inputStream) { checkNotNull(inputStream);//from w w w . j ava 2s . com JsonParser parser = null; try { parser = jsonFactory.createParser(inputStream); parser.nextToken(); String id = null; String reportId = null; String description = null; String trait = null; Set<String> possibleTraits = new HashSet<String>(); List<Trait> traits = new ArrayList<Trait>(); while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { id = parser.getText(); } else if ("traits".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { String traitField = parser.getCurrentName(); parser.nextToken(); if ("report_id".equals(traitField)) { reportId = parser.getText(); } else if ("description".equals(traitField)) { description = parser.getText(); } else if ("trait".equals(traitField)) { trait = parser.getText(); } else if ("possible_traits".equals(traitField)) { while (parser.nextToken() != JsonToken.END_ARRAY) { possibleTraits.add(parser.getText()); } } } traits.add(new Trait(id, reportId, description, trait, possibleTraits)); reportId = null; description = null; trait = null; possibleTraits.clear(); } } } return traits; } catch (IOException e) { logger.warn("could not parse traits", 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
/** * Each element in the geometries array of a GeometryCollection is one of * the geometry objects described above: * * { "type": "GeometryCollection", "geometries": [ { "type": "Point", * "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ * [101.0, 0.0], [102.0, 1.0] ] } ]/* w w w. j av a 2 s. c o m*/ * * @param jp * * @throws IOException * @throws SQLException * @return GeometryCollection */ private GeometryCollection parseGeometryCollection(JsonParser jp) throws IOException, SQLException { jp.nextToken(); // FIELD_NAME geometries String coordinatesField = jp.getText(); if (coordinatesField.equalsIgnoreCase(GeoJsonField.GEOMETRIES)) { jp.nextToken();//START array jp.nextToken();//START object ArrayList<Geometry> geometries = new ArrayList<Geometry>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { geometries.add(parseGeometry(jp)); jp.nextToken(); } jp.nextToken();//END_OBJECT } geometry return GF.createGeometryCollection(geometries.toArray(new Geometry[geometries.size()])); } else { throw new SQLException( "Malformed GeoJSON file. Expected 'geometries', found '" + coordinatesField + "'"); } }
From source file:com.ntsync.shared.RawContact.java
private static List<RawImData> readImList(String rowId, List<RawImData> imAddresses, JsonParser jp) throws IOException { List<RawImData> newImAddresses = imAddresses; while (jp.nextToken() != JsonToken.END_ARRAY) { ImType type = null;/*ww w.ja v a 2 s. c o m*/ ImProtocolType proType = null; String customProctocolName = null; String imAddress = null; String imTypeLabel = null; boolean isSuperPrimary = false; boolean isPrimary = false; while (jp.nextToken() != JsonToken.END_OBJECT) { String namefield = jp.getCurrentName(); // move to value if (jp.nextToken() == null) { throw new JsonParseException("Invalid JSON-Structure. End of Object missing.", jp.getCurrentLocation()); } if (ContactConstants.DATA.equals(namefield)) { imAddress = jp.getValueAsString(); } else if (ContactConstants.TYPE.equals(namefield)) { type = ImType.fromVal(jp.getValueAsInt()); } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) { isSuperPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.PRIMARY.equals(namefield)) { isPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.LABEL.equals(namefield)) { imTypeLabel = jp.getValueAsString(); } else if (ContactConstants.PROTOCOL_TYPE.equals(namefield)) { proType = ImProtocolType.fromVal(jp.getValueAsInt()); } else if (ContactConstants.PROTOCOL_CUSTOM_PROT.equals(namefield)) { customProctocolName = jp.getValueAsString(); } else { LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + namefield); } } if (newImAddresses == null) { newImAddresses = new ArrayList<RawImData>(); } if (type == null) { type = ImType.TYPE_OTHER; } newImAddresses.add(new RawImData(imAddress, type, imTypeLabel, isPrimary, isSuperPrimary, proType, customProctocolName)); } return newImAddresses; }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses a sequence of coordinates array expressed as * * [ [100.0, 0.0], [101.0, 1.0] ]/* www . j av a2 s . c o m*/ * * @param jp * @throws IOException * @throws SQLException * @return Coordinate[] */ private Coordinate[] parseCoordinates(JsonParser jp) throws IOException { jp.nextToken(); // START_ARRAY [ to parse the each positions ArrayList<Coordinate> coords = new ArrayList<Coordinate>(); while (jp.getCurrentToken() != JsonToken.END_ARRAY) { coords.add(parseCoordinate(jp)); } return coords.toArray(new Coordinate[coords.size()]); }
From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java
/** * Parses a GeoJSON coordinate array and returns a JTS coordinate. The first * token corresponds to the first X value. The last token correponds to the * end of the coordinate array "]"./*from w ww .ja v a 2s.com*/ * * Parsed syntax: * * 100.0, 0.0] * * @param jp * @throws IOException * @return Coordinate */ private Coordinate parseCoordinate(JsonParser jp) throws IOException { jp.nextToken(); double x = jp.getDoubleValue();// VALUE_NUMBER_FLOAT jp.nextToken(); // second value double y = jp.getDoubleValue(); Coordinate coord; //We look for a z value jp.nextToken(); if (jp.getCurrentToken() == JsonToken.END_ARRAY) { coord = new Coordinate(x, y); } else { double z = jp.getDoubleValue(); jp.nextToken(); // exit array coord = new Coordinate(x, y, z); } jp.nextToken(); return coord; }
From source file:com.zenesis.qx.remote.RequestHandler.java
/** * Reads an array from JSON, where each value is of the listed in types; EG the first element * is class type[0], the second element is class type[1] etc * @param jp//from ww w . j a v a 2 s . c om * @param types * @return * @throws IOException */ private Object[] readArray(JsonParser jp, Class[] types) throws IOException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; ArrayList result = new ArrayList(); for (int paramIndex = 0; jp.nextToken() != JsonToken.END_ARRAY; paramIndex++) { Class type = null; if (types != null && paramIndex < types.length) type = types[paramIndex]; if (type != null && type.isArray()) { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) result.add(null); else if (jp.getCurrentToken() == JsonToken.START_ARRAY) { Object obj = readArray(jp, type.getComponentType()); result.add(obj); } else throw new IllegalStateException("Expected array but found " + jp.getCurrentToken()); } else if (type != null && Proxied.class.isAssignableFrom(type)) { Integer id = jp.readValueAs(Integer.class); if (id != null) { Proxied obj = getProxied(id); result.add(obj); } else result.add(null); } else if (type != null && Enum.class.isAssignableFrom(type)) { Object obj = jp.readValueAs(Object.class); if (obj != null) { String str = Helpers.camelCaseToEnum(obj.toString()); obj = Enum.valueOf(type, str); result.add(obj); } } else { Object obj = jp.readValueAs(type != null ? type : Object.class); result.add(obj); } } return result.toArray(new Object[result.size()]); }
From source file:com.ntsync.shared.RawContact.java
private static List<RawAddressData> readAddressList(String rowId, List<RawAddressData> addresses, JsonParser jp) throws IOException { List<RawAddressData> newAddresses = addresses; while (jp.nextToken() != JsonToken.END_ARRAY) { AddressType type = null;/*from www. jav a 2 s.c om*/ String label = null; String street = null; String city = null; String postcode = null; String country = null; String region = null; String pobox = null; String neighborhood = null; boolean isSuperPrimary = false; boolean isPrimary = false; while (jp.nextToken() != JsonToken.END_OBJECT) { String namefield = jp.getCurrentName(); // move to value if (jp.nextToken() == null) { throw new JsonParseException("Invalid JSON-Structure. End of Array missing.", jp.getCurrentLocation()); } if (ContactConstants.NEIGHBORHOOD.equals(namefield)) { neighborhood = jp.getValueAsString(); } else if (ContactConstants.TYPE.equals(namefield)) { type = AddressType.fromVal(jp.getValueAsInt()); } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) { isSuperPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.PRIMARY.equals(namefield)) { isPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.LABEL.equals(namefield)) { label = jp.getValueAsString(); } else if (ContactConstants.STREET.equals(namefield)) { street = jp.getValueAsString(); } else if (ContactConstants.REGION.equals(namefield)) { region = jp.getValueAsString(); } else if (ContactConstants.CITY.equals(namefield)) { city = jp.getValueAsString(); } else if (ContactConstants.POSTCODE.equals(namefield)) { postcode = jp.getValueAsString(); } else if (ContactConstants.COUNTRY.equals(namefield)) { country = jp.getValueAsString(); } else if (ContactConstants.REGION.equals(namefield)) { region = jp.getValueAsString(); } else if (ContactConstants.POBOX.equals(namefield)) { pobox = jp.getValueAsString(); } else { LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + namefield); } } if (newAddresses == null) { newAddresses = new ArrayList<RawAddressData>(); } if (type == null) { type = AddressType.TYPE_OTHER; } newAddresses.add(new RawAddressData(type, label, isPrimary, isSuperPrimary, street, pobox, neighborhood, city, region, postcode, country)); } return newAddresses; }