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:com.zxy.commons.json.JsonUtils.java

/**
 * json?List?//w  w w  .  j  a va  2 s  .  com
 * 
 * @param <T> This is the type parameter
 * @param include include
 * @param listJsonString list?json
 * @param clazz 
 * @return ?
 */
public static <T> List<T> toList(JsonInclude.Include include, String listJsonString, Class<T> clazz) {
    try {
        ObjectMapper mapper = getObjectMapper(include);
        return mapper.readValue(listJsonString,
                TypeFactory.defaultInstance().constructCollectionType(List.class, clazz));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hp.autonomy.hod.client.config.ResponseParserTest.java

@Test
public void parseResponseToJavaTypeWithoutRefreshedToken() throws IOException {
    final ResponseAndBody responseAndBody = createTestResponse();

    final TokenProxy<EntityType.Application, TokenType.Simple> tokenProxy = new TokenProxy<>(
            EntityType.Application.INSTANCE, TokenType.Simple.INSTANCE);
    final Object expectedReturnValue = new Object();

    final JavaType objectType = TypeFactory.defaultInstance().uncheckedSimpleType(Object.class);
    when(objectMapper.readValue(eq(responseAndBody.body), eq(objectType))).thenReturn(expectedReturnValue);

    final Object returnValue = responseParser.unsafeParseResponse(tokenProxy, objectType,
            responseAndBody.response);//from w w  w .ja va 2 s . com

    verify(tokenRepository, never()).update(isA(TokenProxy.class), isA(AuthenticationToken.class));
    assertThat(returnValue, is(expectedReturnValue));
}

From source file:io.fabric8.jolokia.facade.FabricMBeanFacadeTest.java

@Test
public void testContainersWithFields() {
    // this can only be run if you have a fabric running...
    Assume.assumeTrue(Boolean.valueOf(System.getProperty("hasFabric")));

    FabricMBean facade = getFabricMBean();

    String json = facade.containers(Helpers.toList("id"));

    try {//from ww w .j  a v  a2 s  . c  om
        Collection<ContainerDTO> containers = Helpers.getObjectMapper().readValue(json,
                TypeFactory.defaultInstance().constructParametricType(Collection.class, ContainerDTO.class));
        Assume.assumeNotNull(containers);
        for (ContainerDTO container : containers) {
            System.out.println(container);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.airavata.db.AbstractThriftDeserializer.java

/**
 * Generates a {@link JavaType} that matches the target Thrift field represented by the provided
 * {@code <E>} enumerated value.  If the field's type includes generics, the generics will
 * be added to the generated {@link JavaType} to support proper conversion.
 * @param thriftInstance The Thrift-generated class instance that will be converted to/from JSON.
 * @param field A {@code <E>} enumerated value that represents a field in a Thrift-based entity.
 * @return The {@link JavaType} representation of the type associated with the field.
 * @throws NoSuchFieldException if unable to determine the field's type.
 * @throws SecurityException if unable to determine the field's type.
 *//*from w w  w.  j a va 2 s  .  c o m*/
protected JavaType generateValueType(final T thriftInstance, final E field)
        throws NoSuchFieldException, SecurityException {
    final TypeFactory typeFactory = TypeFactory.defaultInstance();

    final Field declaredField = thriftInstance.getClass().getDeclaredField(field.getFieldName());
    if (declaredField.getType().equals(declaredField.getGenericType())) {
        log.debug("Generating JavaType for type '{}'.", declaredField.getType());
        return typeFactory.constructType(declaredField.getType());
    } else {
        final ParameterizedType type = (ParameterizedType) declaredField.getGenericType();
        final Class<?>[] parameterizedTypes = new Class<?>[type.getActualTypeArguments().length];
        for (int i = 0; i < type.getActualTypeArguments().length; i++) {
            parameterizedTypes[i] = (Class<?>) type.getActualTypeArguments()[i];
        }
        log.debug("Generating JavaType for type '{}' with generics '{}'", declaredField.getType(),
                parameterizedTypes);
        return typeFactory.constructParametricType(declaredField.getType(), parameterizedTypes);
    }
}

From source file:com.palantir.typescript.services.language.LanguageEndpoint.java

public List<TextSpan> getBraceMatchingAtPosition(String serviceKey, String fileName, int position) {
    checkNotNull(serviceKey);/*from   w ww  . j  a va  2s.c om*/
    checkNotNull(fileName);
    checkArgument(position >= 0);

    Request request = new Request(SERVICE, "getBraceMatchingAtPosition", serviceKey, fileName, position);
    CollectionType resultType = TypeFactory.defaultInstance().constructCollectionType(List.class,
            TextSpan.class);
    return this.bridge.call(request, resultType);
}

From source file:io.fabric8.jolokia.facade.FabricMBeanFacadeTest.java

@Test
public void testGetProfiles() {
    // this can only be run if you have a fabric running...
    Assume.assumeTrue(Boolean.valueOf(System.getProperty("hasFabric")));

    FabricMBean facade = getFabricMBean();

    String json = facade.getProfiles("1.0");

    try {// w ww  .j a  va2s  .c o  m
        Collection<ProfileDTO> profiles = Helpers.getObjectMapper().readValue(json,
                TypeFactory.defaultInstance().constructParametricType(Collection.class, ProfileDTO.class));
        Assume.assumeNotNull(profiles);
        for (ProfileDTO profile : profiles) {
            System.out.println(profile);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * json?Set?/*from   ww  w . j  a va 2s .  c o  m*/
 * 
 * @param <T> This is the type parameter
 * @param include include
 * @param setJsonString set?json
 * @param clazz 
 * @return ?
 */
@SuppressWarnings("PMD.LooseCoupling")
public static <T> Set<T> toSet(JsonInclude.Include include, String setJsonString, Class<T> clazz) {
    try {
        ObjectMapper mapper = getObjectMapper(include);
        return mapper.readValue(setJsonString,
                TypeFactory.defaultInstance().constructCollectionType(LinkedHashSet.class, clazz));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static Map<String, ReferenceDataPropertiesDto> getReferenceDataSettings(String referenceData)
        throws ServiceException {
    if (referenceData == null) {
        return null;
    }//  ww  w.ja  v  a2s  .co m
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        Object obj = mapper.readValue(referenceData, Map.class);
        String jsonString = mapper.writeValueAsString(obj);
        return mapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class,
                String.class, ReferenceDataPropertiesDto.class));

    } catch (IOException e) {
        throw new ServiceException("Parse Exception from Json to Object", e);
    }
}

From source file:com.palantir.typescript.services.language.LanguageEndpoint.java

public List<DefinitionInfo> getDefinitionAtPosition(String serviceKey, String fileName, int position) {
    checkNotNull(serviceKey);//from w  w w .  j a  v a  2  s. c  o m
    checkNotNull(fileName);
    checkArgument(position >= 0);

    Request request = new Request(SERVICE, "getDefinitionAtPosition", serviceKey, fileName, position);
    CollectionType resultType = TypeFactory.defaultInstance().constructCollectionType(List.class,
            DefinitionInfo.class);
    return this.bridge.call(request, resultType);
}