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:org.apache.usergrid.chop.stack.StackTest.java

@Test
public void testBasicName() throws Exception {
    stack.setName("foobar");
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(stack);
    LOG.info(json);/* ww  w  .j a va  2s . c  om*/

    assertTrue(json.startsWith("{\"name\":\"foobar\",\"id\":\""));
}

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

@Test
public void testGetVmAsync() throws IOException, InterruptedException {
    final SystemStatus systemStatus = new SystemStatus();
    systemStatus.setStatus(StatusType.READY);

    ObjectMapper mapper = new ObjectMapper();
    String serialized = mapper.writeValueAsString(systemStatus);

    setupMocks(serialized, HttpStatus.SC_OK);

    SystemStatusApi systemStatusApi = new SystemStatusApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    systemStatusApi.getSystemStatusAsync(new FutureCallback<SystemStatus>() {
        @Override/*from   ww  w.j  a  va2 s  . c om*/
        public void onSuccess(@Nullable SystemStatus result) {
            assertEquals(result, systemStatus);
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            fail(t.toString());
            latch.countDown();
        }
    });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

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

@Test
public void testGetVmAsync() throws IOException, InterruptedException {
    final SystemStatus systemStatus = new SystemStatus();
    systemStatus.setStatus(StatusType.READY);

    ObjectMapper mapper = new ObjectMapper();
    String serialized = mapper.writeValueAsString(systemStatus);

    setupMocks(serialized, HttpStatus.SC_OK);

    SystemStatusApi systemStatusApi = new SystemStatusRestApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    systemStatusApi.getSystemStatusAsync(new FutureCallback<SystemStatus>() {
        @Override//from ww w .java2s .  c om
        public void onSuccess(@Nullable SystemStatus result) {
            assertEquals(result, systemStatus);
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            fail(t.toString());
            latch.countDown();
        }
    });

    assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true));
}

From source file:com.hortonworks.registries.storage.SearchApiTest.java

@Test
public void testSearchAPIJsons() throws Exception {
    LOG.info("simpleQuery = [{}]", simpleQuery);
    LOG.info("complexQuery = [{}]", complexQuery);

    SearchQuery[] queries = { simpleQuery, complexQuery };
    for (SearchQuery query : queries) {
        ObjectMapper objectMapper = new ObjectMapper();
        String queryAsJson = objectMapper.writeValueAsString(query);
        LOG.info("queryAsJson = [{}]", queryAsJson);

        SearchQuery returnedQuery = objectMapper.readValue(queryAsJson, SearchQuery.class);
        LOG.info("returnedQuery [{}] ", returnedQuery);

        Assert.assertEquals(query, returnedQuery);
    }//  ww w . j a v a  2  s. c  o  m
}

From source file:com.intel.rsa.podm.rest.filters.UrlValidationFilter.java

private String getJsonResponse(ErrorResponse error) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(error);
}

From source file:com.springboot.demo.domain.City.java

@Override
public String toJsonString() {
    final ObjectMapper jacksonObjectMapper = new ObjectMapper();
    try {//w  w  w  . j a v  a  2 s.c o  m
        return jacksonObjectMapper.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.metamx.datatypes.openrtb.BidResponseTest.java

@Test
public void testSerializationByObject() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();

    Assert.assertEquals(extJson, objectMapper.writeValueAsString(sampleExt));
    Assert.assertEquals(bidJson, objectMapper.writeValueAsString(sampleBid));
    Assert.assertEquals(seatBidJson, objectMapper.writeValueAsString(sampleSeatBid));
    Assert.assertEquals(bidResponseJson, objectMapper.writeValueAsString(sampleBidResponse));

}

From source file:net.terryyiu.emailservice.providers.AbstractEmailServiceProvider.java

@Override
public boolean send(Email email) throws IOException {
    HttpURLConnection connection = createConnection();

    // Create a Map from an email object, which can be translated to JSON which
    // the specific email service provider understands.
    Map<String, Object> map = getRequestPostData(email);

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(map);

    DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
    dos.writeBytes(json);//from  ww  w.  j  ava 2  s. c  o  m
    dos.flush();
    dos.close();

    int responseCode = connection.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + getServiceUrl());
    System.out.println("JSON: " + json);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    connection.disconnect();
    return false;
}

From source file:com.diffeo.dossier.fc.StringCounterTest.java

@Test
public void serializeToJson() throws JsonProcessingException {
    StringCounter name = new StringCounter();
    name.add("John Smith", 1);

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(name);
    assertThat(json, is(equalTo("{\"John Smith\":1}")));
}

From source file:com.shampan.db.codec.StatusCodec.java

@Override
public void encode(BsonWriter writer, StatusDAO status, EncoderContext encoderContext) {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {/*from w ww  .  j  av  a 2 s.  c o m*/
        json = mapper.writeValueAsString(status);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Document doc = new Document();
    documentCodec.encode(writer, Document.parse(json), encoderContext);
}