use of com.fasterxml.jackson.databind.JavaType in project ratpack by ratpack.
the class DefaultConfigData method getAsConfigObject.
@Override
public <O> ConfigObject<O> getAsConfigObject(String pointer, TypeToken<O> type) {
JsonNode node = pointer != null ? rootNode.at(pointer) : rootNode;
if (node.isMissingNode()) {
node = emptyNode;
}
try {
JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType());
O value = objectMapper.readValue(new TreeTraversingParser(node, objectMapper), javaType);
return new DefaultConfigObject<>(pointer, type, value);
} catch (IOException ex) {
throw Exceptions.uncheck(ex);
}
}
use of com.fasterxml.jackson.databind.JavaType in project Gaffer by gchq.
the class GraphConfigurationService method getSerialisedFields.
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<String> getSerialisedFields(final String className) {
final Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Set<String> fields = new HashSet<>();
for (final BeanPropertyDefinition property : properties) {
fields.add(property.getName());
}
return fields;
}
use of com.fasterxml.jackson.databind.JavaType in project alfresco-remote-api by Alfresco.
the class SerializerOfExecutionResult method serialize.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void serialize(ExecutionResult value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
SerializationConfig config = provider.getConfig();
Object rootObj = value.getRoot();
if (rootObj == null) {
provider.getDefaultNullValueSerializer().serialize(null, jgen, provider);
} else {
Class<?> cls = rootObj.getClass();
// create an untyped map, add the contents of the root + the embeds.
Map toBeSerialized = new HashMap();
BeanPropertiesFilter filter = value.getFilter();
if (filter == null)
filter = BeanPropertiesFilter.ALLOW_ALL;
if (Map.class.isAssignableFrom(cls)) {
// Its a map so
Map rootAsaMap = (Map) rootObj;
toBeSerialized.putAll(rootAsaMap);
} else {
JavaType classType = config.constructType(cls);
BeanDescription beanDesc = provider.getConfig().introspect(classType);
List<BeanPropertyDefinition> props = beanDesc.findProperties();
for (BeanPropertyDefinition beanProperty : props) {
if (beanProperty.couldSerialize() && filter.isAllowed(beanProperty.getName())) {
Object propertyValue = ResourceInspectorUtil.invokeMethod(beanProperty.getGetter().getAnnotated(), rootObj);
if (propertyValue != null) {
if ((propertyValue instanceof String)) {
if (((String) propertyValue).trim().length() > 0) {
toBeSerialized.put(beanProperty.getName(), propertyValue);
}
} else {
toBeSerialized.put(beanProperty.getName(), propertyValue);
}
}
}
}
}
// Add embedded
for (Entry<String, Object> embedded : value.getEmbedded().entrySet()) {
if (filter == null || filter.isAllowed(embedded.getKey())) {
toBeSerialized.put(embedded.getKey(), embedded.getValue());
}
}
// if its an embedded entity then render the properties (not as an "entry:")
if (value.isAnEmbeddedEntity()) {
jgen.writeObject(toBeSerialized);
} else {
jgen.writeStartObject();
jgen.writeObjectField("entry", toBeSerialized);
if (value.getRelated() != null && !value.getRelated().isEmpty()) {
jgen.writeObjectField("relations", value.getRelated());
}
jgen.writeEndObject();
}
}
}
use of com.fasterxml.jackson.databind.JavaType in project leopard by tanhaichao.
the class UnderlineJson method toEnumListByOnlyKey.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> List<T> toEnumListByOnlyKey(String json, Class<T> clazz) {
JavaType javaType = getObjectMapper().getTypeFactory().constructParametrizedType(ArrayList.class, List.class, Object.class);
List<Object> keyList;
try {
keyList = getObjectMapper().readValue(json, javaType);
} catch (Exception e) {
logger.error("clazz:" + clazz.getName() + " json:" + json);
throw new JsonException(e.getMessage(), e);
}
// System.err.println("keyList:" + keyList);
List<T> list = new ArrayList<T>();
for (Object key : keyList) {
T onum = (T) EnumUtil.toEnum(key, (Class<Enum>) clazz);
// System.err.println("key:" + key + " onum:" + onum);
list.add(onum);
}
return list;
}
use of com.fasterxml.jackson.databind.JavaType in project rskj by rsksmart.
the class LocalCryptoTest method testAllInCryptoSute.
@Test
public void testAllInCryptoSute() throws ParseException, IOException {
String json = getJSON("crypto");
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, CryptoTestCase.class);
HashMap<String, CryptoTestCase> testSuite = mapper.readValue(json, type);
for (String key : testSuite.keySet()) {
System.out.println("executing: " + key);
testSuite.get(key).execute();
}
}
Aggregations