Search in sources :

Example 6 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project cytoscape-impl by cytoscape.

the class PropertiesJsonDeserializer method readValue.

public static Object readValue(final String key, final String input, final ObjectMapper mapper, final AbstractCustomGraphics2<?> cg2) {
    Object value = null;
    final Class<?> type = cg2.getSettingType(key);
    if (type != null) {
        final TypeFactory typeFactory = mapper.getTypeFactory();
        try {
            if (type == Array.class) {
                final Class<?> elementType = cg2.getSettingElementType(key);
                if (elementType != null) {
                    final ArrayType arrType = typeFactory.constructArrayType(elementType);
                    if (mapper.canDeserialize(arrType))
                        value = mapper.readValue(input, arrType);
                }
            } else if (List.class.isAssignableFrom(type)) {
                final Class<?> elementType = cg2.getSettingElementType(key);
                if (elementType != null) {
                    final CollectionType collType = typeFactory.constructCollectionType(List.class, elementType);
                    if (mapper.canDeserialize(collType))
                        value = mapper.readValue(input, collType);
                }
            } else {
                final JavaType simpleType = typeFactory.constructSimpleType(type, new JavaType[] {});
                if (mapper.canDeserialize(simpleType))
                    value = mapper.readValue(input, simpleType);
            }
        } catch (Exception e) {
            logger.error("Cannot parse JSON field " + key, e);
        }
    }
    return value;
}
Also used : ArrayType(com.fasterxml.jackson.databind.type.ArrayType) JavaType(com.fasterxml.jackson.databind.JavaType) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) List(java.util.List) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project syndesis by syndesisio.

the class JsonDbDao method fetchAll.

@Override
@SuppressWarnings({ "unchecked", "PMD.CyclomaticComplexity" })
public ListResult<T> fetchAll(Function<ListResult<T>, ListResult<T>>... operators) {
    try {
        GetOptions options = new GetOptions();
        // Try to convert operators to equivalent DB queries.
        if (operators != null) {
            for (int i = 0; i < operators.length; i++) {
                Function<ListResult<T>, ListResult<T>> operator = operators[i];
                if (operator.getClass() == IdPrefixFilter.class) {
                    IdPrefixFilter<T> filter = (IdPrefixFilter<T>) operator;
                    options.startAt(":" + filter.getPrefix());
                    options.endAt(":" + filter.getPrefix());
                    // Take it out of the list.
                    operators[i] = null;
                }
            }
        }
        // get the data out..
        byte[] json = jsondb.getAsByteArray(getCollectionPath(), options);
        ListResult<T> result;
        if (json != null && json.length > 0) {
            // Lets use jackson to parse the map of keys to our model instances
            ObjectReader reader = Json.reader();
            TypeFactory typeFactory = reader.getTypeFactory();
            MapType mapType = typeFactory.constructMapType(LinkedHashMap.class, String.class, getType());
            LinkedHashMap<String, T> map = reader.forType(mapType).readValue(json);
            result = ListResult.of(map.values());
        } else {
            result = ListResult.of(Collections.<T>emptyList());
        }
        if (operators == null) {
            return result;
        }
        for (Function<ListResult<T>, ListResult<T>> operator : operators) {
            if (operator != null) {
                result = operator.apply(result);
            }
        }
        return result;
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException | IOException e) {
        throw SyndesisServerException.launderThrowable(e);
    }
}
Also used : IOException(java.io.IOException) GetOptions(io.syndesis.server.jsondb.GetOptions) MapType(com.fasterxml.jackson.databind.type.MapType) ListResult(io.syndesis.common.model.ListResult) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IdPrefixFilter(io.syndesis.server.dao.manager.operators.IdPrefixFilter)

Example 8 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project openlmis-stockmanagement by OpenLMIS.

the class BaseCommunicationService method getMap.

protected <K, V> Map<K, V> getMap(String resourceUrl, RequestParameters parameters, Class<K> keyType, Class<V> valueType) {
    String url = getServiceUrl() + getUrl() + StringUtils.defaultIfBlank(resourceUrl, "");
    TypeFactory factory = objectMapper.getTypeFactory();
    MapType mapType = factory.constructMapType(HashMap.class, keyType, valueType);
    HttpEntity<Object> entity = createEntity();
    List<Map<K, V>> maps = new ArrayList<>();
    for (URI uri : RequestHelper.splitRequest(url, parameters, maxUrlLength)) {
        ResponseEntity<Map> response = restTemplate.exchange(uri, HttpMethod.GET, entity, Map.class);
        Map<K, V> map = objectMapper.convertValue(response.getBody(), mapType);
        maps.add(map);
    }
    return Merger.ofMaps(maps).withDefaultValue(Collections::emptyMap).merge();
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) MapType(com.fasterxml.jackson.databind.type.MapType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project openlmis-stockmanagement by OpenLMIS.

the class OrderableFulfillReferenceDataServiceTest method setUp.

@Before
public void setUp() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    TypeFactory factory = mapper.getTypeFactory();
    when(objectMapper.getTypeFactory()).thenReturn(factory);
    when(authService.obtainAccessToken()).thenReturn("token");
    ReflectionTestUtils.setField(service, "restTemplate", restTemplate);
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 10 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project carina by qaprosoft.

the class JsonUtils method fromJson.

public static <T> T fromJson(File file, Type type) {
    try {
        TypeFactory tf = mapper.getTypeFactory();
        JavaType javaType = tf.constructType(type);
        return mapper.readValue(file, javaType);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Aggregations

TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)34 JavaType (com.fasterxml.jackson.databind.JavaType)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 IOException (java.io.IOException)8 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)4 MapType (com.fasterxml.jackson.databind.type.MapType)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 MetricsModule (com.codahale.metrics.json.MetricsModule)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 ArrayType (com.fasterxml.jackson.databind.type.ArrayType)2 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)2 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)2 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)2 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)2 Query (io.druid.query.Query)2