Java Code Examples for com.fasterxml.jackson.databind.ObjectReader#readValues()
The following examples show how to use
com.fasterxml.jackson.databind.ObjectReader#readValues() .
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: CsvInputStreamMapper.java From pocket-etl with Apache License 2.0 | 6 votes |
/** * Converts an inputStream into an iterator of objects using Jackson CSV mapper. * @param inputStream inputStream in CSV format. * @return An iterator based on the inputStream. */ @Override public Iterator<T> apply(InputStream inputStream) { try { CsvSchema csvSchema = mapper.schemaFor(objectClass); if (columnSeparator != null) { csvSchema = csvSchema.withColumnSeparator(columnSeparator); } ObjectReader reader = mapper.readerFor(objectClass).withFeatures(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS) .with(csvSchema); return reader.readValues(inputStream); } catch (IOException e) { throw new RuntimeException(e); } }
Example 2
Source File: CliMain.java From styx with Apache License 2.0 | 4 votes |
private void workflowCreate() throws IOException, ExecutionException, InterruptedException { final String component = namespace.getString(parser.workflowCreateComponentId.getDest()); final File file = namespace.get(parser.workflowCreateFile.getDest()); final ObjectReader workflowReader = Json.YAML_MAPPER.reader() .forType(WorkflowConfiguration.class); final MappingIterator<WorkflowConfiguration> iterator; if (file == null || file.getName().equals("-")) { iterator = workflowReader.readValues(System.in); } else { iterator = workflowReader.readValues(file); } final List<WorkflowConfiguration> configurations; try { configurations = iterator.readAll(); } catch (IOException e) { throw createInputErrorException(e); } boolean invalid = false; for (WorkflowConfiguration configuration : configurations) { var workflow = Workflow.create(component, configuration); var errors = cliContext.workflowValidator().validateWorkflow(workflow); if (!errors.isEmpty()) { cliOutput.printError("Invalid workflow configuration: " + configuration.id()); errors.forEach(error -> cliOutput.printError(" error: " + error)); invalid = true; } } if (invalid) { throw CliExitException.of(ExitStatus.ArgumentError); } final List<CompletionStage<Workflow>> futures = configurations.stream() .map(configuration -> styxClient.createOrUpdateWorkflow(component, configuration)) .collect(toList()); for (CompletionStage<Workflow> future : futures) { final Workflow created = future.toCompletableFuture().get(); cliOutput.printMessage("Workflow " + created.workflowId() + " in component " + created.componentId() + " created."); } }
Example 3
Source File: DefaultCsvEventService.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Events readEvents( InputStream inputStream, boolean skipFirst ) throws IOException, ParseException { Events events = new Events(); ObjectReader reader = CSV_MAPPER.readerFor( CsvEventDataValue.class ) .with( CSV_SCHEMA.withSkipFirstDataRow( skipFirst ) ); MappingIterator<CsvEventDataValue> iterator = reader.readValues( inputStream ); Event event = new Event(); event.setEvent( "not_valid" ); while ( iterator.hasNext() ) { CsvEventDataValue dataValue = iterator.next(); if ( !event.getEvent().equals( dataValue.getEvent() ) ) { event = new Event(); event.setEvent( dataValue.getEvent() ); event.setStatus( StringUtils.isEmpty( dataValue.getStatus() ) ? EventStatus.ACTIVE : Enum.valueOf( EventStatus.class, dataValue.getStatus() ) ); event.setProgram( dataValue.getProgram() ); event.setProgramStage( dataValue.getProgramStage() ); event.setEnrollment( dataValue.getEnrollment() ); event.setOrgUnit( dataValue.getOrgUnit() ); event.setEventDate( dataValue.getEventDate() ); event.setDueDate( dataValue.getDueDate() ); event.setCompletedDate( dataValue.getCompletedDate() ); event.setCompletedBy( dataValue.getCompletedBy() ); if ( dataValue.getGeometry() != null ) { event.setGeometry( new WKTReader().read( dataValue.getGeometry() ) ); } else if ( dataValue.getLongitude() != null && dataValue.getLatitude() != null ) { event.setGeometry( new WKTReader() .read( "Point(" + dataValue.getLongitude() + " " + dataValue.getLatitude() + ")" ) ); } events.getEvents().add( event ); } DataValue value = new DataValue( dataValue.getDataElement(), dataValue.getValue() ); value.setStoredBy( dataValue.getStoredBy() ); value.setProvidedElsewhere( dataValue.getProvidedElsewhere() ); event.getDataValues().add( value ); } return events; }
Example 4
Source File: JsonSerializerTest.java From Wikidata-Toolkit with Apache License 2.0 | 4 votes |
@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); }
Example 5
Source File: JacksonCSVRecordReader.java From nifi with Apache License 2.0 | 4 votes |
public JacksonCSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader, final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding) throws IOException { super(logger, schema, hasHeader, ignoreHeader, dateFormat, timeFormat, timestampFormat); final Reader reader = new InputStreamReader(new BOMInputStream(in)); CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder() .setColumnSeparator(csvFormat.getDelimiter()) .setLineSeparator(csvFormat.getRecordSeparator()) // Can only use comments in Jackson CSV if the correct marker is set .setAllowComments("#" .equals(CharUtils.toString(csvFormat.getCommentMarker()))) // The call to setUseHeader(false) in all code paths is due to the way Jackson does data binding/mapping. Missing or extra columns may not // be handled correctly when using the header for mapping. .setUseHeader(false); csvSchemaBuilder = (csvFormat.getQuoteCharacter() == null) ? csvSchemaBuilder : csvSchemaBuilder.setQuoteChar(csvFormat.getQuoteCharacter()); csvSchemaBuilder = (csvFormat.getEscapeCharacter() == null) ? csvSchemaBuilder : csvSchemaBuilder.setEscapeChar(csvFormat.getEscapeCharacter()); if (hasHeader) { if (ignoreHeader) { csvSchemaBuilder = csvSchemaBuilder.setSkipFirstDataRow(true); } } CsvSchema csvSchema = csvSchemaBuilder.build(); // Add remaining config options to the mapper List<CsvParser.Feature> features = new ArrayList<>(); features.add(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS); if (csvFormat.getIgnoreEmptyLines()) { features.add(CsvParser.Feature.SKIP_EMPTY_LINES); } if (csvFormat.getTrim()) { features.add(CsvParser.Feature.TRIM_SPACES); } ObjectReader objReader = mapper.readerFor(String[].class) .with(csvSchema) .withFeatures(features.toArray(new CsvParser.Feature[features.size()])); recordStream = objReader.readValues(reader); }