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.tectonica.thirdparty.Jackson2.java

public static String toJson(Object o, ObjectMapper mapper) {
    try {/*from  w w  w .j  av  a 2s . c  o m*/
        return mapper.writeValueAsString(o);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.trusolve.ant.filters.SynapseAPISwaggerFilter.java

public static Reader readDocument(Reader in) throws JsonProcessingException, IOException {
    ObjectMapper jsonIn = new ObjectMapper();
    JsonNode jn = jsonIn.readTree(in);/*from   w ww.  ja  v a  2s .  c om*/

    fixGetPaths(jn);

    jsonIn.enable(SerializationFeature.INDENT_OUTPUT);

    return new StringReader(jsonIn.writeValueAsString(jn));
}

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

@BeforeClass
public static void setUpClass() throws Exception {
    Properties info = new Properties();
    info.put("unitTestMode", "true");
    info.put("schemaFactory", "com.qubole.quark.planner.test.LatticeTest$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.hybridbpm.ui.component.chart.util.DiagrammeUtil.java

public static String objectToString(Object object) {
    try {//from   w  ww. j  av a2 s.co  m
        ObjectMapper mapper = new ObjectMapper();
        //            mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        return mapper.writeValueAsString(object);
    } catch (JsonProcessingException ex) {
        LOG.log(Level.SEVERE, ex.getMessage(), ex);
    }
    return new String();
}

From source file:com.abuabdul.mytravelpal.util.MyTravelPalFunc.java

public static final String eventJson(Object object) throws JsonProcessingException {
    final ObjectMapper mapper = new ObjectMapper();
    String jsonEventObjects = mapper.writeValueAsString(object);
    log.info("Event objects JSON :" + jsonEventObjects);
    return jsonEventObjects;
}

From source file:io.fabric8.devops.ProjectConfigs.java

public static String toYaml(Object dto) throws JsonProcessingException {
    ObjectMapper mapper = createObjectMapper();
    return mapper.writeValueAsString(dto);
}

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

/**
 * Tests that the provided object can be converted to json and
 * reconstructed. Also logs the json with the provided logger at FINE level.
 *
 * @return the reconstructed object//  w  ww  .ja  va  2  s  .c o m
 */
public static <T> T testJsonConv(ObjectMapper om, Logger logger, @Nullable T obj) {

    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, obj.getClass());
        recObj = (T) ret;
    } catch (Exception ex) {
        throw new RuntimeException("FAILED DESERIALIZING!", ex);
    }

    assertEquals(obj, recObj);
    return recObj;
}

From source file:de.alpharogroup.xml.json.JsonTransformer.java

/**
 * To json.//from   w w  w  .java 2  s  .c om
 *
 * @param <T>
 *            the generic type
 * @param object
 *            the object
 * @param newMapper
 *            flag that indicates if a new ObjectMapper should be created. if true a new
 *            ObjectMapper will be created otherwise the ObjectMapper from this class will be
 *            returned.
 * @return the string
 * @throws JsonProcessingException
 *             If an error occurs when converting object to String
 */
public static <T> String toJson(final T object, final boolean newMapper) throws JsonProcessingException {
    final ObjectMapper mapper = getObjectMapper(newMapper);
    final String json = mapper.writeValueAsString(object);
    return json;
}

From source file:io.dacopancm.socketdcm.helper.HelperUtil.java

public static String toJSON(Object[] toArray) {
    ObjectMapper mapper = new ObjectMapper();

    try {/*from w w  w. ja v a 2s . c  o  m*/
        return mapper.writeValueAsString(toArray);
    } catch (JsonProcessingException ex) {
        return "[]";
    }
}

From source file:com.github.yongchristophertang.engine.java.LoggerProxyHelper.java

private static String toPrinterString(Object obj, boolean pretty) {
    /*/*from w w  w.  java2s . c  o m*/
     * returns true if an object can be converted to a string which matches the regex pattern of ^([a-z]+\.)+[a-zA-Z]+@\w+$ that is
     * the official default object toString implementation
     */
    if (!obj.toString().matches("^([a-z]+\\.)+[a-zA-Z]+@\\w+$")) {
        return obj.toString();
    }

    ObjectMapper mapper = new ObjectMapper();
    try {
        return obj.getClass().getSimpleName() + ": "
                + (pretty ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj)
                        : mapper.writeValueAsString(obj));
    } catch (JsonProcessingException e) {
        return "The object has not implemented toString() method and cannot be serialized to a string either";
    }
}