Search in sources :

Example 16 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project jhipster-sample-app-websocket by jhipster.

the class TestUtil method convertObjectToJsonBytes.

/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JavaTimeModule module = new JavaTimeModule();
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project jhipster-sample-app-mongodb by jhipster.

the class TestUtil method convertObjectToJsonBytes.

/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JavaTimeModule module = new JavaTimeModule();
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 18 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project jhipster-sample-app-hazelcast by jhipster.

the class TestUtil method convertObjectToJsonBytes.

/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JavaTimeModule module = new JavaTimeModule();
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 19 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project hello-world by haoziapple.

the class TestUtil method convertObjectToJsonBytes.

/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JavaTimeModule module = new JavaTimeModule();
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 20 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project presto by prestodb.

the class PrometheusQueryResponse method parsePrometheusQueryResponse.

private void parsePrometheusQueryResponse(InputStream response) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    JsonParser parser = new JsonFactory().createParser(response);
    while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();
        if (FIELD_NAME.equals(jsonToken)) {
            if (parser.getCurrentName().equals(parseResultStatus)) {
                parser.nextToken();
                if (parser.getValueAsString().equals(parseResultSuccess)) {
                    this.status = true;
                    while (!parser.isClosed()) {
                        parser.nextToken();
                        if (FIELD_NAME.equals(jsonToken)) {
                            if (parser.getCurrentName().equals(parseResultType)) {
                                parser.nextToken();
                                resultType = ResultType.valueOf(parser.getValueAsString());
                            }
                            if (parser.getCurrentName().equals(parseResult)) {
                                parser.nextToken();
                                ArrayNode node = mapper.readTree(parser);
                                result = node.toString();
                                break;
                            }
                        }
                    }
                } else {
                    // error path
                    String parsedStatus = parser.getValueAsString();
                    // parsing json is key-value based, so first nextToken is advanced to the key, nextToken advances to the value.
                    // for "errorType" key
                    parser.nextToken();
                    // for "errorType" key's value
                    parser.nextToken();
                    errorType = parser.getValueAsString();
                    // advance to "error" key
                    parser.nextToken();
                    // advance to "error" key's value
                    parser.nextToken();
                    error = parser.getValueAsString();
                    throw new PrestoException(PROMETHEUS_PARSE_ERROR, "Unable to parse Prometheus response: " + parsedStatus + " " + errorType + " " + error);
                }
            }
        }
        if (result != null) {
            break;
        }
    }
    if (result != null && resultType != null) {
        switch(resultType) {
            case matrix:
            case vector:
                results = mapper.readValue(result, new TypeReference<List<PrometheusMetricResult>>() {
                });
                break;
            case scalar:
            case string:
                PrometheusTimeSeriesValue stringOrScalarResult = mapper.readValue(result, new TypeReference<PrometheusTimeSeriesValue>() {
                });
                Map<String, String> madeUpMetricHeader = new HashMap<>();
                madeUpMetricHeader.put("__name__", resultType.toString());
                PrometheusTimeSeriesValueArray timeSeriesValues = new PrometheusTimeSeriesValueArray(singletonList(stringOrScalarResult));
                results = singletonList(new PrometheusMetricResult(madeUpMetricHeader, timeSeriesValues));
        }
    }
}
Also used : HashMap(java.util.HashMap) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PrestoException(com.facebook.presto.spi.PrestoException) JsonToken(com.fasterxml.jackson.core.JsonToken) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)75 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)59 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)16 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)7 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)6 Test (org.junit.Test)6 Bean (org.springframework.context.annotation.Bean)6 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)5 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)4 Before (org.junit.Before)4 Primary (org.springframework.context.annotation.Primary)4 StdDateFormat (com.fasterxml.jackson.databind.util.StdDateFormat)3 LocalDateSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer)3 LocalDateTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer)3 LocalTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer)3 IOException (java.io.IOException)3 MetricsModule (com.codahale.metrics.json.MetricsModule)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2 Module (com.fasterxml.jackson.databind.Module)2