Search in sources :

Example 1 with JsonDeserialize

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());
}
Also used : JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Field(java.lang.reflect.Field) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Test(org.junit.Test)

Example 2 with JsonDeserialize

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;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize)

Example 3 with JsonDeserialize

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;
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PatternConstraint(com.att.cdp.openstack.heat.model.PatternConstraint) RangeConstraint(com.att.cdp.openstack.heat.model.RangeConstraint) Constraint(com.att.cdp.openstack.heat.model.Constraint) ValuesConstraint(com.att.cdp.openstack.heat.model.ValuesConstraint) CustomConstraint(com.att.cdp.openstack.heat.model.CustomConstraint) LengthConstraint(com.att.cdp.openstack.heat.model.LengthConstraint) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 UnmarshallException (com.att.cdp.openstack.exception.UnmarshallException)1 Constraint (com.att.cdp.openstack.heat.model.Constraint)1 CustomConstraint (com.att.cdp.openstack.heat.model.CustomConstraint)1 LengthConstraint (com.att.cdp.openstack.heat.model.LengthConstraint)1 ModelObject (com.att.cdp.openstack.heat.model.ModelObject)1 PatternConstraint (com.att.cdp.openstack.heat.model.PatternConstraint)1 RangeConstraint (com.att.cdp.openstack.heat.model.RangeConstraint)1 ValuesConstraint (com.att.cdp.openstack.heat.model.ValuesConstraint)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1