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.qubole.quark.planner.test.LatticeWithFilterTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    Properties info = new Properties();
    info.put("unitTestMode", "true");
    info.put("schemaFactory", "com.qubole.quark.planner.test.LatticeWithFilterTest$SchemaFactory");

    ImmutableList<String> defaultSchema = ImmutableList.of("FOODMART");
    final ObjectMapper mapper = new ObjectMapper();

    info.put("defaultSchema", mapper.writeValueAsString(defaultSchema));

    parser = new SqlQueryParser(info);

}

From source file:com.tomtom.speedtools.json.Json.java

@Nonnull
private static String toMapper(@Nonnull final ObjectMapper mapper, @Nonnull final Object obj) {
    assert mapper != null;
    assert obj != null;
    try {//from   w ww .j  av a 2  s .com
        return mapper.writeValueAsString(obj);
    } catch (final JsonMappingException e) {
        LOG.error("toMapper: Map exception {} --> JSON, mapper={}, exception={}",
                obj.getClass().getCanonicalName(), mapper.getClass().getCanonicalName(), e.toString());
    } catch (final IOException e) {
        LOG.error("toMapper: Cannot map {} --> JSON, mapper={}, exception={}",
                obj.getClass().getCanonicalName(), mapper.getClass().getCanonicalName(), e.toString());
    }
    return "";
}

From source file:com.netsteadfast.greenstep.bsc.util.BscMobileCardUtils.java

public static String getVisionCardUpload(VisionVO vision) throws ServiceException, Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonData = objectMapper.writeValueAsString(vision);
    return UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, jsonData.getBytes(),
            SimpleUtils.getUUIDStr() + ".json");
}

From source file:eu.trentorise.opendata.commons.test.jackson.TodJacksonTester.java

/**
 * Tests that the provided object can be converted to json and reconstructed
 * as type T. Also logs the json with the provided logger at FINE level.
 *
 * @return the reconstructed object//from  ww  w.  j av a  2s  . co  m
 */
public static <T> T testJsonConv(ObjectMapper om, Logger logger, @Nullable Object obj, Class<T> targetClass) {

    checkNotNull(om);
    checkNotNull(logger);

    T recObj;

    String json;

    try {
        json = om.writeValueAsString(obj);
        logger.log(Level.FINE, "json = {0}", json);
    } catch (Exception ex) {
        throw new RuntimeException("FAILED SERIALIZING!", ex);
    }
    try {
        Object ret = om.readValue(json, targetClass);
        recObj = (T) ret;
    } catch (Exception ex) {
        throw new RuntimeException("FAILED DESERIALIZING!", ex);
    }

    assertEquals(obj, recObj);
    return recObj;
}

From source file:com.netsteadfast.greenstep.bsc.util.BscMobileCardUtils.java

public static String getVisionCardUpload(String frequency, String startDate, String endDate)
        throws ServiceException, Exception {
    List<VisionVO> visionScores = getVisionCard(frequency, startDate, endDate);
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonData = objectMapper.writeValueAsString(visionScores);
    return UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, jsonData.getBytes(),
            SimpleUtils.getUUIDStr() + ".json");
}

From source file:com.netsteadfast.greenstep.sys.CxfServerBean.java

public static String createParamValue() throws Exception {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("before", String.valueOf(System.currentTimeMillis()));
    ObjectMapper mapper = new ObjectMapper();
    String jsonData = mapper.writeValueAsString(paramMap);
    String uploadOid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false,
            jsonData.getBytes(), SimpleUtils.getUUIDStr() + ".json");
    return SimpleUtils.toHex(
            EncryptorUtils.encrypt(Constants.getEncryptorKey1(), Constants.getEncryptorKey2(), uploadOid));
}

From source file:org.commonjava.aprox.bind.jaxrs.util.ResponseUtils.java

public static Response formatOkResponseWithJsonEntity(final Object dto, final ObjectMapper objectMapper) {
    if (dto == null) {
        return Response.noContent().build();
    }/*w  w  w.ja  v a 2  s  .  c  om*/

    Response response;
    try {
        response = Response.ok(objectMapper.writeValueAsString(dto), ApplicationContent.application_json)
                .build();
    } catch (final JsonProcessingException e) {
        response = formatResponse(e, "Failed to serialize DTO to JSON: " + dto, true);
    }

    return response;
}

From source file:eu.trentorise.opendata.commons.test.jackson.OdtJacksonTester.java

/**
* Converts {@code obj} to an {@link ObjectNode}, sets field
* {@code fieldName} to {@code newNode} and returns the json string
* representation of such new object. Also logs the json with the provided logger at FINE
* level./* w  w w. j  a  v a2s  .  c  o  m*/
*/
public static String changeField(ObjectMapper objectMapper, Logger logger, Object obj, String fieldName,
        JsonNode newNode) {
    checkNotNull(obj);
    checkNotEmpty(fieldName, "Invalid field name!");

    String string;
    try {
        string = objectMapper.writeValueAsString(obj);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException("Error while jacksonizing object to json node!", ex);
    }
    TreeNode treeNode;
    try {
        treeNode = (ObjectNode) objectMapper.readTree(string);
    } catch (IOException ex) {
        throw new RuntimeException("Error while creating json tree from serialized object:" + string, ex);
    }
    if (!treeNode.isObject()) {
        throw new OdtException(
                "The provided object was jacksonized to a string which does not represent a JSON object! String is "
                        + string);
    }
    ObjectNode jo = (ObjectNode) treeNode;
    jo.put(fieldName, newNode);

    String json = jo.toString();

    logger.log(Level.FINE, "converted json = {0}", json);

    return json;

}

From source file:com.qubole.quark.planner.test.RelToSqlConverterTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    info = new Properties();
    info.put("unitTestMode", "true");
    info.put("defaultSchema", QuarkTestUtil.toJson("FOODMART"));
    info.put("schemaFactory", "com.qubole.quark.planner.test.RelToSqlConverterTest$SchemaFactory");

    ImmutableList<String> defaultSchema = ImmutableList.of("FOODMART");
    final ObjectMapper mapper = new ObjectMapper();
    info.put("defaultSchema", mapper.writeValueAsString(defaultSchema));
}

From source file:com.qubole.quark.planner.test.LayeredCubeTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    Properties info = new Properties();
    info.put("unitTestMode", "true");
    info.put("schemaFactory", "com.qubole.quark.planner.test.LayeredCubeTest$SchemaFactory");

    ImmutableList<String> defaultSchema = ImmutableList.of("TPCDS");
    final ObjectMapper mapper = new ObjectMapper();

    info.put("defaultSchema", mapper.writeValueAsString(defaultSchema));
    parser = new SqlQueryParser(info);
}