Example usage for com.fasterxml.jackson.databind.type TypeFactory defaultInstance

List of usage examples for com.fasterxml.jackson.databind.type TypeFactory defaultInstance

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.type TypeFactory defaultInstance.

Prototype

public static TypeFactory defaultInstance() 

Source Link

Usage

From source file:org.roda.core.data.utils.JsonUtils.java

public static <T> List<T> getListFromJson(String json, Class<T> objectClass) throws GenericException {
    try {/* w  w  w . j a va  2 s  . c  o  m*/
        ObjectMapper mapper = new ObjectMapper(new JsonFactory());
        TypeFactory t = TypeFactory.defaultInstance();
        return mapper.readValue(json, t.constructCollectionType(ArrayList.class, objectClass));
    } catch (IOException e) {
        throw new GenericException(JSON_ERROR_MESSAGE, e);
    }
}

From source file:net.turnbig.jdbcx.utilities.JsonMapper.java

public <K, IK, IV> HashMap<K, Map<IK, IV>> getMapBean(String jsonString, Class<K> keyClazz,
        Class<IK> innerKeyClazz, Class<IV> innerValueClazz) {
    try {/*  ww w. j ava2  s.  c  om*/
        JavaType innerType = TypeFactory.defaultInstance().constructMapType(HashMap.class, innerKeyClazz,
                innerValueClazz);
        JavaType keyType = TypeFactory.defaultInstance().constructType(keyClazz);
        JavaType typeRef = TypeFactory.defaultInstance().constructMapType(HashMap.class, keyType, innerType);
        return mapper.readValue(jsonString, typeRef);
    } catch (IOException e) {
        logger.warn("convert string to map bean.", e);
        return null;
    }
}

From source file:org.agorava.facebook.impl.GraphApiImpl.java

@SuppressWarnings("unchecked")
private <T> List<T> deserializeDataList(JsonNode jsonNode, final Class<T> elementType) {
    try {/*from www.j a v  a 2s .c om*/
        CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class,
                elementType);
        return (List<T>) objectMapper.readValue(jsonNode.textValue(), listType);
    } catch (IOException e) {
        throw new AgoravaException("Error deserializing data from Facebook: " + e.getMessage(), e);
    }
}

From source file:springfox.documentation.schema.property.bean.BeanModelPropertyProvider.java

private BeanDescription beanDescription(ResolvedType type, ModelContext context) {
    if (context.isReturnType()) {
        SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
        return serializationConfig
                .introspect(TypeFactory.defaultInstance().constructType(type.getErasedType()));
    } else {//from  w  w  w . jav  a2  s  .c  o m
        DeserializationConfig serializationConfig = objectMapper.getDeserializationConfig();
        return serializationConfig
                .introspect(TypeFactory.defaultInstance().constructType(type.getErasedType()));
    }
}

From source file:com.xiongyingqi.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java

protected JavaType getJavaType(Class<?> clazz) {
    return TypeFactory.defaultInstance().constructRawMapType((Class<? extends Map>) clazz);
}

From source file:com.github.yongchristophertang.engine.web.response.JsonResultTransformer.java

/**
 * Transform the json result to a map with K as its key class and V as its value class.
 *
 * @param keyClass   the type class of transformed map key object
 * @param valueClass the type class of transformed map value object
 *//*w ww. ja  v  a  2  s  .c o  m*/
public <K, V> ResultTransform<Map<K, V>> map(Class<K> keyClass, Class<V> valueClass) {
    return result -> {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return expression == null
                ? mapper.readValue(result.getResponseStringContent(),
                        TypeFactory.defaultInstance().constructMapLikeType(Map.class, keyClass, valueClass))
                : mapper.readValue(
                        JsonPath.compile(expression).read(result.getResponseStringContent()).toString(),
                        TypeFactory.defaultInstance().constructMapLikeType(Map.class, keyClass, valueClass));
    };
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint.java

public void processResult(JsonNode response) throws NoSuchMethodException {

    logger.trace("Response : {}", response.toString());
    CallContext returnCtxt = methodContext.get(response.get("id").asText());
    if (returnCtxt == null) {
        return;//from  ww w .j  a  v a 2s.co m
    }

    if (ListenableFuture.class == returnCtxt.getMethod().getReturnType()) {
        TypeToken<?> retType = TypeToken.of(returnCtxt.getMethod().getGenericReturnType())
                .resolveType(ListenableFuture.class.getMethod("get").getGenericReturnType());
        JavaType javaType = TypeFactory.defaultInstance().constructType(retType.getType());

        JsonNode result = response.get("result");
        Object result1 = objectMapper.convertValue(result, javaType);
        JsonNode error = response.get("error");
        if (error != null && !error.isNull()) {
            logger.error("Error : {}", error.toString());
        }

        returnCtxt.getFuture().set(result1);

    } else {
        throw new UnexpectedResultException("Don't know how to handle this");
    }
}

From source file:org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapperTest.java

@Test
public void shouldLookInTheContentClassIdFieldNameToFindTheContainerClassIDWhenClassIdIsContainerType() {
    properties.getHeaders().put("contentType", "java.lang.String");
    properties.getHeaders().put(javaTypeMapper.getClassIdFieldName(), ArrayList.class.getName());
    given(javaTypeMapper.getContentClassIdFieldName()).willReturn("contentType");

    JavaType javaType = javaTypeMapper.toJavaType(properties);

    assertThat((CollectionType) javaType,
            equalTo(TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, String.class)));
}

From source file:com.xively.client.http.util.ParserUtil.java

public static <T extends DomainObject> Collection<T> toConnectedObjects(String body, Class elementType) {
    log.debug(String.format("Parsing string to objects: %s", body));

    Collection<T> objs;/*from   w  w  w.j a  v a2s.co m*/
    CollectionType collectionType = TypeFactory.defaultInstance().constructCollectionType(ArrayList.class,
            elementType);

    try {
        JsonNode node = getObjectMapper().reader().readTree(body);
        JsonNode total = node.get("totalResults");
        if (total != null) {
            JsonNode results = node.get("results");
            objs = getObjectMapper().readValue(results.toString(), collectionType);
            log.debug(String.format("Parsed string to %s objects", total));
        } else {
            objs = getObjectMapper().readValue(body, collectionType);
        }
    } catch (IOException e) {
        throw new ParseToObjectException(String.format("Unable to parse [%s] to %s.", body, elementType), e);
    }

    return objs;
}

From source file:com.zxy.commons.json.JsonUtils.java

/**
 * json?//from   www .jav a 2  s .  c om
 * 
 * @param <T> This is the type parameter
 * @param include include
 * @param jsonString json
 * @param clazz 
 * @return 
 */
public static <T> T toObject(JsonInclude.Include include, String jsonString, Class<T> clazz) {
    try {
        ObjectMapper mapper = getObjectMapper(include);
        return mapper.readValue(jsonString, TypeFactory.defaultInstance().constructType(clazz));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}