use of com.fasterxml.jackson.databind.annotation.JsonDeserialize in project jsonschema2pojo by joelittlejohn.
the class MediaIT method shouldCreateByteArrayFieldWithAnyEncoding.
@Test
public void shouldCreateByteArrayFieldWithAnyEncoding() throws SecurityException, NoSuchFieldException {
Field field = classWithMediaProperties.getDeclaredField("anyBinaryEncoding");
JsonSerialize serAnnotation = field.getAnnotation(JsonSerialize.class);
JsonDeserialize deserAnnotation = field.getAnnotation(JsonDeserialize.class);
assertThat("any binary encoding field has type byte[]", field.getType(), equalToType(BYTE_ARRAY));
assertThat("any binary encoding has a serializer", serAnnotation, notNullValue());
assertThat("any binary encoding has a deserializer", deserAnnotation, notNullValue());
}
use of com.fasterxml.jackson.databind.annotation.JsonDeserialize in project kylo by Teradata.
the class AnnotationFieldNameResolver method getProperties.
/**
* Walk a class and obtain {@link AnnotatedFieldProperty} objects matching any fields with the {@link #annotation} supplied
*
* @param clazz the class to inspect and parse annotations
* @return a list of objects describing the annotated fields
*/
public List<AnnotatedFieldProperty> getProperties(Class clazz) {
processedClasses.add(clazz);
classPropertyFields.put(clazz, new HashSet<AnnotatedFieldProperty>());
List<AnnotatedFieldProperty> names = new ArrayList<>();
List<Field> fields = FieldUtils.getFieldsListWithAnnotation(clazz, annotation);
List<Field> allFields = FieldUtils.getAllFieldsList(clazz);
for (Field field : fields) {
AnnotatedFieldProperty p = addFieldProperty(clazz, names, field);
classPropertyFields.get(clazz).add(p);
Class fieldType = field.getType();
if (!processedClasses.contains(fieldType)) {
names.addAll(getProperties(fieldType));
}
}
for (Field field : allFields) {
Class fieldType = field.getType();
if (!processedClasses.contains(fieldType)) {
stack.push(field.getName());
names.addAll(getProperties(fieldType));
// check to see if field is annotated with deserialize
JsonDeserialize deserialize = field.getAnnotation(JsonDeserialize.class);
if (deserialize != null) {
Class<?> deserializeClass = deserialize.as();
if (!processedClasses.contains(deserializeClass)) {
names.addAll(getProperties(deserializeClass));
}
}
stack.pop();
} else if (classPropertyFields.containsKey(fieldType)) {
stack.push(field.getName());
for (AnnotatedFieldProperty prop : classPropertyFields.get(fieldType)) {
addFieldProperty(clazz, names, prop.getField());
}
// check to see if field is annotated with deserialize
JsonDeserialize deserialize = field.getAnnotation(JsonDeserialize.class);
if (deserialize != null) {
Class<?> deserializeClass = deserialize.as();
if (classPropertyFields.containsKey(deserializeClass)) {
for (AnnotatedFieldProperty prop : classPropertyFields.get(deserializeClass)) {
addFieldProperty(clazz, names, prop.getField());
}
}
}
stack.pop();
}
}
return names;
}
use of com.fasterxml.jackson.databind.annotation.JsonDeserialize in project AJSC by att.
the class Unmarshaller method determineImplementationClass.
/**
* This method checks the specified bean property to locate the implementation class to be used. If the
* implementation class supports polymorphic mappings, then the implementation class is further inspected for
* JsonTypeInfo annotations. If the annotation is present, then the annotation directs the processing. Because the
* processing of the annotation may be data-dependent, the mapped data to be unmarshalled must be provided as well.
*
* @param beanProperty
* The bean property we are mapping the contents into
* @param obj
* The object that contains the mappings
* @return The class to be constructed to contain the mappings
* @throws UnmarshallException
* If the json type info uses unsupported metadata determination
*/
@SuppressWarnings("unchecked")
private Class<? extends ModelObject> determineImplementationClass(BeanProperty beanProperty, Object obj) throws UnmarshallException {
Class<? extends ModelObject> implClass = null;
JsonTypeInfo typeInfo = null;
JsonSubTypes subTypes = null;
Class<?> defaultImplClass = null;
/*
* First, try to determine the implementation class to be created based on the bean property type. If the bean
* property is a scalar type, then use it as is. If it is a list, then use the first generic type. If it is a
* map, use the second generic type (maps are always assumed to be keyed by strings).
*/
implClass = (Class<? extends ModelObject>) beanProperty.getPropertyType();
if (beanProperty.isList()) {
implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[0];
} else if (beanProperty.isMap()) {
implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[1];
}
/*
* HACK: If the implClass is Constraint, then we have a special case. We need to examine the first key in the
* map to determine the type of constraint and return that name.
*/
if (implClass.equals(Constraint.class)) {
if (obj instanceof List) {
Map<String, Object> map = (Map<String, Object>) ((List<Object>) obj).get(0);
String constraintType = (String) map.keySet().toArray()[0];
for (int index = 0; index < CONSTRAINT_TYPES.length; index++) {
if (CONSTRAINT_TYPES[index].equals(constraintType)) {
implClass = CONSTRAINT_CLASSES[index];
return implClass;
}
}
}
}
/*
* If typeInfo annotation is present on the property type class, then check it to get the actual implementation
* class. Otherwise, we will instantiate the property defined.
*/
typeInfo = (JsonTypeInfo) ObjectHelper.getClassAnnotation(implClass, JsonTypeInfo.class);
if (typeInfo != null) {
subTypes = (JsonSubTypes) ObjectHelper.getClassAnnotation(implClass, JsonSubTypes.class);
defaultImplClass = typeInfo.defaultImpl();
JsonTypeInfo.Id use = typeInfo.use();
JsonTypeInfo.As include = typeInfo.include();
if (use.equals(JsonTypeInfo.Id.NAME) && include.equals(JsonTypeInfo.As.PROPERTY)) {
if (obj instanceof Map) {
Map<String, Object> map = (Map<String, Object>) obj;
String property = typeInfo.property();
String propertyValue = (String) map.get(property);
implClass = (Class<? extends ModelObject>) defaultImplClass;
if (propertyValue != null) {
JsonSubTypes.Type[] types = subTypes.value();
for (JsonSubTypes.Type type : types) {
if (type.name().equals(propertyValue)) {
implClass = (Class<? extends ModelObject>) type.value();
break;
}
}
}
}
} else if (use.equals(JsonTypeInfo.Id.CUSTOM)) {
JsonDeserialize deserializeAnnotation = (JsonDeserialize) ObjectHelper.getClassAnnotation(implClass, JsonDeserialize.class);
Class<? extends JsonDeserializer> deserializer = deserializeAnnotation.using();
} else {
throw new UnmarshallException(String.format("Only JsonTypeInfo use=\"NAME\" and include=\"PROPERTY\" " + " or use=\"CUSTOM\" with custom deserializer are supported. The mapping specified " + "use=\"%s\" and include=\"%s\"", use.name(), include.name()));
}
}
return implClass;
}
Aggregations