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:org.lenskit.specs.SpecUtils.java

/**
 * Convert a specification to a string./*from w ww  . j  a  v  a 2  s .  c om*/
 * @param spec The specification.
 * @return The JSON string representation of the specification.
 */
public static String stringify(AbstractSpec spec) {
    ObjectWriter writer = createMapper().writer();
    try {
        return writer.writeValueAsString(spec);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error stringifying JSON", e);
    }
}

From source file:us.colloquy.util.ElasticLoader.java

private static void indexDiaries(List<DiaryEntry> diaryEntryList, RestHighLevelClient client,
        BulkRequest bulkRequest) throws IOException {
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

    for (DiaryEntry diary : diaryEntryList) {
        String json = ow.writeValueAsString(diary);

        String id = diary.getId() + "-" + diary.getDate() + "-" + diary.getSource();

        if (StringUtils.isNotEmpty(json)) {
            IndexRequest indexRequest = new IndexRequest("lntolstoy-diaries", "diaries", id).source(json,
                    XContentType.JSON);//from  ww w.j  a  va2 s. co  m

            bulkRequest.add(new UpdateRequest("lntolstoy-diaries", "diaries", id).doc(json, XContentType.JSON)
                    .upsert(indexRequest));
        } else {
            System.out.println("empty doc: " + id.toString());
        }
    }

    BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

    if (bulkResponse.hasFailures()) {
        // process failures by iterating through each bulk response item
        for (BulkItemResponse br : bulkResponse.getItems()) {
            System.out.println(br.getFailureMessage());
        }
    }

}

From source file:com.hpcloud.util.Serialization.java

/**
 * Returns {@code node} serialized to a json string for the {@code node}.
 * /*from   ww  w.  j  a  v  a 2  s.c o m*/
 * @throws RuntimeException if deserialization fails
 */
public static String toJson(JsonNode node) {
    try {
        ObjectWriter writer = mapper.writer();
        return writer.writeValueAsString(node);
    } catch (Exception e) {
        throw Exceptions.uncheck(e, "Failed to serialize object: {}", node);
    }
}

From source file:com.hpcloud.util.Serialization.java

/**
 * Returns {@code object} serialized to a json string.
 * //from w  ww.  j av  a  2 s  . c o  m
 * @throws RuntimeException if deserialization fails
 */
public static String toJson(Object object) {
    Class<?> unwrappedType = Types.deProxy(object.getClass());
    registerTarget(unwrappedType);

    try {
        ObjectWriter writer = rootMapper.writerWithType(unwrappedType);
        return writer.writeValueAsString(object);
    } catch (Exception e) {
        throw Exceptions.uncheck(e, "Failed to serialize object: {}", object);
    }
}

From source file:com.infinities.skyport.util.JsonUtil.java

public static String toJson(Object object, Class<? extends Views.Short> view) {
    ObjectMapper mapper = getObjectMapper();
    ObjectWriter writer = mapper.writerWithView(view).withDefaultPrettyPrinter();
    try {//from w ww. j a v  a 2s  .  c o  m
        if (object == null || "".equals(object)) {
            object = mapper.createObjectNode();
        }
        return writer.writeValueAsString(object);
    } catch (Exception e) {
        logger.error("json parsing failed", e);
        throw new RuntimeException(e);
    }
}

From source file:org.wrml.util.AsciiArt.java

public static String express(final Object object) {

    final ObjectWriter objectWriter = new ObjectMapper().writer(new DefaultPrettyPrinter());
    try {//from  ww w.  jav  a 2s  .c om
        return objectWriter.writeValueAsString(object);
    } catch (final Exception e) {
        LOG.warn(e.getMessage());
    }

    return null;
}

From source file:com.infinities.skyport.util.JsonUtil.java

public static String toJson(boolean insertResponseCode, String msg, Object object,
        Class<? extends Views.Short> view) {
    ObjectMapper mapper = getObjectMapper();
    ObjectWriter writer = mapper.writerWithView(view).withDefaultPrettyPrinter();
    JsonNode rootNode = mapper.createObjectNode();

    try {/*from   w ww. ja  v  a  2  s  .c o  m*/
        if (object == null || "".equals(object)) {
            object = mapper.createObjectNode();
        }

        String temp = writer.writeValueAsString(object);
        rootNode = mapper.readTree(temp);
    } catch (Exception e) {
        logger.error("json parsing failed", e);

    }

    ObjectNode root = getObjectMapper().createObjectNode();
    root.put(JsonConstants.STATUS, insertResponseCode ? 1 : 0).put(JsonConstants.MSG, msg)
            .put(JsonConstants._DATA, rootNode);

    try {
        return getObjectMapper().configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false)
                .writeValueAsString(root);
    } catch (Exception e) {
        logger.error("json parsing failed", e);
        throw new RuntimeException(e);
    }
}

From source file:com.amazon.sqs.javamessaging.JsonDataConverter.java

public String serializeToJson(Object obj) throws JsonProcessingException {
    ObjectWriter objectWriter = objectMapper.writer();
    return objectWriter.writeValueAsString(obj);
}

From source file:com.rusticisoftware.tincan.json.JSONBase.java

@Override
public String toJSON(TCAPIVersion version, Boolean pretty) {
    ObjectWriter writer = Mapper.getWriter(pretty);
    try {//  w ww. j  a va2 s .  c om
        return writer.writeValueAsString(this.toJSONNode(version));
    } catch (JsonProcessingException ex) {
        return "Exception in JSONBase Class: " + ex.toString();
    }
}

From source file:puma.application.evaluation.metrics.MetricsController.java

@RequestMapping(value = "/metrics/results", method = RequestMethod.GET, produces = "text/plain")
public @ResponseBody String results() {
    try {//from  w  ww.j av a2s.  com
        ObjectMapper mapper = new ObjectMapper();
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        return writer.writeValueAsString(TimerFactory.getInstance().getMetricRegistry());
    } catch (JsonProcessingException e) {
        return e.getMessage();
    }
}