Example usage for com.fasterxml.jackson.databind ObjectMapper valueToTree

List of usage examples for com.fasterxml.jackson.databind ObjectMapper valueToTree

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper valueToTree.

Prototype

@SuppressWarnings({ "unchecked", "resource" })
public <T extends JsonNode> T valueToTree(Object fromValue) throws IllegalArgumentException 

Source Link

Document

Reverse of #treeToValue ; given a value (usually bean), will construct equivalent JSON Tree representation.

Usage

From source file:com.linecorp.armeria.server.grpc.GrpcDocServiceTest.java

@Test
public void testOk() throws Exception {
    List<ServiceEntry> entries = ImmutableList.of(
            new ServiceEntry(TEST_SERVICE_DESCRIPTOR,
                    ImmutableList.of(new EndpointInfo("*", "/test/armeria.grpc.testing.TestService/", "",
                            GrpcSerializationFormats.PROTO.mediaType(),
                            ImmutableSet.of(GrpcSerializationFormats.PROTO.mediaType(),
                                    GrpcSerializationFormats.JSON.mediaType(),
                                    GrpcSerializationFormats.PROTO_WEB.mediaType(),
                                    GrpcSerializationFormats.JSON_WEB.mediaType(),
                                    MediaType.PROTOBUF.withParameter("protocol", "gRPC"),
                                    MediaType.JSON_UTF_8.withParameter("protocol", "gRPC"))))),
            new ServiceEntry(RECONNECT_SERVICE_DESCRIPTOR, ImmutableList.of(new EndpointInfo("*",
                    "/armeria.grpc.testing.ReconnectService/", "", GrpcSerializationFormats.PROTO,
                    ImmutableSet.of(GrpcSerializationFormats.PROTO, GrpcSerializationFormats.PROTO_WEB)))));
    final ObjectMapper mapper = new ObjectMapper();

    final JsonNode expectedJson = mapper.valueToTree(new GrpcDocServicePlugin().generate(entries));

    // The specification generated by GrpcDocServicePlugin does not include the examples specified
    // when building a DocService, so we add them manually here.
    addExamples(expectedJson);/*from   www  .  j a v  a2 s .c  o m*/

    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpGet req = new HttpGet(specificationUri());

        try (CloseableHttpResponse res = hc.execute(req)) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            final JsonNode actualJson = mapper.readTree(EntityUtils.toString(res.getEntity()));

            // The specification generated by ThriftDocServicePlugin does not include the docstrings
            // because it's injected by the DocService, so we remove them here for easier comparison.
            removeDocStrings(actualJson);

            // Convert to the prettified strings for human-readable comparison.
            final ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
            final String actualJsonString = writer.writeValueAsString(actualJson);
            final String expectedJsonString = writer.writeValueAsString(expectedJson);
            assertThat(actualJsonString).isEqualTo(expectedJsonString);
        }
    }
}

From source file:com.almende.eve.state.AbstractState.java

/**
 * Loc put.//from w w w .j a  v  a2s .c o m
 *
 * @param key the key
 * @param value the value
 * @return the serializable
 */
public synchronized Serializable locPut(final String key, final Serializable value) {
    final ObjectMapper om = JOM.getInstance();
    locPut(key, om.valueToTree(value));
    return value;
}

From source file:com.almende.eve.protocol.jsonrpc.formats.JSONResponse.java

/**
 * Instantiates a new jSON response.//  www  . ja v a  2s  .co  m
 * 
 * @param json
 *            the json
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public JSONResponse(final String json) throws IOException {
    final ObjectMapper mapper = JOM.getInstance();
    init(mapper.valueToTree(json));
}

From source file:com.almende.eve.state.AbstractState.java

/**
 * Loc put if unchanged.//from  w w w  .  j a  v  a 2  s. c o  m
 *
 * @param key the key
 * @param newVal the new val
 * @param oldVal the old val
 * @return true, if successful
 */
public boolean locPutIfUnchanged(final String key, final Serializable newVal, final Serializable oldVal) {
    final ObjectMapper om = JOM.getInstance();
    return locPutIfUnchanged(key, om.valueToTree(newVal), om.valueToTree(oldVal));
}

From source file:com.sra.biotech.submittool.persistence.service.SubmissionServiceImpl.java

public URI assignStudy(URI submissionUri, Study study) {
    ObjectMapper objectMapper = restTemplateService.getObjectMapperWithHalModule();
    ObjectNode jsonNodeStudy = (ObjectNode) objectMapper.valueToTree(study);
    jsonNodeStudy.put("submission", submissionUri.getPath());

    URI studyUri = restTemplate.postForLocation(studiesUri(), jsonNodeStudy);
    ResponseEntity<Resource<Study>> studyResponseEntity = restTemplate.exchange(studyUri, HttpMethod.GET, null,
            new ParameterizedTypeReference<Resource<Study>>() {
            });// w  ww .  j ava  2 s  .  c  om
    Resource<Study> studyResource = studyResponseEntity.getBody();
    Link submissionLinkThroughStudy = studyResource.getLink("submission");
    System.out.println("Submission Link through Study = " + submissionLinkThroughStudy);
    return studyUri;
}

From source file:com.sra.biotech.submittool.persistence.service.SubmissionServiceImpl.java

public Link assignRun(URI submissionUri, Run run) {
    ObjectMapper objectMapper = restTemplateService.getObjectMapperWithHalModule();
    String runsUri = RestClientConfiguration.BASE_URL + "/" + "runs";
    ObjectNode jsonNodeRun = (ObjectNode) objectMapper.valueToTree(run);
    jsonNodeRun.put("experiment", submissionUri.getPath());

    URI runUri = restTemplate.postForLocation(runsUri, jsonNodeRun);
    ResponseEntity<Resource<Run>> runResponseEntity = restTemplate.exchange(runUri, HttpMethod.GET, null,
            new ParameterizedTypeReference<Resource<Run>>() {
            });/*from   w w  w  .ja  va  2s.  co m*/

    Resource<Run> runResource = runResponseEntity.getBody();
    Link experimentLinkThroughRun = runResource.getLink("experiment");
    System.out.println("Experiment Link through Run = " + experimentLinkThroughRun);
    return experimentLinkThroughRun;
}

From source file:com.sra.biotech.submittool.persistence.service.SubmissionServiceImpl.java

public URI assignSample(URI submissionUri, Sample sample) {
    ObjectMapper objectMapper = restTemplateService.getObjectMapperWithHalModule();
    String samplesUri = RestClientConfiguration.BASE_URL + "/" + "samples";
    ObjectNode jsonNodeSample = (ObjectNode) objectMapper.valueToTree(sample);
    jsonNodeSample.put("submission", submissionUri.getPath());

    URI sampleUri = restTemplate.postForLocation(samplesUri, jsonNodeSample);
    ResponseEntity<Resource<Sample>> sampleResponseEntity = restTemplate.exchange(sampleUri, HttpMethod.GET,
            null, new ParameterizedTypeReference<Resource<Sample>>() {
            });//from   ww w  . j a  v a  2  s .  com

    Resource<Sample> studyResource = sampleResponseEntity.getBody();
    Link submissionLinkThroughSample = studyResource.getLink("submission");
    System.out.println("Submission Link through Sample = " + submissionLinkThroughSample);
    return sampleUri;
}

From source file:de.fhg.fokus.odp.registry.ckan.ODRClientImpl.java

public static <T> JsonNode convert(T obj) {
    ObjectMapper m = new ObjectMapper();
    m.setDateFormat(new SimpleDateFormat(JSON_DATETIME_PATTERN));

    JsonNode node = m.valueToTree(obj);

    return node;/*from   www .j a  v  a  2s . c  om*/
}

From source file:com.sra.biotech.submittool.persistence.service.SubmissionServiceImpl.java

public URI assignExperiment(URI submissionUri, Experiment experiment) {
    ObjectMapper objectMapper = restTemplateService.getObjectMapperWithHalModule();
    String experimentsUri = RestClientConfiguration.BASE_URL + "/" + "experiments";
    ObjectNode jsonNodeExperiment = (ObjectNode) objectMapper.valueToTree(experiment);
    jsonNodeExperiment.put("submission", submissionUri.getPath());

    URI experimentUri = restTemplate.postForLocation(experimentsUri, jsonNodeExperiment);
    ResponseEntity<Resource<Experiment>> experimentResponseEntity = restTemplate.exchange(experimentUri,
            HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Experiment>>() {
            });//  w ww . j a v  a 2s.co m

    Resource<Experiment> experimentResource = experimentResponseEntity.getBody();
    Link submissionLinkThroughExperiment = experimentResource.getLink("submission");
    System.out.println("Submission Link through Experiment = " + submissionLinkThroughExperiment);
    return experimentUri;
}

From source file:de.upb.wdqa.wdvd.test.JsonNormalizerTest.java

private void testOldFormatParsing(String filename) throws IOException {
    String jsonString = readFile(filename, StandardCharsets.UTF_8);

    ObjectMapper mapper = new ObjectMapper();
    OldJacksonItemDocument oldDoc = mapper.readValue(jsonString, OldJacksonItemDocument.class);

    JacksonItemDocument itemDocument = JsonNormalizer.normalizeFormat(oldDoc);

    JsonNode node = mapper.valueToTree(itemDocument);
    mapper.readValue(mapper.treeAsTokens(node), JacksonItemDocument.class);

    itemDocument.setSiteIri(Datamodel.SITE_WIKIDATA);
    itemDocument.toString(); // raises an exception if information is missing
}