Example usage for com.fasterxml.jackson.databind ObjectWriter writeValueAsString

List of usage examples for com.fasterxml.jackson.databind ObjectWriter writeValueAsString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectWriter writeValueAsString.

Prototype

@SuppressWarnings("resource")
public String writeValueAsString(Object value) throws JsonProcessingException 

Source Link

Document

Method that can be used to serialize any Java value as a String.

Usage

From source file:org.opencb.opencga.storage.hadoop.variant.AbstractHadoopVariantStoragePipeline.java

@Override
protected Pair<Long, Long> processProto(Path input, String fileName, Path output, VariantSource source,
        Path outputVariantsFile, Path outputMetaFile, boolean includeSrc, String parser,
        boolean generateReferenceBlocks, int batchSize, String extension, String compression,
        BiConsumer<String, RuntimeException> malformatedHandler, boolean failOnError)
        throws StorageEngineException {

    //Writer/*  w w  w  .  j  a v  a 2 s .  co m*/
    DataWriter<VcfSliceProtos.VcfSlice> dataWriter = new ProtoFileWriter<>(outputVariantsFile, compression);

    // Normalizer
    VariantNormalizer normalizer = new VariantNormalizer();
    normalizer.setGenerateReferenceBlocks(generateReferenceBlocks);

    // Stats calculator
    VariantGlobalStatsCalculator statsCalculator = new VariantGlobalStatsCalculator(source);

    VariantReader dataReader = null;
    try {
        if (VariantReaderUtils.isVcf(input.toString())) {
            InputStream inputStream = FileUtils.newInputStream(input);

            VariantVcfHtsjdkReader reader = new VariantVcfHtsjdkReader(inputStream, source, normalizer);
            if (null != malformatedHandler) {
                reader.registerMalformatedVcfHandler(malformatedHandler);
                reader.setFailOnError(failOnError);
            }
            dataReader = reader;
        } else {
            dataReader = VariantReaderUtils.getVariantReader(input, source);
        }
    } catch (IOException e) {
        throw new StorageEngineException("Unable to read from " + input, e);
    }

    // Transformer
    VcfMeta meta = new VcfMeta(source);
    ArchiveHelper helper = new ArchiveHelper(conf, meta);
    ProgressLogger progressLogger = new ProgressLogger("Transform proto:").setBatchSize(100000);

    logger.info("Generating output file {}", outputVariantsFile);

    long start = System.currentTimeMillis();
    long end;
    // FIXME
    if (options.getBoolean("transform.proto.parallel")) {
        VariantSliceReader sliceReader = new VariantSliceReader(helper.getChunkSize(), dataReader);

        // Use a supplier to avoid concurrent modifications of non thread safe objects.
        Supplier<ParallelTaskRunner.TaskWithException<ImmutablePair<Long, List<Variant>>, VcfSliceProtos.VcfSlice, ?>> supplier = () -> {
            VariantToVcfSliceConverter converter = new VariantToVcfSliceConverter();
            return batch -> {
                List<VcfSliceProtos.VcfSlice> slices = new ArrayList<>(batch.size());
                for (ImmutablePair<Long, List<Variant>> pair : batch) {
                    slices.add(converter.convert(pair.getRight(), pair.getLeft().intValue()));
                    progressLogger.increment(pair.getRight().size());
                }
                return slices;
            };
        };

        ParallelTaskRunner.Config config = ParallelTaskRunner.Config.builder()
                .setNumTasks(options.getInt(Options.TRANSFORM_THREADS.key(), 1)).setBatchSize(1)
                .setAbortOnFail(true).setSorted(false).setCapacity(1).build();

        ParallelTaskRunner<ImmutablePair<Long, List<Variant>>, VcfSliceProtos.VcfSlice> ptr;
        ptr = new ParallelTaskRunner<>(sliceReader, supplier, dataWriter, config);

        try {
            ptr.run();
        } catch (ExecutionException e) {
            throw new StorageEngineException(
                    String.format("Error while Transforming file %s into %s", input, outputVariantsFile), e);
        }
        end = System.currentTimeMillis();
    } else {
        VariantHbaseTransformTask transformTask = new VariantHbaseTransformTask(helper, null);
        long[] t = new long[] { 0, 0, 0 };
        long last = System.nanoTime();

        try {
            dataReader.open();
            dataReader.pre();
            dataWriter.open();
            dataWriter.pre();
            transformTask.pre();
            statsCalculator.pre();

            start = System.currentTimeMillis();
            last = System.nanoTime();
            // Process data
            List<Variant> read = dataReader.read(batchSize);
            t[0] += System.nanoTime() - last;
            last = System.nanoTime();
            while (!read.isEmpty()) {
                progressLogger.increment(read.size());
                statsCalculator.apply(read);
                List<VcfSliceProtos.VcfSlice> slices = transformTask.apply(read);
                t[1] += System.nanoTime() - last;
                last = System.nanoTime();
                dataWriter.write(slices);
                t[2] += System.nanoTime() - last;
                last = System.nanoTime();
                read = dataReader.read(batchSize);
                t[0] += System.nanoTime() - last;
                last = System.nanoTime();
            }
            List<VcfSliceProtos.VcfSlice> drain = transformTask.drain();
            t[1] += System.nanoTime() - last;
            last = System.nanoTime();
            dataWriter.write(drain);
            t[2] += System.nanoTime() - last;

            end = System.currentTimeMillis();

            source.getMetadata().put(VariantFileUtils.VARIANT_FILE_HEADER, dataReader.getHeader());
            statsCalculator.post();
            transformTask.post();
            dataReader.post();
            dataWriter.post();

            end = System.currentTimeMillis();
            logger.info("Times for reading: {}, transforming {}, writing {}",
                    TimeUnit.NANOSECONDS.toSeconds(t[0]), TimeUnit.NANOSECONDS.toSeconds(t[1]),
                    TimeUnit.NANOSECONDS.toSeconds(t[2]));
        } catch (Exception e) {
            throw new StorageEngineException(
                    String.format("Error while Transforming file %s into %s", input, outputVariantsFile), e);
        } finally {
            dataWriter.close();
            dataReader.close();
        }
    }

    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.addMixIn(VariantSource.class, VariantSourceJsonMixin.class);
    jsonObjectMapper.addMixIn(GenericRecord.class, GenericRecordAvroJsonMixin.class);

    ObjectWriter variantSourceObjectWriter = jsonObjectMapper.writerFor(VariantSource.class);
    try {
        String sourceJsonString = variantSourceObjectWriter.writeValueAsString(source);
        StringDataWriter.write(outputMetaFile, Collections.singletonList(sourceJsonString));
    } catch (IOException e) {
        throw new StorageEngineException("Error writing meta file", e);
    }
    return new ImmutablePair<>(start, end);
}

From source file:edu.usd.btl.toolTree.OntoToTree.java

public String jsonToJava(String input) throws Exception {
    try {/* w  w  w .j  a  v a2s .c om*/
        //File input = new File("C:\\Users\\Tyler\\Documents\\GitHub\\BTL\\src\\ontology_files\\testEdam.json");

        /*
         CONVERT JSON TO JAVA OBJECTS
         */
        //map EDAM ontology JSON to List of objects
        ObjectMapper treeMapper = new ObjectMapper(); //create new Jackson Mapper
        ObjectWriter treeWriter = treeMapper.writer().withDefaultPrettyPrinter();
        List<EDAMNode> ontologyNodes = treeMapper.readValue(input,
                treeMapper.getTypeFactory().constructCollectionType(List.class, EDAMNode.class));
        System.out.println("********SIZE OF ontologyNodes*********" + ontologyNodes.size());
        for (EDAMNode node : ontologyNodes) { //for each ontology node
            System.out.println(node.getName());
            //build tree from ontology nodes

        }
        JsonNode rootNode = treeMapper.readValue(getOntoJson(), JsonNode.class);
        JsonNode tool = rootNode.get("name");

        System.out.println("\n\n\n\n****************************" + tool.toString());
        System.out.println("**** ONTOLOGY SIZE *****" + ontologyNodes.size());
        String jsonOutput = treeWriter.writeValueAsString(ontologyNodes.get(0));
        System.out.println("\n\n****ONTOLOGY JSON OUPUT*****+\n" + jsonOutput);

        //            IplantV1 iplantOutput = BETSConverter.toIplant(betsTool);
        //            String iplantOutputJson = iplantWriter.writeValueAsString(iplantOutput); //write Json as String
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return "jsonToJava success";
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Search item by dto/* w w  w  . j av  a2s. c  o m*/
 * 
 * @param searchDTO
 * @return
 * @throws IOException
 */
public Response items(SearchRestDTO searchDTO) throws IOException {
    this.webTarget = this.client.target(baseURI + pathIR).path("items/search");

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String jsonSearchDTO = ow.writeValueAsString(searchDTO);

    Response response = this.webTarget.request(MediaType.APPLICATION_JSON)
            .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
            .post(Entity.entity(jsonSearchDTO, MediaType.APPLICATION_JSON));
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }
    return response;
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Search item by dto/*from  ww  w. j a  va 2 s.  c o m*/
 * 
 * @param searchDTO
 * @return
 * @throws IOException
 */
public Response createItem(ItemRestWriteDTO itemDTO, MultivaluedMap<String, Object> headers)
        throws IOException {
    this.webTarget = this.client.target(baseURI + pathIR).path("items");

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String jsonItemDTO = ow.writeValueAsString(itemDTO);

    Response response = this.webTarget.request(MediaType.APPLICATION_JSON).headers(headers)
            .post(Entity.entity(jsonItemDTO, MediaType.APPLICATION_JSON));

    if (response.getStatus() != 201) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    return response;
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Get list item id by dto//from   ww  w . j  ava2s. c om
 * 
 * @param searchDTO
 * @return
 * @throws IOException
 */
public Response itemIds(SearchIdsRestDTO searchDTO) throws IOException {
    this.webTarget = this.client.target(baseURI + pathIR).path("items/ids/search");

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String jsonSearchDTO = ow.writeValueAsString(searchDTO);

    Response response = this.webTarget.request(MediaType.APPLICATION_JSON)
            .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
            .post(Entity.entity(jsonSearchDTO, MediaType.APPLICATION_JSON));
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }
    return response;
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Get ance journal by dto//from   www  .  ja  v a 2  s. c om
 * 
 * @param searchDTO
 * @return
 * @throws IOException
 */
public Response journals(AnceSearchRestDTO searchDTO) throws IOException {
    this.webTarget = this.client.target(baseURI + pathIR).path("ance/search");

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String jsonSearchDTO = ow.writeValueAsString(searchDTO);

    Response response = this.webTarget.request(MediaType.APPLICATION_JSON)
            .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
            .post(Entity.entity(jsonSearchDTO, MediaType.APPLICATION_JSON));
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }
    return response;
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Upload attachment with licence metadata for item
 * //from  w  w w  .  ja  v a  2s  .  c o  m
 * @param itemId
 * @param optionBitStreamDTO
 * @param fileName
 * @return
 * @throws IOException
 */
public Response uploadStream(String handle, BitstreamOptionsDTO bitstreamOptionsDTO, InputStream inputstream,
        String attachFileName) throws IOException {
    WebTarget webTarget = client.target(baseURI + pathIR).path("streams/items/" + handle);

    //Compose multipart request (multipart/form-data)
    //See http://stackoverflow.com/questions/27609569/file-upload-along-with-other-object-in-jersey-restful-web-service/27614403#27614403
    //See https://jersey.java.net/documentation/latest/media.html#multipart

    MultiPart multipartEntity = null;
    try {
        StreamDataBodyPart fileDataBodyPart = new StreamDataBodyPart("file", inputstream);

        fileDataBodyPart.setContentDisposition(
                FormDataContentDisposition.name("file").fileName(attachFileName).build());

        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String jsonBitstreamOptionsDTO = ow.writeValueAsString(bitstreamOptionsDTO);

        multipartEntity = new FormDataMultiPart()
                .field("bitstreamOptionsDTO", jsonBitstreamOptionsDTO, MediaType.APPLICATION_JSON_TYPE)
                .bodyPart(fileDataBodyPart);

        Response response = webTarget.request(MediaType.APPLICATION_JSON)
                .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
                .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA_TYPE));

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        return response;
    } finally {
        if (multipartEntity != null) {
            multipartEntity.close();
        }
    }
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Upload attachment with licence metadata for item
 * /* w ww. j  a  v  a  2 s .c om*/
 * @param itemId
 * @param optionBitStreamDTO
 * @param fileName
 * @return
 * @throws IOException
 */
public Response uploadStream(Integer itemId, BitstreamOptionsDTO bitstreamOptionsDTO, String fileName)
        throws IOException {
    WebTarget webTarget = client.target(baseURI + pathIR).path("streams/items/" + itemId);

    //Compose multipart request (multipart/form-data)
    //See http://stackoverflow.com/questions/27609569/file-upload-along-with-other-object-in-jersey-restful-web-service/27614403#27614403
    //See https://jersey.java.net/documentation/latest/media.html#multipart

    MultiPart multipartEntity = null;
    try {
        FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File(fileName),
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        fileDataBodyPart
                .setContentDisposition(FormDataContentDisposition.name("file").fileName(fileName).build());

        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String jsonBitstreamOptionsDTO = ow.writeValueAsString(bitstreamOptionsDTO);

        multipartEntity = new FormDataMultiPart()
                .field("bitstreamOptionsDTO", jsonBitstreamOptionsDTO, MediaType.APPLICATION_JSON_TYPE)
                .bodyPart(fileDataBodyPart);

        Response response = webTarget.request(MediaType.APPLICATION_JSON)
                .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
                .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA_TYPE));

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        response.getHeaderString("location");

        return response;
    } finally {
        if (multipartEntity != null) {
            multipartEntity.close();
        }
    }
}

From source file:it.cineca.iris.restclient.main.RESTIRClient.java

/**
 * Upload attachment with licence metadata for item 
 * //ww w  . j a va 2  s .  co m
 * @param itemId
 * @param bitstreamOptionsDTO
 * @param inputstream
 * @param fileName
 * @return
 * @throws IOException
 */
public Response uploadStream(Integer itemId, BitstreamOptionsDTO bitstreamOptionsDTO, InputStream inputstream,
        String attachFileName) throws IOException {
    WebTarget webTarget = client.target(baseURI + pathIR).path("streams/items/" + itemId);

    //Compose multipart request (multipart/form-data)
    //See http://stackoverflow.com/questions/27609569/file-upload-along-with-other-object-in-jersey-restful-web-service/27614403#27614403
    //See https://jersey.java.net/documentation/latest/media.html#multipart

    MultiPart multipartEntity = null;
    try {
        StreamDataBodyPart fileDataBodyPart = new StreamDataBodyPart("file", inputstream);

        fileDataBodyPart.setContentDisposition(
                FormDataContentDisposition.name("file").fileName(attachFileName).build());

        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String jsonBitstreamOptionsDTO = ow.writeValueAsString(bitstreamOptionsDTO);

        multipartEntity = new FormDataMultiPart()
                .field("bitstreamOptionsDTO", jsonBitstreamOptionsDTO, MediaType.APPLICATION_JSON_TYPE)
                .bodyPart(fileDataBodyPart);

        Response response = webTarget.request(MediaType.APPLICATION_JSON)
                .header(HeaderScopeEnum.getHeaderTag(), HeaderScopeEnum.ROLE_ADMIN.getHeaderValue())
                .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA_TYPE));

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        response.getHeaderString("location");

        return response;
    } finally {
        if (multipartEntity != null) {
            multipartEntity.close();
        }
    }
}

From source file:org.opencb.cellbase.app.cli.DownloadCommandExecutor.java

private void writeVersionDataFile(Map versionData, Path outputFilePath) {
    try {//from w w  w.  j  av  a 2 s .co m
        OutputStream os = Files.newOutputStream(outputFilePath);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        ObjectMapper jsonObjectMapper = new ObjectMapper();
        ObjectWriter jsonObjectWriter = jsonObjectMapper.writer();
        bw.write(jsonObjectWriter.writeValueAsString(versionData) + "\n");
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}