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

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

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper 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:eu.trentorise.opendata.commons.test.jackson.OdtCommonsModuleTest.java

@Test
public void example1() throws JsonProcessingException, IOException {

    ObjectMapper om = new ObjectMapper();
    om.registerModule(new GuavaModule());
    om.registerModule(new OdtCommonsModule());

    String json = om.writeValueAsString(LocalizedString.of(Locale.ITALIAN, "ciao"));
    LocalizedString reconstructedLocalizedString = om.readValue(json, LocalizedString.class);
}

From source file:org.apache.hadoop.gateway.services.registry.impl.DefaultServiceRegistryService.java

private String renderAsJsonString(HashMap<String, HashMap<String, RegEntry>> registry) {
    String json = null;/*from   w w w . j a  v a 2  s.com*/
    ObjectMapper mapper = new ObjectMapper();

    try {
        // write JSON to a file
        json = mapper.writeValueAsString((Object) registry);

    } catch (JsonProcessingException e) {
        e.printStackTrace(); //TODO: I18N
    }
    return json;
}

From source file:com.vmware.photon.controller.api.client.resource.ResourceTicketApiTest.java

@Test
public void testGetResourceTicket() throws IOException {
    ResourceTicket resourceTicket1 = new ResourceTicket();
    resourceTicket1.setId("resourceTicket1");

    ObjectMapper mapper = new ObjectMapper();
    String serializedTask = mapper.writeValueAsString(resourceTicket1);

    setupMocks(serializedTask, HttpStatus.SC_OK);

    ResourceTicketApi resourceTicketApi = new ResourceTicketApi(restClient);

    ResourceTicket response = resourceTicketApi.getResourceTicket("foo");
    assertEquals(response, resourceTicket1);
}

From source file:com.vmware.photon.controller.api.client.resource.ResourceTicketRestApiTest.java

@Test
public void testGetResourceTicket() throws IOException {
    ResourceTicket resourceTicket1 = new ResourceTicket();
    resourceTicket1.setId("resourceTicket1");

    ObjectMapper mapper = new ObjectMapper();
    String serializedTask = mapper.writeValueAsString(resourceTicket1);

    setupMocks(serializedTask, HttpStatus.SC_OK);

    ResourceTicketApi resourceTicketApi = new ResourceTicketRestApi(restClient);

    ResourceTicket response = resourceTicketApi.getResourceTicket("foo");
    assertEquals(response, resourceTicket1);
}

From source file:com.buildria.mocking.StubSpecTest.java

@Test
public void testQueryParam() throws Exception {
    Person p = new Person("hoge", 19);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(p);

    when("/api/p").then().withStatusCode(SC_200_OK).withRawBody(json, Charset.defaultCharset())
            .withContentType("application/json; charset=UTF-8");

    given().accept(ContentType.JSON).queryParam("name", "value 1").when().get("/api/p").then().statusCode(200)
            .contentType(ContentType.JSON).body("name", is("hoge")).body("old", is(19));

    verifyWhen("/api/p").withGet().then().withAccept(containsString("application/json")).withQueryParam("name",
            "value 1");
}

From source file:cn.designthoughts.sample.axon.sfav.query.customer.controller.QueryCustomerController.java

@RequestMapping(value = "/rest/customers/{customerId}", method = RequestMethod.GET)
public String getCustomer(@PathVariable String customerId) {
    CustomerEntry customer = customerEntryRepository.findByIdentifier(customerId);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    String customerString = "";
    try {/*www.  j av  a  2s.c o m*/
        customerString = mapper.writeValueAsString(customer);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    return customerString;
}

From source file:com.buildria.mocking.StubSpecTest.java

@Test
public void testQueryParams() throws Exception {
    Person p = new Person("hoge", 19);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(p);

    when("/api/p").then().withStatusCode(SC_200_OK).withRawBody(json, Charset.defaultCharset())
            .withContentType("application/json; charset=UTF-8");

    given().accept(ContentType.JSON).queryParam("name", "value 1").queryParam("name", "value 2").when()
            .get("/api/p").then().statusCode(200).contentType(ContentType.JSON).body("name", is("hoge"))
            .body("old", is(19));

    verifyWhen("/api/p").withGet().then().withAccept(containsString("application/json"))
            .withQueryParam("name", "value 1").withQueryParam("name", "value 2");
}

From source file:com.amazonaws.dynamodb.bootstrap.AttributeValueMixInTest.java

/**
 * Test the Mixin to make sure that it capitalizes the values, and is
 * different from an ObjectMapper without the Mixin.
 *///www .j ava 2s .c om
@Test
public void testReturnsCapitalSWithMixin() throws JsonProcessingException {
    String capitalS = "S";
    String lowercaseS = "s";
    ObjectMapper mapperWith = new ObjectMapper();
    mapperWith.setSerializationInclusion(Include.NON_NULL);

    mapperWith.addMixInAnnotations(AttributeValue.class, AttributeValueMixIn.class);

    String withMixIn = mapperWith.writeValueAsString(sampleScanResult().get(0));

    ObjectMapper mapperWithout = new ObjectMapper();
    mapperWithout.setSerializationInclusion(Include.NON_NULL);

    String withoutMixIn = mapperWithout.writeValueAsString(sampleScanResult().get(0));

    assertTrue(withMixIn.contains(capitalS));
    assertTrue(withoutMixIn.contains(lowercaseS));
}

From source file:org.venice.piazza.servicecontroller.messaging.handlers.DescribeServiceHandlerTest.java

/**
 * Test that the service metadata can be retrieved.
 *//*from www  . ja va 2s . com*/
@Test
public void testSuccessDescribe() {
    DescribeServiceMetadataJob job = new DescribeServiceMetadataJob();
    String testServiceId = "a842aae2-bd74-4c4b-9a65-c45e8cd9060";
    job.serviceID = testServiceId;
    try {
        ObjectMapper mapper = new ObjectMapper();
        String responseServiceString = mapper.writeValueAsString(service);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(responseServiceString,
                HttpStatus.OK);

        final DescribeServiceHandler dsMock = Mockito.spy(dsHandler);

        Mockito.doReturn(responseEntity).when(dsMock).handle(job);
        ResponseEntity<String> result = dsMock.handle(job);

        assertEquals("The response entity was correct for this describe request", responseEntity, result);
        assertEquals("The response code is 200", responseEntity.getStatusCode(), HttpStatus.OK);
        assertEquals("The body of the response is correct", responseEntity.getBody(), responseServiceString);

    } catch (JsonProcessingException jpe) {
        jpe.printStackTrace();
    }

}