Java Code Examples for com.fasterxml.jackson.databind.MappingIterator#hasNextValue()

The following examples show how to use com.fasterxml.jackson.databind.MappingIterator#hasNextValue() . 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: PackageMetadataService.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
protected List<PackageMetadata> deserializeFromIndexFiles(List<File> indexFiles) {
	List<PackageMetadata> packageMetadataList = new ArrayList<>();
	YAMLMapper yamlMapper = new YAMLMapper();
	yamlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	for (File indexFile : indexFiles) {
		try {
			MappingIterator<PackageMetadata> it = yamlMapper.readerFor(PackageMetadata.class).readValues(indexFile);
			while (it.hasNextValue()) {
				PackageMetadata packageMetadata = it.next();
				packageMetadataList.add(packageMetadata);
			}
		}
		catch (IOException e) {
			throw new IllegalArgumentException("Can't parse Release manifest YAML", e);
		}
	}
	return packageMetadataList;
}
 
Example 2
Source File: SocketAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        try (final Socket socket = serverSocket.accept()) {
            if (socket != null) {
                final InputStream is = socket.getInputStream();
                while (!shutdown) {
                    final MappingIterator<LogEvent> mappingIterator = objectMapper.readerFor(Log4jLogEvent.class).readValues(is);
                    while (mappingIterator.hasNextValue()) {
                        queue.add(mappingIterator.nextValue());
                        ++count;
                    }
                }
            }
        }
    } catch (final EOFException eof) {
        // Socket is closed.
    } catch (final Exception e) {
        if (!shutdown) {
            Throwables.rethrow(e);
        }
    }
}
 
Example 3
Source File: ReadJsonBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doProcess(Record inputRecord, InputStream in) throws IOException {
  Record template = inputRecord.copy();
  removeAttachments(template);
  MappingIterator iter = reader.readValues(in);
  try {
    while (iter.hasNextValue()) {
      Object rootNode = iter.nextValue();
      incrementNumRecords();
      LOG.trace("jsonObject: {}", rootNode);
      
      Record outputRecord = template.copy();
      outputRecord.put(Fields.ATTACHMENT_BODY, rootNode);
      outputRecord.put(Fields.ATTACHMENT_MIME_TYPE, MIME_TYPE);
  
      // pass record to next command in chain:
      if (!getChild().process(outputRecord)) {
        return false;
      }
    }
    return true;
  } finally {
    iter.close();
  }
}
 
Example 4
Source File: NHANESSample.java    From synthea with Apache License 2.0 5 votes vote down vote up
/**
 * Load the NHANES samples from resources.
 * @return A list of samples.
 */
public static List<NHANESSample> loadSamples() {
  CsvMapper mapper = new CsvMapper();
  List<NHANESSample> samples = new LinkedList<NHANESSample>();
  CsvSchema schema = CsvSchema.emptySchema().withHeader();
  String filename = "nhanes_two_year_olds_bmi.csv";
  try {
    String rawCSV = Utilities.readResource(filename);
    MappingIterator<NHANESSample> it =
        mapper.readerFor(NHANESSample.class).with(schema).readValues(rawCSV);
    while (it.hasNextValue()) {
      samples.add(it.nextValue());
    }
  } catch (Exception e) {
    System.err.println("ERROR: unable to load CSV: " + filename);
    e.printStackTrace();
    throw new RuntimeException(e);
  }
  return samples;
}
 
Example 5
Source File: NodeDtos.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected static <T> List<T> toList(MappingIterator<T> iter) throws java.io.IOException {
    List<T> answer = new ArrayList<>();
    while (iter != null && iter.hasNextValue()) {
        T value = iter.nextValue();
        answer.add(value);
    }
    return answer;
}
 
Example 6
Source File: DefaultInvocationBuilder.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(DockerHttpClient.Response response) {
    try {
        InputStream body = response.getBody();
        MappingIterator<Object> iterator = objectMapper.readerFor(typeReference).readValues(body);
        while (iterator.hasNextValue()) {
            resultCallback.onNext((T) iterator.nextValue());
        }
    } catch (Exception e) {
        resultCallback.onError(e);
    }
}
 
Example 7
Source File: JsonSerialization.java    From Cardshifter with Apache License 2.0 5 votes vote down vote up
@Override
public void read(InputStream in, MessageHandler onReceived) throws CardshifterSerializationException {
       try {
           MappingIterator<Message> values;
           values = mapper.readValues(new JsonFactory().createParser(in), Message.class);
           while (values.hasNextValue()) {
               Message message = values.next();
               if (!onReceived.messageReceived(message)) {
                   return;
               }
           }
       } catch (IOException ex) {
           throw new CardshifterSerializationException(ex);
       }
}
 
Example 8
Source File: JsonTemplateLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        try (final Socket socket = serverSocket.accept()) {
            final InputStream inputStream = socket.getInputStream();
            while (!closed) {
                final MappingIterator<JsonNode> iterator = JacksonFixture
                        .getObjectMapper()
                        .readerFor(JsonNode.class)
                        .readValues(inputStream);
                while (iterator.hasNextValue()) {
                    final JsonNode value = iterator.nextValue();
                    synchronized (this) {
                        final boolean added = receivedNodes.offer(value);
                        if (!added) {
                            droppedNodeCount++;
                        }
                    }
                }
            }
        }
    } catch (final EOFException ignored) {
        // Socket is closed.
    } catch (final Exception error) {
        if (!closed) {
            throw new RuntimeException(error);
        }
    }
}
 
Example 9
Source File: JsonSerializerTest.java    From Wikidata-Toolkit with Apache License 2.0 4 votes vote down vote up
@Test
public void testSerializer() throws IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	JsonSerializer serializer = new JsonSerializer(out);

	ItemIdValue qid1 = Datamodel.makeWikidataItemIdValue("Q1");
	ItemDocument id1 = Datamodel.makeItemDocument(
			qid1,
			Collections.singletonList(Datamodel.makeMonolingualTextValue("Label1", "lang1")),
			Collections.emptyList(), Collections.emptyList(),
			Collections.singletonList(Datamodel.makeStatementGroup(Collections.singletonList(
					Datamodel.makeStatement(qid1,
							Datamodel.makeNoValueSnak(Datamodel.makeWikidataPropertyIdValue("P42")),
							Collections.emptyList(), Collections.emptyList(),
							StatementRank.NORMAL, "MyId"
			)))), Collections.emptyMap(), 1234);
	ItemDocument id2 = Datamodel.makeItemDocument(
			Datamodel.makeWikidataItemIdValue("Q2"),
			Collections.emptyList(), Collections.emptyList(), Collections.emptyList(),
			Collections.emptyList(), Collections.emptyMap(), 12);
	PropertyDocument pd1 = Datamodel.makePropertyDocument(
			Datamodel.makeWikidataPropertyIdValue("P1"),
			Collections.emptyList(), Collections.emptyList(),
			Collections.singletonList(Datamodel.makeMonolingualTextValue("Alias1", "lang1")),
			Collections.emptyList(), Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_COMMONS_MEDIA),
			3456);

	serializer.open();
	serializer.processItemDocument(id1);
	serializer.processItemDocument(id2);
	serializer.processPropertyDocument(pd1);
	serializer.close();

	List<EntityDocument> inputDocuments = Arrays.asList(id1, id2, pd1);

	List<EntityDocument> outputDocuments = new ArrayList<>();

	ObjectMapper mapper = new DatamodelMapper("http://www.wikidata.org/entity/");
	ObjectReader documentReader = mapper.readerFor(EntityDocumentImpl.class);

	MappingIterator<EntityDocument> documentIterator = documentReader.readValues(out.toString());
	while (documentIterator.hasNextValue()) {
		outputDocuments.add(documentIterator.nextValue());
	}
	documentIterator.close();

	assertEquals(inputDocuments, outputDocuments);
}