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.baasbox.controllers.Root.java

@With(RootCredentialWrapFilter.class)
public static Result timers() throws JsonProcessingException {
    if (!BaasBoxMetric.isActivate())
        return status(SERVICE_UNAVAILABLE, "The metrics service are disabled");
    ObjectMapper mapper = new ObjectMapper()
            .registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.MILLISECONDS, false));
    return ok(mapper.writeValueAsString(BaasBoxMetric.registry.getTimers()));
}

From source file:com.groupdocs.sdk.common.ApiInvoker.java

public static String serialize(Object obj) throws ApiException {
    try {/*from w  w w .jav  a2s. c o  m*/
        if (obj != null) {
            ObjectMapper jsonMapper = JsonUtil.getJsonMapper();
            jsonMapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);
            return jsonMapper.writeValueAsString(obj);
        } else
            return "";
    } catch (Exception e) {
        throw new ApiException(500, e.getMessage());
    }
}

From source file:com.gtcgroup.jped.test.core.rulechain.JptRuleChain.java

/**
 * @param testRule//from ww  w.  j av  a  2  s  . c o m
 * @return boolean
 */
protected static boolean isRuleAlreadyInvoked(final TestRule testRule) {

    boolean result = false;

    try {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        final String testRuleKey = mapper.writeValueAsString(testRule);

        if (testRule instanceof JptBaseForSuiteRule) {

            if (JptRuleChain.suiteRuleInstanceList.contains(testRuleKey)) {

                result = true;
            }

            JptRuleChain.suiteRuleInstanceList.add(testRuleKey);

        } else if (testRule instanceof JptBaseForClassRule) {

            if (JptRuleChain.classRuleInstanceList.contains(testRuleKey)) {

                result = true;
            }

            JptRuleChain.suiteRuleInstanceList.add(testRuleKey);
        }
    } catch (final Exception e) {
        throw new TestingException("TODO: message", e);
    }
    return result;

}

From source file:edu.usd.btl.toolTree.OntoToTree.java

public static String getOntoJson() throws Exception {
    ObjectMapper treeMapper = new ObjectMapper(); //create new Jackson Mapper
    OntologyFileRead ontFileRead = new OntologyFileRead();
    ArrayList<edu.usd.btl.ontology.BioPortalElement> nodeList = ontFileRead
            .readFile(".\\ontology_files\\EDAM_1.3.owl");

    //write nodelist to JSON string
    ObjectWriter treeWriter = treeMapper.writer().withDefaultPrettyPrinter();
    String edamJSON = treeMapper.writeValueAsString(nodeList);
    System.out.println("**** EDAM ONTOLOGY JSON ****" + edamJSON);

    return edamJSON;
}

From source file:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static Response formatOkResponseWithJsonEntity(final Object dto, final ObjectMapper objectMapper) {
    if (dto == null) {
        return Response.noContent().build();
    }//from   ww w .j a  va2 s .c om

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

    return response;
}

From source file:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static Response formatCreatedResponseWithJsonEntity(final URI location, final Object dto,
        final ObjectMapper objectMapper) {
    if (dto == null) {
        return Response.noContent().build();
    }// w w  w .j  a va2 s . com

    Response response;
    try {
        response = Response.created(location).entity(objectMapper.writeValueAsString(dto))
                .type(StandardApplicationContent.application_json).build();
    } catch (final JsonProcessingException e) {
        response = formatResponse(e, "Failed to serialize DTO to JSON: " + dto);
    }

    return response;
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactory.java

private static String toJsonString(Object objectToConvert) {
    try {/*from   www.  jav a2s .  c om*/
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        return objectMapper.writeValueAsString(objectToConvert);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error while converting object to JSON", e);
    }
}

From source file:org.eclipse.sw360.datahandler.common.SW360Utils.java

private static String serializeToJson(Object value) throws SW360Exception {
    ObjectMapper mapper = getObjectMapper();
    try {//ww w . j a  va 2  s  . c o  m
        return mapper.writeValueAsString(value);
    } catch (JsonProcessingException e) {
        String msg = String.format("Cannot serialize field value %s to JSON", value);
        log.error(msg, e);
        throw new SW360Exception(msg);
    }
}

From source file:io.orchestrate.client.ResponseConverterUtil.java

@SuppressWarnings("unchecked")
static <T> T jsonToDomainObject(ObjectMapper mapper, JsonNode json, String rawValue, Class<T> clazz)
        throws IOException {
    if (clazz == null || clazz == Void.class || json == null || json.isNull()) {
        return null;
    }/*from w  ww .j a  va 2 s  .c om*/

    if (clazz.equals(String.class)) {
        if (rawValue != null) {
            return (T) rawValue;
        }
        return (T) mapper.writeValueAsString(json);
    }
    return mapper.treeToValue(json, clazz);
}

From source file:com.servioticy.api.commons.data.CouchBase.java

/**
 * @return all the Service Objects as String
 *///from   www.  j  a  va  2  s .c  o m
public static String getAllSOs() {
    ArrayList<String> sos = new ArrayList<String>();

    try {
        View view = cli_so.getView("index", "byIndex");
        Query query = new Query();
        query.setStale(Stale.FALSE);
        ViewResponse result = cli_so.query(view, query);
        for (ViewRow row : result) {
            if (row.getKey() != null)
                sos.add(row.getKey());
        }
    } catch (Exception e) {
        LOG.error(e);
        throw new ServIoTWebApplicationException(Response.Status.INTERNAL_SERVER_ERROR,
                "Accessing the view: " + e.getMessage());
    }

    ObjectMapper mapper = new ObjectMapper();
    String str_sos = null;
    try {
        str_sos = mapper.writeValueAsString(sos);
    } catch (JsonProcessingException e) {
        LOG.error(e);
        throw new ServIoTWebApplicationException(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }

    return str_sos;
}