Search in sources :

Example 16 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SqlResourceTest method doPost.

// Returns either an error or a result.
private Pair<QueryInterruptedException, List<Map<String, Object>>> doPost(final SqlQuery query) throws Exception {
    final Response response = resource.doPost(query);
    if (response.getStatus() == 200) {
        final StreamingOutput output = (StreamingOutput) response.getEntity();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        output.write(baos);
        return Pair.of(null, JSON_MAPPER.<List<Map<String, Object>>>readValue(baos.toByteArray(), new TypeReference<List<Map<String, Object>>>() {
        }));
    } else {
        return Pair.of(JSON_MAPPER.readValue((byte[]) response.getEntity(), QueryInterruptedException.class), null);
    }
}
Also used : Response(javax.ws.rs.core.Response) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) QueryInterruptedException(io.druid.query.QueryInterruptedException)

Example 17 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class WhiteListBasedDruidToTimelineEventConverter method readMap.

private ImmutableSortedMap<String, ImmutableList<String>> readMap(final String mapPath) {
    String fileContent;
    String actualPath = mapPath;
    try {
        if (Strings.isNullOrEmpty(mapPath)) {
            actualPath = this.getClass().getClassLoader().getResource("defaultWhiteListMap.json").getFile();
            LOGGER.info("using default whiteList map located at [%s]", actualPath);
            fileContent = CharStreams.toString(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("defaultWhiteListMap.json")));
        } else {
            fileContent = Files.asCharSource(new File(mapPath), Charset.forName("UTF-8")).read();
        }
        return mapper.reader(new TypeReference<ImmutableSortedMap<String, ImmutableList<String>>>() {
        }).readValue(fileContent);
    } catch (IOException e) {
        throw new ISE(e, "Got an exception while parsing file [%s]", actualPath);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) ISE(com.metamx.common.ISE) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) File(java.io.File)

Example 18 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project feign by OpenFeign.

the class JacksonCodecTest method customEncoder.

@Test
public void customEncoder() throws Exception {
    JacksonEncoder encoder = new JacksonEncoder(Arrays.<Module>asList(new SimpleModule().addSerializer(Zone.class, new ZoneSerializer())));
    List<Zone> zones = new LinkedList<Zone>();
    zones.add(new Zone("denominator.io."));
    zones.add(new Zone("denominator.io.", "abcd"));
    RequestTemplate template = new RequestTemplate();
    encoder.encode(zones, new TypeReference<List<Zone>>() {
    }.getType(), template);
    assertThat(template).hasBody(//
    "" + "[ {\n" + "  \"name\" : \"DENOMINATOR.IO.\"\n" + "}, {\n" + "  \"name\" : \"DENOMINATOR.IO.\",\n" + "  \"id\" : \"ABCD\"\n" + "} ]");
}
Also used : RequestTemplate(feign.RequestTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 19 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project feign by OpenFeign.

the class JacksonCodecTest method encodesFormParams.

@Test
public void encodesFormParams() throws Exception {
    Map<String, Object> form = new LinkedHashMap<String, Object>();
    form.put("foo", 1);
    form.put("bar", Arrays.asList(2, 3));
    RequestTemplate template = new RequestTemplate();
    new JacksonEncoder().encode(form, new TypeReference<Map<String, ?>>() {
    }.getType(), template);
    assertThat(template).hasBody(//
    "" + //
    "{\n" + //
    "  \"foo\" : 1,\n" + //
    "  \"bar\" : [ 2, 3 ]\n" + "}");
}
Also used : RequestTemplate(feign.RequestTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 20 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project pinot by linkedin.

the class Utils method convertToMetricExpressions.

public static List<MetricExpression> convertToMetricExpressions(String metricsJson, MetricAggFunction aggFunction, String collection) throws ExecutionException {
    List<MetricExpression> metricExpressions = new ArrayList<>();
    if (metricsJson == null) {
        return metricExpressions;
    }
    ArrayList<String> metricExpressionNames;
    try {
        TypeReference<ArrayList<String>> valueTypeRef = new TypeReference<ArrayList<String>>() {
        };
        metricExpressionNames = OBJECT_MAPPER.readValue(metricsJson, valueTypeRef);
    } catch (Exception e) {
        LOG.warn("Expected json expression for metric [{}], adding as it is. Error in json parsing : [{}]", metricsJson, e.getMessage());
        metricExpressionNames = new ArrayList<>();
        String[] metrics = metricsJson.split(",");
        for (String metric : metrics) {
            metricExpressionNames.add(metric.trim());
        }
    }
    for (String metricExpressionName : metricExpressionNames) {
        String derivedMetricExpression = ThirdEyeUtils.getDerivedMetricExpression(metricExpressionName, collection);
        MetricExpression metricExpression = new MetricExpression(metricExpressionName, derivedMetricExpression, aggFunction);
        metricExpressions.add(metricExpression);
    }
    return metricExpressions;
}
Also used : ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)67 IOException (java.io.IOException)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)25 Test (org.junit.Test)23 Map (java.util.Map)13 BaseMandrillAnonymousListResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillAnonymousListResponse)7 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 List (java.util.List)5 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 AuditInfo (io.druid.audit.AuditInfo)4 Interval (org.joda.time.Interval)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)3