Example usage for com.fasterxml.jackson.core.type TypeReference TypeReference

List of usage examples for com.fasterxml.jackson.core.type TypeReference TypeReference

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.type TypeReference TypeReference.

Prototype

protected TypeReference() 

Source Link

Usage

From source file:org.springframework.social.facebook.api.impl.json.CommentListAndCountDeserializer.java

@SuppressWarnings("unchecked")
@Override//from w w  w . j a  v a  2 s.c  om
public ListAndCount<Comment> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new FacebookModule());
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode commentsNode = jp.readValueAs(JsonNode.class);
        JsonNode dataNode = commentsNode.get("data");
        List<Comment> commentsList = dataNode != null
                ? (List<Comment>) mapper.reader(new TypeReference<List<Comment>>() {
                }).readValue(dataNode)
                : Collections.<Comment>emptyList();
        JsonNode countNode = commentsNode.get("count");
        int commentCount = countNode != null ? countNode.intValue() : 0;
        return new ListAndCount<Comment>(commentsList, commentCount);
    }

    return null;
}

From source file:net.nikore.gozer.marathon.MarathonHost.java

public List<App> getAllApps() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://" + marathonHost + "/v2/apps");

    SimpleRibbonResponse response = utils.simpleRequest(request);

    Map<String, List<App>> apps = mapper.readValue(response.getContent(),
            new TypeReference<Map<String, List<App>>>() {
            });// w w w.j a v a  2 s  . c  om

    return apps.get("apps");
}

From source file:com.strandls.alchemy.inject.AlchemyModuleFilterConfigurationTest.java

/**
 * Setup expected result.//from  w w  w  .  j  a v  a2  s .  c o  m
 *
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 */
@Before
public void setup() throws JsonParseException, JsonMappingException, IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
    expectedResult = mapper.readValue(
            "{'Prod' : ['(?i).*dummy.*'], 'Test':['ToFilter1', 'ToFilter2'],"
                    + " 'All':['(?i).*dummy.*', 'ToFilter1', 'ToFilter2']}",
            new TypeReference<Map<Environment, Set<String>>>() {
            });
}

From source file:org.mayocat.attachment.store.jdbi.mapper.AttachmentMapper.java

@Override
public Attachment map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException {
    Attachment attachment = new Attachment();
    attachment.setId((UUID) resultSet.getObject("id"));
    attachment.setTitle(resultSet.getString("title"));
    attachment.setDescription(resultSet.getString("description"));
    attachment.setSlug(resultSet.getString("slug"));
    attachment.setExtension(resultSet.getString("extension"));
    attachment.setParentId((UUID) resultSet.getObject("parent_id"));

    ObjectMapper mapper = new ObjectMapper();
    if (!Strings.isNullOrEmpty(resultSet.getString("metadata"))) {
        try {//from ww  w . j a  v a 2s .co m

            Map<String, Map<String, Object>> metadata = mapper.readValue(resultSet.getString("metadata"),
                    new TypeReference<Map<String, Map<String, Object>>>() {
                    });
            attachment.setMetadata(metadata);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    if (MapperUtils.hasColumn("localization_data", resultSet)
            && !Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
        try {
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
            for (Map map : data) {
                localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            attachment.setLocalizedVersions(localizedVersions);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    return attachment;

}

From source file:kirchnerei.glatteis.json.JsonConvertServiceTest.java

@Test
public void testStringToBeanList() {
    String text = beanToString(new Bean(1, "Bean 1", TEST_DATE), new Bean(2, "Bean 2", TEST_DATE));
    List<Bean> items = service.fromString(text, new TypeReference<List<Bean>>() {
    });//from  w ww . j a  v a 2 s . c om
    assertNotNull(items);
    assertEquals(2, items.size());
}

From source file:com.flipkart.foxtrot.client.handlers.DummyEventHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    try {/*  w  w w.  j a  v  a2  s  . com*/
        BasicHttpEntityEnclosingRequest post = (BasicHttpEntityEnclosingRequest) request;
        List<Document> documents = mapper.readValue(EntityUtils.toByteArray(post.getEntity()),
                new TypeReference<List<Document>>() {
                });
        counter.addAndGet(documents.size());
        logger.info("Received {} documents.", documents.size());
    } catch (Exception e) {
        logger.error("Error: ", e);
    }
    response.setStatusCode(201);
}

From source file:org.mayocat.search.elasticsearch.AbstractGenericEntityMappingGenerator.java

@Override
public Map<String, Object> generateMapping() {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from ww w.ja  v  a  2  s.  c om*/
        try {
            String fullJson = Resources.toString(Resources.getResource(getMappingFileName(forClass())),
                    Charsets.UTF_8);

            // There is a full mapping JSON file

            Map<String, Object> mapping = mapper.readValue(fullJson, new TypeReference<Map<String, Object>>() {
            });

            // check if "addons" mapping present, and merge it in if not
            if (!hasAddonsMapping(mapping)) {
                insertAddonsMapping(mapping);
            }

            return mapping;
        } catch (IllegalArgumentException e) {

            // This means the full mapping has not been found
            try {
                String propertiesJson = Resources.toString(
                        Resources.getResource(getPropertiesMappingFileName(forClass())), Charsets.UTF_8);

                // There is a mapping just for properties
                final Map<String, Object> properties = mapper.readValue(propertiesJson,
                        new TypeReference<Map<String, Object>>() {
                        });

                Map<String, Object> mapping = new HashMap<String, Object>();

                Map<String, Object> entity = new HashMap<String, Object>();
                Map<String, Object> entityProperties = new HashMap<String, Object>() {
                    {
                        // the "properties" property of the properties of the product object
                        put("properties", new HashMap<String, Object>() {
                            {
                                // the "properties" of the property "properties"
                                // of the properties of the product object
                                put("properties", properties);
                            }
                        });
                    }
                };

                if (Slug.class.isAssignableFrom(this.forClass())) {
                    entityProperties.put("slug", new HashMap<String, Object>() {
                        {
                            put("index", "not_analyzed");
                            put("type", "string");
                        }
                    });
                }

                entity.put("properties", entityProperties);
                mapping.put(getEntityName(forClass()), entity);

                insertAddonsMapping(mapping);

                return mapping;
            } catch (IllegalArgumentException e1) {

                // There is no mapping at all.
                return null;
            }
        }
    } catch (IOException e1) {
        return null;
    }
}

From source file:com.github.philippn.yqltables.tests.YahooFinanceComponentsTest.java

@Test
public void testDax() throws Exception {
    YqlQueryBuilder builder = YqlQueryBuilder.fromQueryString(
            "use '" + TABLE_DEFINTION + "' as mytable; select * from mytable where symbol=@symbol");

    builder.withVariable("symbol", "^GDAXI");
    YqlResult result = client.query(builder.build());
    String[] components = result.getContentAsMappedObject(new TypeReference<QueryResultType<String[]>>() {
    }).getResults();//from   w  w w .  j a  v a 2s  .  c om
    assertEquals(30, components.length);
    assertTrue(asList(components).contains("ALV.DE"));
}

From source file:com.networknt.light.rule.post.GetRecentPostRule.java

public boolean execute(Object... objects) throws Exception {
    Map<String, Object> inputMap = (Map<String, Object>) objects[0];
    Map<String, Object> data = (Map<String, Object>) inputMap.get("data");
    if (data.get("host") == null || data.get("limitTo") == null) {
        inputMap.put("result", "Host and limitTo are required");
        inputMap.put("responseCode", 404);
        return false;
    } else {//www .  j a v a  2 s.c  o  m

        // get recent post for blog

        // get recent post for news

        // get recent post for forum

    }

    long total = DbService.getCount("Post", data);
    if (total > 0) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", total);
        String posts = DbService.getData("Post", data);
        List<Map<String, Object>> jsonList = mapper.readValue(posts,
                new TypeReference<List<HashMap<String, Object>>>() {
                });
        result.put("posts", jsonList);
        inputMap.put("result", mapper.writeValueAsString(result));
        return true;
    } else {
        inputMap.put("result", "No post can be found.");
        inputMap.put("responseCode", 404);
        return false;
    }
}

From source file:com.evrythng.java.wrapper.service.UrlBindingService.java

/**
 * Reads url bindings./*from   w w  w.j a va2  s  .  c o  m*/
 *
 * @return a preconfigured {@link Builder}.
 */
public Builder<List<UrlBinding>> bindingsReader() throws EvrythngClientException {

    return get(PATH_URLS, new TypeReference<List<UrlBinding>>() {

    });
}