Example usage for com.fasterxml.jackson.databind ObjectWriter writeValueAsString

List of usage examples for com.fasterxml.jackson.databind ObjectWriter writeValueAsString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectWriter 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.orange.ngsi.model.RegisterContextResponseTest.java

@Test
public void serializationSimpleRegisterContextResponse() throws IOException {

    RegisterContextResponse registerContextResponse = new RegisterContextResponse("52a744b011f5816465943d58");
    registerContextResponse.setDuration("P1M");

    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
    String json = writer.writeValueAsString(registerContextResponse);
    assertEquals("52a744b011f5816465943d58", JsonPath.read(json, "$.registrationId"));
    assertEquals("P1M", JsonPath.read(json, "$.duration"));
}

From source file:org.esbtools.gateway.GatewayRequest.java

public String toJson() {
    String thisJson;//  ww  w  . ja v a 2 s  .  c  o  m
    try {
        ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter();
        thisJson = ow.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
    return thisJson;
}

From source file:com.appspot.potlachkk.auth.LoginStatusBuilder.java

public String toJson() throws JsonProcessingException {
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    return ow.writeValueAsString(this.loginStatus);
}

From source file:org.esbtools.gateway.GatewayResponse.java

public String toJson() {
    String thisJson;//  w  w w .j ava  2 s  . c o m
    try {
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        thisJson = ow.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
    return thisJson;
}

From source file:io.cloudslang.engine.node.services.WorkersMBean.java

@ManagedOperation(description = "Returns a list of all registered workers")
public String showWorkers() throws IOException {
    ObjectWriter objectWriter = objectMapper().writerWithDefaultPrettyPrinter();
    return objectWriter.writeValueAsString(workerNodeService.readAllWorkers());
}

From source file:com.orange.ngsi.model.QueryContextModelTest.java

@Test
public void serializationSimpleQueryContext() throws IOException {
    QueryContext queryContext = createQueryContextTemperature();
    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
    String json = writer.writeValueAsString(queryContext);

    List<EntityId> entityIdList = JsonPath.read(json, "$.entities[*]");
    assertEquals(1, entityIdList.size());
    assertEquals("S*", JsonPath.read(json, "$.entities[0].id"));
    assertEquals("TempSensor", JsonPath.read(json, "$.entities[0].type"));
    assertEquals(true, JsonPath.read(json, "$.entities[0].isPattern"));
    List<String> attributeList = JsonPath.read(json, "$.attributes[*]");
    assertEquals(1, attributeList.size());
    assertEquals("temp", JsonPath.read(json, "$.attributes[0]"));
}

From source file:com.butler.service.PushNotifier.java

private void send() {
    try {//from w ww. ja v a  2s  .  co  m
        String url = "https://android.googleapis.com/gcm/send";
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        request.addHeader("Authorization", "key=AIzaSyCBODt7lH-oPSKiZJ5MJlugTS0BV3U-KGc");
        request.addHeader("Content-Type", "application/json");
        Header header = new Header();
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String rawData = ow.writeValueAsString(header);
        request.setEntity(new StringEntity(rawData));
        HttpResponse response = client.execute(request);
        // System.out.println("response"+IOUtils.toString(response.getEntity().getContent()));
    } catch (Exception ex) {
        Logger.getLogger(PushNotifier.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.raml.v2.internal.framework.suggester.SuggesterTestCase.java

@Test
public void verifySuggestion() throws IOException {
    final RamlSuggester ramlSuggester = new RamlSuggester();
    String content = IOUtils.toString(new FileInputStream(input), "UTF-8");

    if (IS_OS_WINDOWS) {
        content = content.replace(IOUtils.LINE_SEPARATOR_WINDOWS, IOUtils.LINE_SEPARATOR_UNIX);
    }/* www. j a v  a 2  s . c  o m*/

    final int offset = content.indexOf(CURSOR_KEYWORD);
    final String document = content.substring(0, offset) + content.substring(offset + CURSOR_KEYWORD.length());
    final List<Suggestion> suggestions = ramlSuggester.suggestions(document, offset - 1).getSuggestions();
    final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    dump = ow.writeValueAsString(suggestions);
    expected = IOUtils.toString(new FileInputStream(this.expectedOutput));
    Assert.assertTrue(jsonEquals(dump, expected));
}

From source file:org.usd.edu.btl.cli.ConvertBioextract.java

public void toBets(String inputS, String output) {
    ObjectMapper mapper = new ObjectMapper(); //create new Jackson Mapper
    File input = new File(inputS);

    BioExtV1 bioExtTool;//from  ww  w  . j  a  v  a  2s.  c o  m

    try {
        //map input json files to iplant class
        bioExtTool = mapper.readValue(input, BioExtV1.class);

        BETSV1 bets = BioExtConverter.toBETS(bioExtTool); //pass the iplant tool spec, convert to bets
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String betsJson = ow.writeValueAsString(bets); //write Json as String
        if (output == null) {
            /*===============PRINT JSON TO CONSOLE AND FILES =================== */
            System.out.println("************************************************\n"
                    + "*********PRINTING OUT CONVERSION************\n"
                    + "----------BioExtract --> Bets--------------\n"
                    + "************************************************\n");
            //print objects as Json using jackson

            System.out.println("=== BioExt TO BETS JSON === \n" + betsJson);

        } else {
            //write to files
            ow.writeValue(new File(output), betsJson);
            System.out.println(output + " has been created successfully");
            System.exit(1);

        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.auditbucket.search.service.SearchAdmin.java

@PostConstruct
private void doHealth() {
    ObjectMapper om = new ObjectMapper();
    try {/*w  w  w  .  j a  va 2 s . co  m*/
        ObjectWriter or = om.writerWithDefaultPrettyPrinter();
        logger.info("\r\n" + or.writeValueAsString(getHealth()));
    } catch (JsonProcessingException e) {

        logger.error("doHealth", e);
    }
}