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:com.vmware.photon.controller.api.client.resource.AuthRestApiTest.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 AuthRestApi(restClient);

    final CountDownLatch latch = new CountDownLatch(1);

    authApi.getAuthStatusAsync(new FutureCallback<Auth>() {
        @Override/*  w ww .  ja  v  a  2  s.  c o m*/
        public void onSuccess(@Nullable Auth result) {
            assertEquals(result, auth);
            latch.countDown();
        }

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

From source file:local.laer.app.knapsack.support.OffsetIntMatrixTest.java

@Test
public void testSerialize() throws JsonProcessingException {
    OffsetIntMatrix matrix = new OffsetIntMatrix(1, 1, 5, 6, 0);
    matrix.mutableTransform(IntFunctions.fillWith(5));

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

    LOG.info(String.format("json: %s", json));

}

From source file:org.opentides.web.json.serializer.TagsSerializerTest.java

@Test
public void testSerialize() throws Exception {
    List<Tag> tags = new ArrayList<>();
    tags.add(new Tag("tag1"));
    tags.add(new Tag("tag2"));
    tags.add(new Tag("tag3"));

    SampleClass sample = new SampleClass();
    sample.setTags(tags);//from www.jav  a  2s .c  o m

    ObjectMapper mapper = new ObjectMapper();
    assertEquals("{\"tags\":\"tag1,tag2,tag3\"}", mapper.writeValueAsString(sample));
}

From source file:com.basistech.rosette.dm.json.plain.NonNullTest.java

@Test
public void nonNull() throws Exception {
    Bean data = new Bean();
    data.setSomething("else");

    ObjectMapper mapper = AnnotatedDataModelModule.setupObjectMapper(new ObjectMapper());
    String json = mapper.writeValueAsString(data);
    assertFalse(json.contains("nothing"));

}

From source file:org.elasticsoftware.elasticactors.base.serialization.ObjectMapperBuilderTest.java

@Test(enabled = false)
public void testAfterburnerModule() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new AfterburnerModule());

    objectMapper.writeValueAsString(new TestObjectWIthJsonSerialize(new BigDecimal("870.04")));
}

From source file:com.basistech.dm.osgitest.BundleIT.java

@Test
public void jsonWorksABit() throws Exception {
    List<Name> names = Lists.newArrayList();
    Name.Builder builder = new Name.Builder("Fred");
    names.add(builder.build());/*from w  w w .  j  a  va2 s  .c o m*/
    builder = new Name.Builder("George");
    builder.languageOfOrigin(LanguageCode.ENGLISH).script(ISO15924.Latn).languageOfUse(LanguageCode.FRENCH);
    names.add(builder.build());
    ObjectMapper mapper = objectMapper();
    String json = mapper.writeValueAsString(names);
    // one way to inspect the works is to read it back in _without_ our customized mapper.
    ObjectMapper plainMapper = new ObjectMapper();
    JsonNode tree = plainMapper.readTree(json);
    Assert.assertTrue(tree.isArray());
    Assert.assertEquals(2, tree.size());
    JsonNode node = tree.get(0);
    Assert.assertTrue(node.has("text"));
    Assert.assertEquals("Fred", node.get("text").asText());
    Assert.assertFalse(node.has("script"));
    Assert.assertFalse(node.has("languageOfOrigin"));
    Assert.assertFalse(node.has("languageOfUse"));

    List<Name> readBack = mapper.readValue(json, new TypeReference<List<Name>>() {
    });
    Assert.assertEquals(names, readBack);
}

From source file:com.shampan.db.collections.fragment.profile.City.java

public String toString() {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {/*  w w w .  java 2  s  . c  o m*/
        json = mapper.writeValueAsString(this);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return json;
}

From source file:org.apache.usergrid.chop.stack.StackTest.java

@Test
public void testBasicGeneration() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(stack);
    LOG.debug(json);/*  ww w . j  a v  a2  s  .  com*/
    assertTrue(json.startsWith("{\"name\":null,\"id\":\""));
}

From source file:io.getlime.security.powerauth.app.server.service.controller.RESTResponseExceptionResolver.java

@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception exception) {
    try {/*from ww w . j a  v a2s .co  m*/
        // Build the error list
        RESTErrorModel error = new RESTErrorModel();
        error.setCode("ERR_SPRING_JAVA");
        error.setMessage(exception.getMessage());
        error.setLocalizedMessage(exception.getLocalizedMessage());
        List<RESTErrorModel> errorList = new LinkedList<>();
        errorList.add(error);

        // Prepare the response
        RESTResponseWrapper<List<RESTErrorModel>> errorResponse = new RESTResponseWrapper<>("ERROR", errorList);

        // Write the response in JSON and send it
        ObjectMapper mapper = new ObjectMapper();
        String responseString = mapper.writeValueAsString(errorResponse);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.getOutputStream().print(responseString);
        response.flushBuffer();
    } catch (IOException e) {
        // Response object does have an output stream here
    }
    return new ModelAndView();
}

From source file:org.jenkinsci.plugins.docker.traceability.model.DockerEvent.java

/**
 * Converts the local class to the native {@link Event}.
 * @return Docker Event/*from   ww w  .  j  a v a 2s  .c  o  m*/
 * @throws IOException Conversion error
 */
public @Nonnull Event toDockerEvent() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(this);
    return mapper.readValue(json, Event.class);
}