Java Code Examples for com.fasterxml.jackson.core.JsonParser#isClosed()
The following examples show how to use
com.fasterxml.jackson.core.JsonParser#isClosed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CarDeserializer.java From java-master with Apache License 2.0 | 6 votes |
@Override public Car deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException { Car car = new Car(); while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME == jsonToken) { String fieldName = parser.getCurrentName(); parser.nextToken(); if ("doors".equals(fieldName)) { car.setDoors(parser.getValueAsInt()); } else if ("brand".equals(fieldName)) { car.setBrand(parser.getValueAsString()); } } } return car; }
Example 2
Source File: JsonSteamTest.java From java-master with Apache License 2.0 | 6 votes |
@Test public void test() throws Exception { String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"; JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(carJson); //START_OBJECT //END_OBJECT //START_ARRAY //END_ARRAY //FIELD_NAME //VALUE_EMBEDDED_OBJECT //VALUE_FALSE //VALUE_TRUE //VALUE_NULL //VALUE_STRING //VALUE_NUMBER_INT //VALUE_NUMBER_FLOAT while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); System.out.println("jsonToken = " + jsonToken); } }
Example 3
Source File: JsonSteamTest.java From java-master with Apache License 2.0 | 6 votes |
@Test public void test1() throws Exception { String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"; JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(carJson); Car car = new Car(); while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { String fieldName = parser.getCurrentName(); System.out.println(fieldName); jsonToken = parser.nextToken(); if ("brand".equals(fieldName)) { car.setBrand(parser.getValueAsString()); } else if ("doors".equals(fieldName)) { car.setDoors(parser.getValueAsInt()); } } } System.out.println("car.brand = " + car); }
Example 4
Source File: UsersStreamDeserializer.java From java-json-benchmark with MIT License | 6 votes |
@Override public Users jackson(JsonParser jParser) throws IOException { Users uc = new Users(); while (jParser.nextToken() != com.fasterxml.jackson.core.JsonToken.END_OBJECT) { String fieldname = jParser.getCurrentName(); if ("users".equals(fieldname)) { if (jParser.nextToken() == com.fasterxml.jackson.core.JsonToken.START_ARRAY) { uc.users = new ArrayList<>(); while (jParser.nextToken() != com.fasterxml.jackson.core.JsonToken.END_ARRAY) { uc.users.add(jacksonUser(jParser)); if (jParser.isClosed()) { throw new IOException("parser closed"); } } } } } return uc; }
Example 5
Source File: RecordDeserialiser.java From octarine with Apache License 2.0 | 6 votes |
@Override public Record applyUnsafe(JsonParser parser) throws IOException { List<Value> values = new ArrayList<>(); if (parser.nextToken() == JsonToken.END_OBJECT) { return Record.empty(); } /* * When the parser has hit the end of input nextToken() will always return null. * So need to prevent infinite loops we check the parser closed flag. */ while (!parser.isClosed() && (parser.nextValue() != JsonToken.END_OBJECT)) { String fieldName = parser.getCurrentName(); if (JsonToken.VALUE_NULL != parser.getCurrentToken()) { Optional<Value> result = parserMapper.apply(fieldName, parser); if (result.isPresent()) { values.add(result.get()); } else { consumeUnwanted(parser); } } } return Record.of(values); }
Example 6
Source File: ListDeserialiser.java From octarine with Apache License 2.0 | 6 votes |
@Override public PVector<V> applyUnsafe(JsonParser p) throws IOException { List<V> values = new LinkedList<>(); /* * If we are in the root context (not inside an object or list) then we need to consume the next token * before attempting to call child deserialisers. */ if (p.getParsingContext().inRoot()) { if (p.nextToken() == JsonToken.END_ARRAY) { return TreePVector.empty(); } } if (JsonToken.VALUE_NULL != p.getCurrentToken()) { /* * When the parser has hit the end of input nextToken() will always return null. * So need to prevent infinite loops we check the parser closed flag. */ while (!p.isClosed() && (p.nextToken() != JsonToken.END_ARRAY)) { values.add(itemDeserialiser.apply(p)); } } return TreePVector.from(values); }
Example 7
Source File: GenesisFile.java From incubator-tuweni with Apache License 2.0 | 5 votes |
private static Map<String, String> readAlloc(JsonParser parser) throws IOException { Map<String, String> allocs = new HashMap<>(); String name = null; String value = null; boolean closed = false; while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { closed = false; String fieldName = parser.getCurrentName(); if ("balance".equals(fieldName)) { parser.nextToken(); value = parser.getValueAsString(); allocs.put(name, value); } else { name = parser.getValueAsString(); } } else if (JsonToken.END_OBJECT.equals(jsonToken)) { if (closed) { return allocs; } else { closed = true; } } } return allocs; }
Example 8
Source File: SnapshotParser.java From synapse with Apache License 2.0 | 5 votes |
public ChannelPosition parse(final File latestSnapshot, final MessageDispatcher messageDispatcher) { try ( FileInputStream fileInputStream = new FileInputStream(latestSnapshot); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream) ) { ChannelPosition channelPosition = fromHorizon(); zipInputStream.getNextEntry(); JsonParser parser = jsonFactory.createParser(zipInputStream); while (!parser.isClosed()) { JsonToken currentToken = parser.nextToken(); if (currentToken == JsonToken.FIELD_NAME) { switch (parser.getValueAsString()) { case "startSequenceNumbers": channelPosition = processSequenceNumbers(parser); break; case "data": // TODO: This expects "startSequenceNumbers" to come _before_ "data" which can/should not be guaranteed in JSON processSnapshotData( parser, /* TODO: Hier wird der StreamName als ShardName verwendet. Damit wird der Message _keine_ sinnvolle Position im Header mitgegeben! */ channelPosition, /*shardPositions.shard(channelName).position(),*/ messageDispatcher); break; default: break; } } } return channelPosition; } catch (IOException e) { throw new UncheckedIOException(e); } }
Example 9
Source File: JSONReader.java From bigquery-etl-dataflow-sample with Apache License 2.0 | 5 votes |
/** * Reads an MusicBrainzDataObject from a json string. * * @param objectName - the namespace for the object * @param json the json string * @return */ public static MusicBrainzDataObject readObject(String objectName, String json) { MusicBrainzDataObject datum = new MusicBrainzDataObject(objectName); JsonFactory factory = new JsonFactory(); try { JsonParser parser = factory.createParser(json); parser.setCodec(new ObjectMapper()); while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token != null && token.equals(JsonToken.START_OBJECT)) { JsonNode jsonTree = parser.readValueAsTree(); jsonTree.fields().forEachRemaining(entry -> { if (entry.getValue() != null) { Object value = nodeValueToObject(entry.getValue()); if (value == null) { // NOOP - unknown node type } else { datum.addColumnValue(entry.getKey(), nodeValueToObject(entry.getValue())); } } else { logger.warn("null value for entry : " + entry.getKey()); } }); } } } catch (Exception e) { logger.error("parse exception", e); } return datum; }
Example 10
Source File: GenesisFile.java From incubator-tuweni with Apache License 2.0 | 4 votes |
public static GenesisFile read(byte[] contents) throws IOException { JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(contents); int chainId = 0; String nonce = null; String difficulty = null; String mixhash = null; String coinbase = null; String timestamp = null; String extraData = null; String gasLimit = null; String parentHash = null; Map<String, String> allocs = null; List<Long> forks = new ArrayList<>(); while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { String fieldName = parser.getCurrentName(); parser.nextToken(); if ("nonce".equalsIgnoreCase(fieldName)) { nonce = parser.getValueAsString(); } else if ("difficulty".equalsIgnoreCase(fieldName)) { difficulty = parser.getValueAsString(); } else if ("mixHash".equalsIgnoreCase(fieldName)) { mixhash = parser.getValueAsString(); } else if ("coinbase".equalsIgnoreCase(fieldName)) { coinbase = parser.getValueAsString(); } else if ("gasLimit".equalsIgnoreCase(fieldName)) { gasLimit = parser.getValueAsString(); } else if ("timestamp".equalsIgnoreCase(fieldName)) { timestamp = parser.getValueAsString(); } else if ("extraData".equalsIgnoreCase(fieldName)) { extraData = parser.getValueAsString(); } else if ("parentHash".equalsIgnoreCase(fieldName)) { parentHash = parser.getValueAsString(); } else if ("alloc".equalsIgnoreCase(fieldName)) { allocs = readAlloc(parser); } else if ("chainId".equalsIgnoreCase(fieldName)) { chainId = parser.getValueAsInt(); } else if (fieldName.contains("Block")) { forks.add(parser.getValueAsLong()); } } } Collections.sort(forks); return new GenesisFile( nonce, difficulty, mixhash, coinbase, timestamp, extraData, gasLimit, parentHash, allocs, chainId, forks); }
Example 11
Source File: PrometheusQueryResponseParse.java From presto with Apache License 2.0 | 4 votes |
private boolean parse() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); JsonParser parser = new JsonFactory().createParser(response); while (!parser.isClosed()) { JsonToken jsonToken = parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { if (parser.getCurrentName().equals("status")) { parser.nextToken(); if (parser.getValueAsString().equals("success")) { this.status = true; while (!parser.isClosed()) { parser.nextToken(); if (JsonToken.FIELD_NAME.equals(jsonToken)) { if (parser.getCurrentName().equals("resultType")) { parser.nextToken(); resultType = parser.getValueAsString(); } if (parser.getCurrentName().equals("result")) { parser.nextToken(); ArrayNode node = mapper.readTree(parser); result = node.toString(); break; } } } } else { //error path String parsedStatus = parser.getValueAsString(); parser.nextToken(); parser.nextToken(); this.errorType = parser.getValueAsString(); parser.nextToken(); parser.nextToken(); error = parser.getValueAsString(); throw new PrestoException(PROMETHEUS_PARSE_ERROR, "Unable to parse Prometheus response: " + parsedStatus + " " + errorType + " " + error); } } } if (result != null) { break; } } if (result != null && resultType != null) { switch (resultType) { case "matrix": case "vector": results = mapper.readValue(result, new TypeReference<List<PrometheusMetricResult>>() {}); break; case "scalar": case "string": stringOrScalarResult = mapper.readValue(result, new TypeReference<PrometheusTimeSeriesValue>() {}); Map<String, String> madeUpMetricHeader = new HashMap<>(); madeUpMetricHeader.put("__name__", resultType); PrometheusTimeSeriesValueArray timeSeriesValues = new PrometheusTimeSeriesValueArray(Arrays.asList(stringOrScalarResult)); results = Arrays.asList(new PrometheusMetricResult(madeUpMetricHeader, timeSeriesValues)); } } return true; }
Example 12
Source File: S3SnapshotMessageStore.java From synapse with Apache License 2.0 | 4 votes |
public S3SnapshotMessageStore(final @Nonnull String channelName, final @Nonnull SnapshotReadService snapshotReadService, final @Nullable ApplicationEventPublisher eventPublisher) { this.channelName = channelName; this.eventPublisher = eventPublisher; publishEvent(STARTING, "Retrieve snapshot file from S3.", null); try { final Optional<File> latestSnapshot = snapshotReadService.retrieveLatestSnapshot(channelName); if (latestSnapshot.isPresent()) { final File snapshot = latestSnapshot.get(); this.snapshotTimestamp = SnapshotFileHelper.getSnapshotTimestamp(snapshot.getName()); publishEvent(STARTED, "Retrieve snapshot file from S3.", snapshotTimestamp); zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(snapshot))); zipInputStream.getNextEntry(); JsonFactory jsonFactory = new JsonFactory(); final JsonParser jsonParser = jsonFactory.createParser(zipInputStream); while (!jsonParser.isClosed() && messageIterator == null) { JsonToken currentToken = jsonParser.nextToken(); if (currentToken == JsonToken.FIELD_NAME) { switch (jsonParser.getValueAsString()) { case "startSequenceNumbers": channelPosition = processSequenceNumbers(jsonParser); break; case "data": // TODO: This expects "startSequenceNumbers" to come _before_ "data" messageIterator = new MessageIterator(jsonParser); break; default: break; } } } } else { LOG.info("No Snapshot available. Returning emptyMessageStore MessageStore."); } } catch (final Exception e) { try { zipInputStream.close(); } catch (final Exception e1) { /* ignore */ } publishEvent(FAILED, "Failed to load snapshot from S3: " + e.getMessage(), snapshotTimestamp); throw new RuntimeException(e); } }
Example 13
Source File: JacksonReladomoWrappedListDeserializer.java From reladomo with Apache License 2.0 | 4 votes |
@Override public SerializedList<?, ?> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { ReladomoDeserializer deserializer; if (this.valueType == null) { deserializer = new ReladomoDeserializer(); } else { Class<?> rawClass = this.valueType.getRawClass(); deserializer = createDeserializer(rawClass); } deserializer.setIgnoreUnknown(); DateParser dateParser = new DateParser(ctxt); JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE; do { JsonToken jsonToken = parser.getCurrentToken(); if (JsonToken.START_OBJECT.equals(jsonToken)) { state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT.equals(jsonToken)) { state = state.endObject(deserializer); } else if (JsonToken.START_ARRAY.equals(jsonToken)) { state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY.equals(jsonToken)) { state = state.endArray(deserializer); } else if (JsonToken.FIELD_NAME.equals(jsonToken)) { state = state.fieldName(parser.getCurrentName(), deserializer); } else if (JsonToken.VALUE_EMBEDDED_OBJECT.equals(jsonToken)) { state = state.valueEmbeddedObject(deserializer); } else if (JsonToken.VALUE_FALSE.equals(jsonToken)) { state = state.valueFalse(deserializer); } else if (JsonToken.VALUE_TRUE.equals(jsonToken)) { state = state.valueTrue(deserializer); } else if (JsonToken.VALUE_NULL.equals(jsonToken)) { state = state.valueNull(deserializer); } else if (JsonToken.VALUE_STRING.equals(jsonToken)) { state = state.valueString(parser.getValueAsString(), deserializer); } else if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) { state = state.valueNumberInt(parser.getValueAsString(), deserializer, dateParser ); } else if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)) { state = state.valueNumberFloat(parser.getValueAsString(), deserializer); } parser.nextToken(); } while (!parser.isClosed()); return deserializer.getDeserializedResultAsList(); }
Example 14
Source File: JacksonReladomoWrappedDeserializer.java From reladomo with Apache License 2.0 | 4 votes |
@Override public Serialized<?> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { ReladomoDeserializer deserializer; if (this.valueType == null) { deserializer = new ReladomoDeserializer(); } else { Class<?> rawClass = this.valueType.getRawClass(); if (MithraObject.class == rawClass || MithraTransactionalObject.class == rawClass || MithraDatedTransactionalObject.class == rawClass) { deserializer = new ReladomoDeserializer(); } else { deserializer = new ReladomoDeserializer(rawClass); } } deserializer.setIgnoreUnknown(); DateParser dateParser = new DateParser(ctxt); JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE; do { JsonToken jsonToken = parser.getCurrentToken(); if (JsonToken.START_OBJECT.equals(jsonToken)) { state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT.equals(jsonToken)) { state = state.endObject(deserializer); } else if (JsonToken.START_ARRAY.equals(jsonToken)) { state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY.equals(jsonToken)) { state = state.endArray(deserializer); } else if (JsonToken.FIELD_NAME.equals(jsonToken)) { state = state.fieldName(parser.getCurrentName(), deserializer); } else if (JsonToken.VALUE_EMBEDDED_OBJECT.equals(jsonToken)) { state = state.valueEmbeddedObject(deserializer); } else if (JsonToken.VALUE_FALSE.equals(jsonToken)) { state = state.valueFalse(deserializer); } else if (JsonToken.VALUE_TRUE.equals(jsonToken)) { state = state.valueTrue(deserializer); } else if (JsonToken.VALUE_NULL.equals(jsonToken)) { state = state.valueNull(deserializer); } else if (JsonToken.VALUE_STRING.equals(jsonToken)) { state = state.valueString(parser.getValueAsString(), deserializer); } else if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) { state = state.valueNumberInt(parser.getValueAsString(), deserializer, dateParser ); } else if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)) { state = state.valueNumberFloat(parser.getValueAsString(), deserializer); } parser.nextToken(); } while (!parser.isClosed()); return deserializer.getDeserializedResult(); }