@Nullable private SequenceWriter openExtendedOutput() throws IOException { if (extendedOutputFile == null) { return null; } ObjectMapper mapper = new ObjectMapper(); ObjectWriter w = mapper.writer().withRootValueSeparator(System.lineSeparator()); return w.writeValues(extendedOutputFile); } }
/** * 序列化为JSON */ public static <V> void toFile(String path, List<V> list) { try (Writer writer = new FileWriter(new File(path), true)) { mapper.writer().writeValues(writer).writeAll(list); writer.flush(); } catch (Exception e) { log.error("jackson to file error, path: {}, list: {}", path, list, e); } }
/** * 序列化为JSON */ public static <V> void toFile(String path, V v) { try (Writer writer = new FileWriter(new File(path), true)) { mapper.writer().writeValues(writer).write(v); writer.flush(); } catch (Exception e) { log.error("jackson to file error, path: {}, obj: {}", path, v, e); } }
@Override public boolean pre() { ObjectMapper jsonObjectMapper = new ObjectMapper(); jsonObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); jsonObjectMapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true); try { jsonObjectWriter = jsonObjectMapper.writerFor(VariantAvro.class).writeValues(bw); } catch (IOException e) { throw Throwables.propagate(e); } return true; }
@Override public boolean pre() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true); ObjectWriter objectWriter = objectMapper.writerFor(Variant.class); try { sequenceWriter = objectWriter.writeValues(outputStream); } catch (IOException e) { throw new UncheckedIOException(e); } return true; }
/** * 序列化为Properties文件 */ public static <V> void toPropFile(String path, V v) { try (Writer writer = new FileWriter(new File(path), true)) { JavaPropsSchema schema = JavaPropsSchema.emptySchema(); propsMapper.writer(schema).writeValues(writer).write(v); writer.flush(); } catch (Exception e) { log.error("jackson to properties file error, path: {}, obj: {}", path, v, e); } }
private void persistLicenseMap() throws IOException { if (licenseMapOutputFile != null) { licenseMapOutputFile.getParentFile().mkdirs(); SequenceWriter sw = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValues(licenseMapOutputFile).init(true); for (LicensedProjects entry : licenseMap.values()) { sw.write(entry); } sw.close(); } }
/** * 序列化为CSV文件 */ public static <V> void toCsvFile(String path, String separator, List<V> list) { try (Writer writer = new FileWriter(new File(path), true)) { Class type = list.get(0).getClass(); CsvSchema schema = csvMapper.schemaFor(type).withHeader().withColumnSeparator(separator.charAt(0)); csvMapper.writer(schema).writeValues(writer).writeAll(list); writer.flush(); } catch (Exception e) { log.error("jackson to csv file error, path: {}, separator: {}, list: {}", path, separator, list, e); } }
/** * 序列化为CSV文件 */ public static <V> void toCsvFile(String path, String separator, V v) { try (Writer writer = new FileWriter(new File(path), true)) { CsvSchema schema = csvMapper.schemaFor(v.getClass()).withHeader().withColumnSeparator(separator.charAt(0)); csvMapper.writer(schema).writeValues(writer).write(v); writer.flush(); } catch (Exception e) { log.error("jackson to csv file error, path: {}, separator: {}, obj: {}", path, separator, v, e); } }
@Override public boolean open() { /** Open output stream **/ OutputStreamWriter writer; try { OutputStream outputStream = gzip ? new GZIPOutputStream(new FileOutputStream(outputPath.toFile())) : new FileOutputStream(outputPath.toFile()); writer = new OutputStreamWriter(outputStream, Charsets.UTF_8); } catch (IOException e) { throw new UncheckedIOException(e); } /** Initialize Json serializer**/ JsonFactory factory = new JsonFactory(); factory.setRootValueSeparator("\n"); ObjectMapper jsonObjectMapper = new ObjectMapper(factory); jsonObjectMapper.addMixIn(VariantAnnotation.class, VariantAnnotationMixin.class); jsonObjectMapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true); try { sequenceWriter = jsonObjectMapper.writerFor(VariantAnnotation.class).writeValues(writer); } catch (IOException e) { throw new UncheckedIOException(e); } return true; }
private void writeJsonFS(OutputStream output) throws IOException { SequenceWriter seqWriter = writer.writeValues(output); boolean first = true; output.write((byte)'['); for (FileDescriptor desc : rootFileDescriptor.getChildFiles()) { if (!first) { output.write((byte)','); } first = false; seqWriter.write(desc); } output.write((byte)']'); seqWriter.flush(); } }
private void annotationMetadata() throws VariantAnnotatorException, StorageEngineException, IOException { StorageVariantCommandOptions.AnnotationMetadataCommandOptions cliOptions = variantCommandOptions.annotationMetadataCommandOptions; QueryResult<ProjectMetadata.VariantAnnotationMetadata> result = variantStorageEngine.getAnnotationMetadata(cliOptions.annotationId); // WRITE ObjectMapper objectMapper = new ObjectMapper(); objectMapper.addMixIn(GenericRecord.class, GenericRecordAvroJsonMixin.class); objectMapper.configure(SerializationFeature.CLOSE_CLOSEABLE, false); ObjectWriter writer = objectMapper.writer(); // ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); SequenceWriter sequenceWriter = writer.writeValues(System.out); for (ProjectMetadata.VariantAnnotationMetadata metadata : result.getResult()) { sequenceWriter.write(metadata); sequenceWriter.flush(); // writer.writeValue(System.out, annotation); System.out.println(); } }
private byte[] rulesToCsv(List<?> list, Class<?> type, Class<?> mixin) throws IOException { CsvMapper csvMapper = new CsvMapper(); CsvSchema csvSchema = csvMapper.schemaFor(type).withHeader().withColumnSeparator(';'); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (SequenceWriter csvWriter = csvMapper .addMixIn(type, mixin) .writerWithDefaultPrettyPrinter() .with(csvSchema) .forType(type) .writeValues(outputStream)) { for (Object nextRow : list) { csvWriter.write(nextRow); } } return outputStream.toByteArray(); }
public void write(CsvRoute route, OutputStream target, int startIndex, int endIndex) throws IOException { List<CsvPosition> positions = route.getPositions(); CsvSchema.Builder builder = new CsvSchema.Builder(); if (positions.size() > 0) for (String key : positions.get(0).getRowAsMap().keySet()) builder = builder.addColumn(key); CsvSchema schema = builder.build().withHeader().withColumnSeparator(getColumnSeparator()); try(SequenceWriter writer = new CsvMapper().writer(schema).writeValues(target)) { for (int i = startIndex; i < endIndex; i++) { CsvPosition position = positions.get(i); writer.write(position.getRowAsMap()); } } } }
ObjectWriter writer = objectMapper.writer(); SequenceWriter sequenceWriter = writer.writeValues(System.out); for (VariantAnnotation annotation : queryResult.getResult()) { sequenceWriter.write(annotation);
@Test public final void testWriteFullCode() throws Exception { List<String> headers = Arrays.asList("TestHeader1", "TestHeader2"); List<List<String>> dataSource = Arrays.asList(); // Or alternatively, // List<List<String>> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2")); java.io.Writer writer = new StringWriter(); CsvSchema.Builder builder = CsvSchema.builder(); for (String nextHeader : headers) { builder = builder.addColumn(nextHeader); } CsvSchema schema = builder.setUseHeader(true).build(); try (SequenceWriter csvWriter = new CsvMapper().writerWithDefaultPrettyPrinter().with(schema).forType( List.class).writeValues(writer);) { for (List<String> nextRow : dataSource) { csvWriter.write(nextRow); } // Check to see whether dataSource is empty // and if so write a single empty list to trigger header output if (dataSource.isEmpty()) { csvWriter.write(Arrays.asList()); } } System.out.println(writer.toString()); }
ObjectWriter objectWriterExplicit = mapperExplicit.writer(); SequenceWriter sequenceWriterExplicit = objectWriterExplicit.writeValues(outputStream); sequenceWriterExplicit.write(testObjectIn1); sequenceWriterExplicit.flush(); ObjectWriter objectWriterImplicit = mapperImplicit.writer(); SequenceWriter sequenceWriterImplicit = objectWriterImplicit.writeValues(outputStream); sequenceWriterImplicit.write(testObjectIn2);