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.codemacro.jcm.test.TestCluster.java

public void testNode() throws JsonGenerationException, JsonMappingException, IOException {
    Node node = new Node("127.0.0.1", "tcp:9000|http:8000", "");
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(node);
    Node node2 = mapper.readValue(json, Node.class);
    assertEquals(node, node2);//from w  ww . j  av  a 2  s  .  c  o  m
}

From source file:at.plechinger.scrapeql.cli.ScrapeQLShell.java

public ScrapeQLShell(String[] args) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
        Query query = parser.parse(CharStreams.toString(reader));

        Map<String, Object> output = query.execute();
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        String outputString = mapper.writeValueAsString(output);

        System.out.println(outputString);

    } catch (Throwable e) {
        System.out.println(e.getMessage());
    }//  w  ww.  ja v  a2 s . c om
}

From source file:com.ibm.iotf.connector.utils.messagehub.CreateTopicParameters.java

/**
 * Convert an instance of this class to its JSON
 * string representation.//  w  w  w . j a  v a 2s. c om
 */
@Override
public String toString() {
    ObjectMapper mapper = new ObjectMapper();

    try {
        return mapper.writeValueAsString(this);
    } catch (final JsonProcessingException e) {
        return "";
    }
}

From source file:macielaguilar.spring.Controladormensaje.java

@RequestMapping(value = "/mensaje", method = RequestMethod.GET, headers = { "Accept=application/json" })
@ResponseBody// w  w  w . j av a 2  s. co  m
String buscartodos() throws Exception {

    //esto va ser optenido a travez de dao mensaje
    /*Mensaje m1=new Mensaje();
    m1.setTitulo("springboot");
    m1.setCuerpo("springboot");
            
    Mensaje m2=new Mensaje();
    m2.setTitulo("Java 3");
    m2.setCuerpo("Esta materia es facil");
             
    Mensaje m3=new Mensaje();
    m3.setTitulo("otniel");
    m3.setCuerpo("tu no vas a pasar ;D");
            
    ArrayList<Mensaje> arreglo=new ArrayList<Mensaje>();
    arreglo.add (m1);
    arreglo.add (m2);
    arreglo.add (m3);
    //Vamos a usar una clase que se llame objetMapper
    ObjectMapper mapper= new ObjectMapper();
    return mapper .writeValueAsString(arreglo);
    */

    DAOMensaje dao = new DAOMensaje();
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(dao.buscarTodos());
}

From source file:com.perry.domain.call.CallTest.java

@Ignore
@Test// ww  w. j  a  v a  2  s . c  om
public void createJsonTest() throws JsonProcessingException {
    Vehicle vehicle = new Vehicle();
    vehicle.setColor("Red");
    vehicle.setKeyLocationType(KeyLocationType.CALL_FOR_KEYS);
    vehicle.setLicensePlateNumber("187 988");
    vehicle.setMake("Dodge");
    vehicle.setModel("Avenger");
    vehicle.setYear("2010");
    String pickUpLocation = "Pick Up Location";
    String dropOffLocation = "Drop Off Location";

    Customer customer = new Customer();
    customer.setFirstName("Levi");
    customer.setLastName("Liester");
    customer.setPhoneNumber("1-238-722-9888");
    customer.setVehicle(vehicle);
    CallType callType = CallType.IMPOUND;
    String truckId = "1k8";
    Call call = new Call(customer, pickUpLocation, dropOffLocation, callType, truckId);
    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(call));
}

From source file:org.seedstack.seed.rest.internal.jsonhome.JsonHomeRootResource.java

@Override
public Response buildResponse(HttpServletRequest httpServletRequest, UriInfo uriInfo) {
    final ObjectMapper mapper = new ObjectMapper();
    try {//  www .j a v  a  2 s . co  m
        return Response.ok(mapper.writeValueAsString(jsonHome)).type(new MediaType("application", "json"))
                .build();
    } catch (JsonProcessingException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error processing JSON-HOME")
                .type(MediaType.TEXT_PLAIN).build();
    }
}

From source file:io.pivotal.ecosystem.service.HelloMVCTest.java

private String toJson(Object o) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(o);
}

From source file:at.ac.univie.isc.asio.insight.VndErrorTest.java

@Test
public void jackson_round_tripping() throws Exception {
    final Correlation correlation = Correlation.valueOf("correlation");
    final VndError.ErrorChainElement first = VndError.ErrorChainElement.create("first-exception",
            "first-location");
    final VndError.ErrorChainElement second = VndError.ErrorChainElement.create("second-exception",
            "second-location");
    final VndError original = VndError.create("message", "cause", correlation, 1337,
            ImmutableList.of(first, second));
    final ObjectMapper mapper = new ObjectMapper();
    final String json = mapper.writeValueAsString(original);
    final VndError read = mapper.readValue(json, VndError.class);
    assertThat(read, is(original));/*from   w  w w  . j  a v  a 2 s  .c  o m*/
}

From source file:org.killbill.billing.plugin.meter.timeline.categories.TestCategoryAndMetrics.java

@Test(groups = "fast")
public void testMapping() throws Exception {
    final CategoryAndMetrics kinds = new CategoryAndMetrics("JVM");
    kinds.addMetric("GC");
    kinds.addMetric("CPU");

    final ObjectMapper mapper = new ObjectMapper();
    final String json = mapper.writeValueAsString(kinds);
    Assert.assertEquals("{\"eventCategory\":\"JVM\",\"metrics\":[\"GC\",\"CPU\"]}", json);

    final CategoryAndMetrics kindsFromJson = mapper.readValue(json, CategoryAndMetrics.class);
    Assert.assertEquals(kindsFromJson, kinds);
}

From source file:demo.ServiceLocationTests.java

@Test
public void json() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ServiceLocation value = mapper.readValue(mapper.writeValueAsString(new ServiceLocation(52, 0)),
            ServiceLocation.class);
    assertEquals(52, value.getLatitude(), 0.01);
}