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;
}
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);
}
}
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();
}
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);
}
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);
}
}
Aggregations