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:de.dev.eth0.elasticsearch.aem.search.Search.java

/**
 * Serialize this Search to a JSON/*ww w .ja  v  a2 s  .c o m*/
 *
 * @return
 */
public String serialize() {
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.writeValueAsString(this);
    } catch (JsonProcessingException ex) {
        LOG.error("Could not serialize search", ex);
    }
    return StringUtils.EMPTY;
}

From source file:com.meltmedia.jackson.crypto.DynamicEncryptionServiceTest.java

@Test
public void shouldSerializeInBase64()
        throws EncryptionException, UnsupportedEncodingException, JsonProcessingException {
    EncryptedJson value = new EncryptedJson();
    value.setKeyName("default");
    value.setIv(base64.decode(IV_FROM_NODE));
    value.setSalt(base64.decode(SALT_FROM_NODE));
    value.setValue(base64.decode(VALUE_FROM_NODE));
    value.setIterations(2000);/* www. ja v a  2  s. c  o  m*/
    value.setKeyLength(256);
    value.setCipher(Ciphers.AES_256_CBC);
    value.setKeyDerivation(KeyDerivations.PBKDF2);

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

    assertThat("the iv is found in the text", serialized, containsString(IV_FROM_NODE));
    assertThat("the salt is found in the text", serialized, containsString(SALT_FROM_NODE));
    assertThat("the value is found in the text", serialized, containsString(VALUE_FROM_NODE));
}

From source file:io.dacopancm.oraclesp.model.Conferencia.java

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

    try {//from  w w  w  . j  a va 2  s .  c  o m
        return mapper.writeValueAsString(this);
    } catch (JsonProcessingException ex) {
        return "[]";
    }
}

From source file:com.callidusrobotics.droptables.model.Parameter.java

@Override
public String toString() {
    ObjectMapper mapper = new ObjectMapper();
    try {//from www .  j a  va  2  s .  c  om
        return mapper.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        return super.toString();
    }
}

From source file:com.github.jrubygradle.jem.Gem.java

/**
 * Convert whatever object we're given into a safe (see: JSON) reprepsentation
 *
 * @param value Object to pass to Jackson to create a string representation of
 * @return String representation of the value parameter
 * @throws JsonProcessingException Exception when the value cannot be JSON serialized
 *//*from   w  w  w.j a va  2  s.c  o  m*/
protected String sanitize(Object value) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(value);
}

From source file:com.mazmy.controller.CarControllerTest.java

@Test
public void testCreateCarWithSecurityWithWrongValue() throws Exception {
    CarDTO.CarDTOBuilder carDTOBuilder = CarDTO.newBuilder().setLicensePlate("D K PC 1313").setSeatCount(4)
            .setRating(2).setManufacturer(null);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(carDTOBuilder.createCarDTO());
    this.mockMvc.perform(post("/v1/cars").with(authentication(auth)).contentType(MediaType.APPLICATION_JSON)
            .content(json).accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
}

From source file:codes.thischwa.c5c.requestcycle.response.GenericResponse.java

protected String serialize(Object obj) {
    ObjectMapper mapper = new ObjectMapper();
    try {//w w  w . j  a  v  a  2 s . c o m
        String jsonStr = mapper.writeValueAsString(obj);
        // escaping the slashes to use JSON in textareas
        jsonStr = jsonStr.replace(Constants.defaultSeparator, "\\".concat(Constants.defaultSeparator));
        return jsonStr;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ibm.BestSellerServlet.java

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 *//*from w ww.j a  va2 s. c o  m*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/json");
    response.setCharacterEncoding("UTF-8");

    OutputStream stream = response.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");

    String listName = request.getParameter("list");
    String date = request.getParameter("date");

    logger.debug("Requested list {} and requested date {}", listName, date);

    NewYorkTimes times = new NewYorkTimes(listName, date);

    try {
        BestSellerList bestSellers = times.getList();
        ObjectMapper mapper = new ObjectMapper();

        String listContents = mapper.writeValueAsString(bestSellers.getBooks());
        logger.debug("Booklist is {}", listContents);
        writer.write(listContents);
        writer.flush();
        writer.close();
    } catch (Exception e) {
        logger.error("New York times list unavailable");
        throw new IOException("Could not get book list from ny times");
    }

}

From source file:com.mazmy.controller.CarControllerTest.java

@Test
public void testCreateCarWithoutSecurity() throws Exception {
    CarDTO.CarDTOBuilder carDTOBuilder = CarDTO.newBuilder().setLicensePlate("D 7a PC 1515").setSeatCount(4)
            .setRating(2).setEngineType(EngineType.PETROL.type()).setConvertible(ConvertibleValue.YES.value())
            .setManufacturer(null);//  w ww. j a  v a2s.c om
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(carDTOBuilder.createCarDTO());
    this.mockMvc.perform(post("/v1/cars").contentType(MediaType.APPLICATION_JSON).content(json))
            .andExpect(status().isUnauthorized());
}

From source file:com.mazmy.controller.CarControllerTest.java

@Test
public void testCreateCarWithSecurity() throws Exception {
    CarDTO.CarDTOBuilder carDTOBuilder = CarDTO.newBuilder().setLicensePlate("D 7a PC 1515").setSeatCount(4)
            .setRating(2).setEngineType(EngineType.PETROL.type()).setConvertible(ConvertibleValue.YES.value())
            .setManufacturer(null);//from ww w .  j  av  a 2s  .com
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(carDTOBuilder.createCarDTO());
    this.mockMvc.perform(post("/v1/cars").with(authentication(auth)).contentType(MediaType.APPLICATION_JSON)
            .content(json).accept(MediaType.APPLICATION_JSON)).andExpect(status().is(201));
}