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:demo.model.ServiceLocationTests.java

@Test
public void testServiceLocationSerialization() throws JsonParseException, JsonMappingException, IOException {
    final ServiceLocation serviceLocation = new ServiceLocation(Double.valueOf(38.907773),
            Double.valueOf(-77.023735));
    serviceLocation.setId("55e521c430044aedf761fa52");
    serviceLocation.setAddress1("1317 9th St NW");
    serviceLocation.setCity("Washington");
    serviceLocation.setState("DC");
    serviceLocation.setZip("20001");
    serviceLocation.setType("Service");

    final InputStream is = ServiceLocationTests.class.getResourceAsStream("/service-location.json");
    final String serviceLocationAsString = IOUtils.toString(is);

    final ObjectMapper objectMapper = new ObjectMapper();
    final String json = objectMapper.writeValueAsString(serviceLocation);

    Assert.assertEquals(serviceLocationAsString, json);
}

From source file:io.helixservice.feature.restservice.marshal.JacksonMarshallerUnitTest.java

@Test(expected = MarshallerException.class)
public void testMarshallException() throws JsonProcessingException {
    Object marshaledObject = new Object();

    ObjectMapper objectMapper = mock(ObjectMapper.class);
    when(objectMapper.writeValueAsString(marshaledObject)).thenThrow(new NullPointerException("nope!"));

    JacksonMarshaller subject = new JacksonMarshaller(objectMapper);
    subject.marshal(marshaledObject);//  w  w w .j  av  a2  s.c  o m
}

From source file:org.springframework.cloud.sleuth.SpanTest.java

@Test
public void should_properly_serialize_object() throws JsonProcessingException {
    Span span = new Span(1, 2, "http:name", 1L, Collections.<Long>emptyList(), 2L, true, true, "process");
    ObjectMapper objectMapper = new ObjectMapper();

    String serializedName = objectMapper.writeValueAsString(span);

    then(serializedName).isNotEmpty();// w  w w.  java2s .c  o m
}

From source file:com.omricat.yacc.data.model.CurrencyCodeTest.java

@Test
public void testJsonSerialization() throws Exception {
    final ObjectMapper mapper = new ObjectMapper();

    final String json = mapper.writeValueAsString(new CurrencyCode("EUR"));

    assertThat(json).isEqualTo("\"EUR\"");
}

From source file:org.grouchotools.jsrules.util.JsonBean.java

@Override
public String toString() {
    ObjectMapper mapper = new ObjectMapper();

    String json;/* w ww.  j  ava  2s  . c  o m*/

    try {
        json = mapper.writeValueAsString(this);
    } catch (JsonProcessingException ex) {
        json = "";
    }

    return json;
}

From source file:org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent.java

public String toString() {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from w w  w .j  a  v a  2  s . com*/
        return mapper.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        log.error("Cannot serialize object.", e);
    }
    return null;
}

From source file:org.whispersystems.textsecuregcm.limits.LeakyBucket.java

public String serialize(ObjectMapper mapper) throws JsonProcessingException {
    return mapper.writeValueAsString(
            new LeakyBucketEntity(bucketSize, leakRatePerMillis, spaceRemaining, lastUpdateTimeMillis));
}

From source file:edu.cornell.mannlib.vitro.webapp.servlet.ConceptSearchServlet.java

protected String renderJson(ConceptInfo conceptInfo) {

    ObjectMapper mapper = new ObjectMapper();
    try {/*from  www. j  a  va  2s. c o  m*/
        return mapper.writeValueAsString(conceptInfo);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        log.error("An error occurred in rendering conceptInfo as json ", e);
        return null;
    }
}

From source file:io.helixservice.feature.restservice.marshal.JacksonMarshallerUnitTest.java

@Test
public void testResponseData() throws JsonProcessingException {
    Object marshaledObject = new Object();

    ObjectMapper objectMapper = mock(ObjectMapper.class);
    when(objectMapper.writeValueAsString(marshaledObject)).thenReturn("{ json body }");

    JacksonMarshaller subject = new JacksonMarshaller(objectMapper);
    Message message = subject.marshal(marshaledObject);

    assertEquals("{ json body }", new String(message.getBody()));
    assertEquals("application/json", message.getContentType());
}

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

@Test
public void testGetAuthStatusAsync() throws IOException, InterruptedException {
    final Auth auth = new Auth();

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

    setupMocks(serialized, HttpStatus.SC_OK);

    AuthApi authApi = new AuthApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    authApi.getAuthStatusAsync(new FutureCallback<Auth>() {
        @Override//w  w w .java 2  s. com
        public void onSuccess(@Nullable Auth result) {
            assertEquals(result, auth);
            latch.countDown();
        }

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