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.samlikescode.stackoverflow.questions.q30999663.DataSerializerTest.java

@Test
public void testDataSerialization() throws JsonProcessingException {
    ObjectMapper om = new ObjectMapper();

    Data d = new Data();
    d.x = 9878989l;/*w w w  .ja v a2 s  . co m*/
    d.y = 7887676l;

    String output = om.writeValueAsString(d);

    System.out.println(output);
}

From source file:de.unirostock.sems.cbarchive.web.dataholder.UserData.java

@JsonIgnore
public String toJson() throws JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString((UserData) this);

    json = Base64.encodeBase64URLSafeString(json.getBytes());
    return json;//  w w  w .  ja v  a 2 s  .c o m
}

From source file:com.feedzai.fos.api.ModelConfigTest.java

@Test
public void testJacksonSerialization() throws IOException {
    CategoricalAttribute categorical = new CategoricalAttribute("categoric", Arrays.asList("abc", "def"));
    NumericAttribute numeric = new NumericAttribute("numeric");

    List<Attribute> attributes = Arrays.<Attribute>asList(categorical, numeric);

    Map<String, String> properties = new HashMap<>();
    properties.put("p1", "value1");
    properties.put("p2", "value2");

    ModelConfig config = new ModelConfig(attributes, properties);

    ObjectMapper mapper = new ObjectMapper();
    Assert.assertTrue(mapper.canSerialize(config.getClass()));
    String json = mapper.writeValueAsString(config);

    System.out.println(json);//  w w w . ja va2 s .com
    System.out.println(config);

    ModelConfig deserialized = mapper.readValue(json, config.getClass());
    assertEquals(config, deserialized);
}

From source file:com.metamx.datatypes.mmx.AuctionSummaryTest.java

@Test
public void testSimpleRoundTrip() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    final String roundTripJson = objectMapper
            .writeValueAsString(objectMapper.readValue(simpleJson, MmxAuctionSummary.class));
    Assert.assertEquals(simpleJson, roundTripJson);
}

From source file:com.metamx.datatypes.mmx.AuctionSummaryTest.java

@Test
public void testSimpleSerialization() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    Assert.assertEquals(simpleJson, objectMapper.writeValueAsString(simpleAuctionSummary));
}

From source file:ch.icclab.cyclops.util.JSONUtil.java

/**
 * Converts the response object into a JSON string
 *
 * @param pojoObj a response POJO with values set
 * @return a Representation object with JSON string
 *//*from  w w w  .  j  av  a  2 s. c o  m*/

public Representation toJson(Object pojoObj) {
    ObjectMapper mapper = new ObjectMapper();
    String output;
    JsonRepresentation responseJson = null;

    try {
        output = mapper.writeValueAsString(pojoObj);
        responseJson = new JsonRepresentation(output);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return responseJson;
}

From source file:mercury.core.AngularDocumentationService.java

@AngularMethod(description = "Returns the list of the methods proposed by the service with their descriptions.", inputDescription = "Service name", inputExample = "{'name':'DocumentationService'}", outputExample = "[{'name':'doSomething','inputExample':'...','outputExample':''}]")
public void getMethodList(Deferred deferred, JSONParameters params) {
    String serviceName = params.getFirstString();
    MercuryTimer t = new MercuryTimer(this.getClass().getSimpleName());
    t.start();/*from w w w .j a  v  a  2s  .  co  m*/
    ObjectMapper mapper = new ObjectMapper();
    t.elapsed("initialising mapper");
    try {

        String json = mapper.writeValueAsString(manager.getService(serviceName).getMethodDescriptionList());
        t.elapsed("JSON transformation");
        deferred.parseAndResolve(json);
        t.elapsed("JSON resolve");
    } catch (Exception ex) {
        ImageJFX.getLogger().log(Level.SEVERE, null, ex);
        deferred.parseAndReject("[]");
    }
}

From source file:it.polimi.diceH2020.SPACE4Cloud.shared.Test2.java

@Test
public void test2() {
    Solution sol = SolutionGenerator.build();
    try {/*from  ww w.  j a v  a 2s .c  o m*/
        ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());

        String serialized = mapper.writeValueAsString(sol);
        System.out.println(serialized);
        Solution data2 = mapper.readValue(serialized, Solution.class);
        System.out.println(data2.toString());
        System.out.println(data2.toStringReduced());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:it.polimi.diceH2020.SPACE4Cloud.shared.Test2.java

@Test
public void test3() {
    System.out.println("-------------");
    InstanceDataMultiProvider sol = InstanceDataMultiProviderGenerator.build();
    try {//from w w w.j a v a 2  s .c  o  m
        ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());

        String serialized = mapper.writeValueAsString(sol);
        System.out.println(serialized);
        InstanceDataMultiProvider data2 = mapper.readValue(serialized, InstanceDataMultiProvider.class);
        System.out.println(data2.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:ch.icclab.cyclops.util.ResponseUtil.java

/**
 * Converts the response object into a JSON string
 *
 * @param response a response POJO with values set
 * @return a Representation object with JSON string
 *//*  w w  w  .j  a  va  2 s  .  c om*/

public Representation toJson(Response response) {
    ObjectMapper mapper = new ObjectMapper();
    String output;
    JsonRepresentation responseJson = null;

    try {
        output = mapper.writeValueAsString(response);
        responseJson = new JsonRepresentation(output);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return responseJson;
}