List of usage examples for com.fasterxml.jackson.core JsonToken END_OBJECT
JsonToken END_OBJECT
To view the source code for com.fasterxml.jackson.core JsonToken END_OBJECT.
Click Source Link
From source file:org.openhab.binding.weather.internal.parser.JsonWeatherParser.java
/** * Iterates through the JSON structure and collects weather data. *//* www . ja v a2 s .c o m*/ private void handleToken(JsonParser jp, String property, Weather weather) throws Exception { JsonToken token = jp.getCurrentToken(); String prop = PropertyResolver.add(property, jp.getCurrentName()); if (token.isStructStart()) { boolean isObject = token == JsonToken.START_OBJECT || token == JsonToken.END_OBJECT; Weather forecast = !isObject ? weather : startIfForecast(weather, prop); while (!jp.nextValue().isStructEnd()) { handleToken(jp, prop, forecast); } if (isObject) { endIfForecast(weather, forecast, prop); } } else { try { setValue(weather, prop, jp.getValueAsString()); } catch (Exception ex) { logger.error("Error setting property '{}' with value '{}' ({})", prop, jp.getValueAsString(), ex.getMessage()); } } }
From source file:org.openrdf.rio.rdfjson.RDFJSONParser.java
private void rdfJsonToHandlerInternal(final RDFHandler handler, final ValueFactory vf, final JsonParser jp) throws IOException, JsonParseException, RDFParseException, RDFHandlerException { if (jp.nextToken() != JsonToken.START_OBJECT) { reportFatalError("Expected RDF/JSON document to start with an Object", jp.getCurrentLocation()); }// www .jav a 2s. c om while (jp.nextToken() != JsonToken.END_OBJECT) { final String subjStr = jp.getCurrentName(); Resource subject = null; subject = subjStr.startsWith("_:") ? vf.createBNode(subjStr.substring(2)) : vf.createIRI(subjStr); if (jp.nextToken() != JsonToken.START_OBJECT) { reportFatalError("Expected subject value to start with an Object", jp.getCurrentLocation()); } boolean foundPredicate = false; while (jp.nextToken() != JsonToken.END_OBJECT) { final String predStr = jp.getCurrentName(); final IRI predicate = vf.createIRI(predStr); foundPredicate = true; if (jp.nextToken() != JsonToken.START_ARRAY) { reportFatalError("Expected predicate value to start with an array", jp.getCurrentLocation()); } boolean foundObject = false; while (jp.nextToken() != JsonToken.END_ARRAY) { if (jp.getCurrentToken() != JsonToken.START_OBJECT) { reportFatalError("Expected object value to start with an Object: subject=<" + subjStr + "> predicate=<" + predStr + ">", jp.getCurrentLocation()); } String nextValue = null; String nextType = null; String nextDatatype = null; String nextLanguage = null; final Set<String> nextContexts = new HashSet<String>(2); while (jp.nextToken() != JsonToken.END_OBJECT) { final String fieldName = jp.getCurrentName(); if (RDFJSONUtility.VALUE.equals(fieldName)) { if (nextValue != null) { reportError( "Multiple values found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_VALUES); } jp.nextToken(); nextValue = jp.getText(); } else if (RDFJSONUtility.TYPE.equals(fieldName)) { if (nextType != null) { reportError( "Multiple types found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_TYPES); } jp.nextToken(); nextType = jp.getText(); } else if (RDFJSONUtility.LANG.equals(fieldName)) { if (nextLanguage != null) { reportError( "Multiple languages found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_LANGUAGES); } jp.nextToken(); nextLanguage = jp.getText(); } else if (RDFJSONUtility.DATATYPE.equals(fieldName)) { if (nextDatatype != null) { reportError( "Multiple datatypes found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_DATATYPES); } jp.nextToken(); nextDatatype = jp.getText(); } else if (RDFJSONUtility.GRAPHS.equals(fieldName)) { if (jp.nextToken() != JsonToken.START_ARRAY) { reportError("Expected graphs to start with an array", jp.getCurrentLocation(), RDFJSONParserSettings.SUPPORT_GRAPHS_EXTENSION); } while (jp.nextToken() != JsonToken.END_ARRAY) { final String nextGraph = jp.getText(); nextContexts.add(nextGraph); } } else { reportError( "Unrecognised JSON field name for object: subject=" + subjStr + " predicate=" + predStr + " fieldname=" + fieldName, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_UNKNOWN_PROPERTY); } } Value object = null; if (nextType == null) { reportFatalError("No type for object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } if (nextValue == null) { reportFatalError("No value for object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } if (RDFJSONUtility.LITERAL.equals(nextType)) { if (nextLanguage != null) { object = this.createLiteral(nextValue, nextLanguage, null, jp.getCurrentLocation()); } else if (nextDatatype != null) { object = this.createLiteral(nextValue, null, this.createURI(nextDatatype), jp.getCurrentLocation()); } else { object = this.createLiteral(nextValue, null, null, jp.getCurrentLocation()); } } else if (RDFJSONUtility.BNODE.equals(nextType)) { if (nextLanguage != null) { reportFatalError("Language was attached to a blank node object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } if (nextDatatype != null) { reportFatalError("Datatype was attached to a blank node object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } object = vf.createBNode(nextValue.substring(2)); } else if (RDFJSONUtility.URI.equals(nextType)) { if (nextLanguage != null) { reportFatalError("Language was attached to a uri object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } if (nextDatatype != null) { reportFatalError("Datatype was attached to a uri object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } object = vf.createIRI(nextValue); } foundObject = true; if (!nextContexts.isEmpty()) { for (final String nextContext : nextContexts) { final Resource context = nextContext.equals(RDFJSONUtility.NULL) ? null : vf.createIRI(nextContext); Statement st = vf.createStatement(subject, predicate, object, context); if (handler != null) { handler.handleStatement(st); } } } else { Statement st = vf.createStatement(subject, predicate, object); if (handler != null) { handler.handleStatement(st); } } } if (!foundObject) { reportFatalError("No object for predicate: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation()); } } if (!foundPredicate) { reportFatalError("No predicate for object: subject=" + subjStr, jp.getCurrentLocation()); } } }
From source file:org.sead.repositories.reference.RefRepository.java
protected static void generateIndex(InputStream ro, File descFile, File indexFile) throws JsonParseException, IOException { log.debug("Generating desc and index files"); JsonFactory f = new MappingJsonFactory(); // reading JsonParser jp = f.createParser(ro);// w w w . j av a2 s . c o m JsonGenerator generator = new JsonFactory().createGenerator(descFile, JsonEncoding.UTF8); JsonToken current; current = jp.nextToken(); report(jp, current); while ((current = jp.nextToken()) != null) { if (current.equals(JsonToken.FIELD_NAME)) { String fName = jp.getText(); if (fName.equals("describes")) { log.trace("describes"); while (((current = jp.nextToken()) != null)) { if (jp.isExpectedStartObjectToken()) { generator.setCodec(new ObjectMapper()); generator.useDefaultPrettyPrinter(); generator.writeStartObject(); while (((current = jp.nextToken()) != JsonToken.END_OBJECT)) { if (current != JsonToken.FIELD_NAME) { log.warn("Unexpected Token!"); report(jp, current); } else { report(jp, current); String name = jp.getText(); current = jp.nextToken(); // Get to start of // value if (!name.equals("aggregates")) { log.trace("Writing: " + name); generator.writeFieldName(name); generator.writeTree(jp.readValueAsTree()); } else { report(jp, current); log.trace("Skipping?"); if (current.isStructStart()) { indexChildren(indexFile, jp); // jp.skipChildren(); } else { log.warn("Was Not Struct start!"); } log.trace("Hit aggregates"); } } } generator.writeEndObject(); generator.close(); } } } } } }
From source file:org.sead.repositories.reference.RefRepository.java
private static void indexChildren(File index, JsonParser jp) throws IOException { JsonGenerator generator = new JsonFactory().createGenerator(index, JsonEncoding.UTF8); generator.useDefaultPrettyPrinter(); generator.writeStartObject();//from w w w . j ava2 s .co m JsonToken cur = jp.nextToken(); while (cur.equals(JsonToken.START_OBJECT)) { long start = jp.getTokenLocation().getByteOffset(); int depth = 1; while (depth > 0) { cur = jp.nextToken(); if (cur.equals(JsonToken.START_OBJECT)) { depth++; } else if (cur.equals(JsonToken.END_OBJECT)) { depth--; } else if (cur.equals(JsonToken.FIELD_NAME) && depth == 1) { if (jp.getText().equals("@id")) { cur = jp.nextToken(); String vName = jp.getText(); generator.writeNumberField(vName, start); } else { report(jp, cur); } } } cur = jp.nextToken(); } generator.writeEndObject(); generator.close(); }
From source file:org.talend.dataprep.api.dataset.DataSetDataReader.java
/** * Reads and Maps the data set from the specified input stream. * <p><strong>Does NOT close the supplied {@link InputStream}</strong></p> * * @param inputStream the input stream containing the data set * @param joinOnColumn the column used to join the lookup data set * @return a map which associates to each value of the joint column its corresponding data set row * @throws IOException In case of JSON exception related error. * @throws IllegalArgumentException If the input stream is not of the expected JSON structure. *//*from ww w . j a va 2s .com*/ public LightweightExportableDataSet parseAndMapLookupDataSet(InputStream inputStream, String joinOnColumn) throws IOException { Validate.isTrue(inputStream != null, "The provided input stream must not be null"); try (JsonParser jsonParser = mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .getFactory().createParser(inputStream)) { LightweightExportableDataSet lookupDataset = new LightweightExportableDataSet(); RowMetadata rowMetadata = new RowMetadata(); JsonToken currentToken = jsonParser.nextToken(); Validate.isTrue(currentToken == JsonToken.START_OBJECT, INCORRECT_OBJECT_STRUCTURE_ERROR_MESSAGE); while (currentToken != JsonToken.END_OBJECT && !jsonParser.isClosed()) { currentToken = jsonParser.nextToken(); String currentField = jsonParser.getCurrentName(); if ("metadata".equalsIgnoreCase(currentField)) { JsonToken metadataStartToken = jsonParser.nextToken(); // advance to start object Validate.isTrue(metadataStartToken == JsonToken.START_OBJECT, INCORRECT_OBJECT_STRUCTURE_ERROR_MESSAGE); rowMetadata = parseDataSetMetadataAndReturnRowMetadata(jsonParser); lookupDataset.setMetadata(rowMetadata); } else if ("records".equalsIgnoreCase(currentField)) { JsonToken recordsStartToken = jsonParser.nextToken(); // advance to start object Validate.isTrue(recordsStartToken == JsonToken.START_ARRAY, INCORRECT_OBJECT_STRUCTURE_ERROR_MESSAGE); lookupDataset.setRecords(parseRecords(jsonParser, rowMetadata, joinOnColumn)); } } if (lookupDataset.isEmpty()) { throw new IOException( "No lookup data has been retrieved when trying to parse the specified data set."); } return lookupDataset; } }
From source file:org.talend.dataprep.api.dataset.json.DataSetRowIterator.java
/** * @see Iterator#next()/* w ww .j a va2 s. c o m*/ */ @Override public DataSetRow next() { try { String currentFieldName = StringUtils.EMPTY; JsonToken nextToken; row.clear(); while ((nextToken = parser.nextToken()) != JsonToken.END_OBJECT) { if (nextToken == null) { // End of input, return the current row. LOGGER.warn("Unexpected end of input for row {}.", row.values()); return row; } switch (nextToken) { // DataSetRow fields case FIELD_NAME: currentFieldName = parser.getText(); break; case VALUE_STRING: setStringValue(currentFieldName, parser.getText()); break; case VALUE_NULL: row.set(currentFieldName, ""); break; case VALUE_TRUE: case VALUE_FALSE: if ("_deleted".equals(currentFieldName)) { row.setDeleted(Boolean.parseBoolean(parser.getText())); } break; case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: if (FlagNames.TDP_ID.equals(currentFieldName)) { row.setTdpId(Long.parseLong(parser.getText())); } break; default: // let's skip this unsupported token break; } } parser.nextToken(); return row; } catch (IOException e) { throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_JSON, e); } }
From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java
private static RowMetadata parseDataSetMetadataAndReturnRowMetadata(JsonParser jsonParser) throws IOException { try {//from w ww . ja v a 2s.com RowMetadata rowMetadata = null; while (jsonParser.nextToken() != JsonToken.END_OBJECT && !jsonParser.isClosed()) { String currentField = jsonParser.getCurrentName(); if (StringUtils.equalsIgnoreCase("columns", currentField)) { rowMetadata = new RowMetadata(parseAnArrayOfColumnMetadata(jsonParser)); } } LOGGER.debug("Skipping data to go back to the outer json object"); while (jsonParser.getParsingContext().getParent().getCurrentName() != null && !jsonParser.isClosed()) { jsonParser.nextToken(); } return rowMetadata; } catch (IOException e) { throw new IOExceptionWithCause("Unable to parse and retrieve the row metadata", e); } }
From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java
/** * Reads and Maps the data set from the specified input stream. * * @param inputStream the input stream containing the data set * @param joinOnColumn the column used to join the lookup data set * @return a map which associates to each value of the joint column its corresponding data set row * @throws IOException In case of JSON exception related error. *//*from w w w . j a v a 2 s . c o m*/ public static LightweightExportableDataSet parseAndMapLookupDataSet(InputStream inputStream, String joinOnColumn) throws IOException { if (inputStream == null) { throw new IllegalArgumentException("The provided input stream must not be null"); } try (JsonParser jsonParser = mapper.getFactory().createParser(inputStream)) { LightweightExportableDataSet lookupDataset = new LightweightExportableDataSet(); RowMetadata rowMetadata = new RowMetadata(); while (jsonParser.nextToken() != JsonToken.END_OBJECT && !jsonParser.isClosed()) { String currentField = jsonParser.getCurrentName(); if (StringUtils.equalsIgnoreCase("metadata", currentField)) { rowMetadata = parseDataSetMetadataAndReturnRowMetadata(jsonParser); } currentField = jsonParser.getCurrentName(); if (StringUtils.equalsIgnoreCase("records", currentField)) { lookupDataset = parseRecords(jsonParser, rowMetadata, joinOnColumn); } } if (lookupDataset.isEmpty()) { throw new IllegalArgumentException( "No lookup data has been retrieved when trying to parse the specified data set ."); } return lookupDataset; } }