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

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

Introduction

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

Prototype

public Type getType() 

Source Link

Usage

From source file:com.ysheng.auth.common.restful.util.JsonSerializer.java

/**
 * Deserializes a JSON string to an object.
 *
 * @param serialized The JSON string that contains the serialized object.
 * @param typeReference The type reference of the object.
 * @param <T> The type of the object.
 * @return An object deserialized from the string.
 * @throws IOException The error that contains detail information.
 */// w ww .j a  va 2s .  c  o  m
public static <T> T deserialize(HttpEntity serialized, TypeReference<T> typeReference) throws IOException {
    if (typeReference.getType() == Void.TYPE) {
        return null;
    }

    return objectMapper.readValue(serialized.getContent(), typeReference);
}

From source file:com.microsoft.alm.client.utils.JsonHelper.java

private static <T> boolean isArrayType(final TypeReference<T> genericType) {

    final Type type = genericType.getType();

    if (genericType.getType() instanceof ParameterizedType) {
        final Type rawType = ((ParameterizedType) genericType.getType()).getRawType();
        return (rawType == ArrayList.class) || (rawType == List.class);
    }/*from w ww  .j a va2 s  .  c o m*/

    return false;
}

From source file:com.nesscomputing.jackson.JacksonSerializerBinder.java

@SuppressWarnings("unchecked")
public static <F, T> Key<Function<F, T>> keyFor(TypeReference<F> from, TypeReference<T> to,
        Class<? extends Annotation> annotation) {
    return (Key<Function<F, T>>) Key
            .get(Types.newParameterizedType(Function.class, from.getType(), to.getType()), annotation);
}

From source file:com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestAnnotater.java

private static boolean stringTypeReference(TypeReference typeReference) {
    if (typeReference.getType() == null || typeReference.getType().getTypeName() == null) {
        log.warn("Malformed type reference {}", typeReference);
        return false;
    }//from   www  .  j  a v a  2s .  c o m

    return typeReference.getType().getTypeName().equals(String.class.getName());
}

From source file:com.github.parisoft.resty.utils.JacksonUtils.java

public static <T> T read(String content, TypeReference<T> reference, MediaType contentType) throws IOException {
    for (ProviderBase<?, ?, ?, ?> provider : DataProcessors.getInstance().values()) {
        if (provider.isReadable(reference.getClass(), reference.getType(), null, contentType)) {
            return provider.locateMapper(reference.getClass(), contentType).readValue(content, reference);
        }// w  w w .  ja  v a  2s.c o m
    }

    throw new IOException(String.format("no processors found for type=%s and Content-Type=%s",
            reference.getType(), contentType));
}

From source file:com.github.parisoft.resty.utils.JacksonUtils.java

public static <T> T read(HttpEntity entity, TypeReference<T> reference, MediaType contentType)
        throws IOException {
    for (ProviderBase<?, ?, ?, ?> provider : DataProcessors.getInstance().values()) {
        if (provider.isReadable(reference.getClass(), reference.getType(), null, contentType)) {
            return provider.locateMapper(reference.getClass(), contentType).readValue(entity.getContent(),
                    reference);/*ww  w . ja  v  a 2 s .  c  o m*/
        }
    }

    throw new IOException(String.format("no processors found for type=%s and Content-Type=%s",
            reference.getType(), contentType));
}

From source file:jp.furplag.util.commons.ObjectUtils.java

/**
 * substitute for {@link java.lang.Class#newInstance()}.
 *
 * @param typeRef {@link com.fasterxml.jackson.core.type.TypeReference}.
 * @return empty instance of specified {@link java.lang.Class}.
 * @throws InstantiationException// w  w  w. ja v  a 2 s .  c  o  m
 * @throws IllegalAccessException
 * @throws NegativeArraySizeException
 * @throws ClassNotFoundException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws SecurityException
 */
@SuppressWarnings("unchecked")
public static <T> T newInstance(TypeReference<T> typeRef) throws InstantiationException {
    if (typeRef == null)
        return null;
    Type type = typeRef.getType();
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        return (T) Array.newInstance((Class<?>) componentType, 0);
    }
    if (type instanceof ParameterizedType) {
        Type rawType = ((ParameterizedType) type).getRawType();
        Class<?> clazz = (Class<?>) rawType;
        if (clazz.isInterface()) {
            if (List.class.isAssignableFrom(clazz))
                return (T) Lists.newArrayList();
            if (Map.class.isAssignableFrom(clazz))
                return (T) Maps.newHashMap();
            if (Set.class.isAssignableFrom(clazz))
                return (T) Sets.newHashSet();
        }
        try {
            return ((Class<T>) rawType).newInstance();
        } catch (IllegalAccessException e) {
        }

        throw new InstantiationException("could not create instance, the default constructor of \""
                + ((Class<T>) rawType).getName() + "()\" is not accessible ( or undefined ).");
    }
    if (type instanceof Class)
        return newInstance((Class<T>) type);

    return null;
}

From source file:com.evrythng.java.wrapper.core.api.Utils.java

/**
 * Converts http response into entity/*from   www  .j  a va 2s  .c o m*/
 *
 * @param response {@link HttpResponse} instance
 * @param type     {@link TypeReference} instance
 * @param <K>      entity type
 * @return entity
 */
@SuppressWarnings("unchecked")
public static <K> K convert(final HttpResponse response, final TypeReference<K> type) throws EvrythngException {

    K result;

    LOGGER.debug("Performing conversion: [type={}]", type.getType());
    if (type.getType().equals(Void.class)) {
        return null;
    }
    if (type.getType().equals(InputStream.class)) {
        try {
            // Retrieve response content stream and buffer data as connection may be closed before input is handled:
            result = (K) IOUtils.toBufferedInputStream(entityStream(response));
        } catch (Exception e) {
            throw new EvrythngClientException(
                    String.format("Unable to retrieve response content stream: [type=%s, cause=%s]",
                            type.getType(), e.getMessage()),
                    e);
        }
    } else {
        if (type.getType().equals(HttpResponse.class)) {
            // We already have a HttpResponse, let's return it:
            result = (K) response;
        } else {
            // Retrieve response entity (as String so that it can be outputted in case of exception):
            String entity = entityString(response);

            if (type.getType().equals(String.class)) {
                result = (K) entity;
            } else {
                try {
                    result = JSONUtils.read(entity, type);
                } catch (Exception e) {
                    throw new EvrythngClientException(
                            String.format("Unable to map response entity: [type=%s, entity=%s, cause=%s]",
                                    type.getType(), entity, e.getMessage()),
                            e);
                }
            }
        }
    }
    return result;
}

From source file:com.microsoft.alm.client.utils.JsonHelper.java

public static <T> T deserializeResponce(final HttpMethodBase response, final TypeReference<T> genericType) {
    try {/*w w  w . j  a  v  a 2s. co m*/
        final byte[] input = response.getResponseBody();

        if (input == null || input.length == 0) {
            return null;
        } else {
            if (isArrayType(genericType)) {
                final JavaType rootType = objectMapper.getTypeFactory().constructParametricType(
                        VssJsonCollectionWrapper.class, objectMapper.constructType(genericType.getType()));

                final VssJsonCollectionWrapper<T> result = objectMapper.readValue(input, rootType);
                return result.getValue();
            } else {
                return objectMapper.readValue(input, genericType);
            }
        }
    } catch (final IOException e) {
        log.error(e.getMessage(), e);
        throw new VssServiceException(e.getMessage(), e);
    } finally {
        response.releaseConnection();
    }
}

From source file:com.basho.riak.client.api.convert.JSONConverter.java

public JSONConverter(TypeReference<T> typeReference) {
    super(typeReference.getType());
    this.typeReference = typeReference;
}