Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser;
  
Helper class for BeanSerializerFactory that is used to construct BeanPropertyWriter instances. Can be sub-classed to change behavior.
 
 public class PropertyBuilder
 {
     final protected SerializationConfig _config;
     final protected BeanDescription _beanDesc;
     final protected JsonInclude.Include _outputProps;
 
     final protected AnnotationIntrospector _annotationIntrospector;

    
If a property has serialization inclusion value of com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion.ALWAYS, we need to know the default value of the bean, to know if property value equals default one.
 
     protected Object _defaultBean;
 
     public PropertyBuilder(SerializationConfig configBeanDescription beanDesc)
     {
          = config;
          = beanDesc;
          = beanDesc.findSerializationInclusion(config.getSerializationInclusion());
     }
 
     /*
     /**********************************************************
     /* Public API
     /**********************************************************
      */
 
     public Annotations getClassAnnotations() {
         return .getClassAnnotations();
     }
    
    

Parameters:
contentTypeSer Optional explicit type information serializer to use for contained values (only used for properties that are of container type)
 
             JavaType declaredTypeJsonSerializer<?> ser,
             TypeSerializer typeSerTypeSerializer contentTypeSer,
             AnnotatedMember amboolean defaultUseStaticTyping)
     {
         // do we have annotation that forces type to use (to declared type or its super type)?
         JavaType serializationType = findSerializationType(amdefaultUseStaticTypingdeclaredType);
 
         // Container types can have separate type serializers for content (value / element) type
         if (contentTypeSer != null) {
             /* 04-Feb-2010, tatu: Let's force static typing for collection, if there is
              *    type information for contents. Should work well (for JAXB case); can be
              *    revisited if this causes problems.
              */
             if (serializationType == null) {
 //                serializationType = TypeFactory.type(am.getGenericType(), _beanDesc.getType());
                 serializationType = declaredType;
             }
             JavaType ct = serializationType.getContentType();
             /* 03-Sep-2010, tatu: This is somehow related to [JACKSON-356], but I don't completely
              *   yet understand how pieces fit together. Still, better be explicit than rely on
              *   NPE to indicate an issue...
              */
             if (ct == null) {
                 throw new IllegalStateException("Problem trying to create BeanPropertyWriter for property '"
                         +propDef.getName()+"' (of type "+.getType()+"); serialization type "+serializationType+" has no content");
             }
             serializationType = serializationType.withContentTypeHandler(contentTypeSer);
             ct = serializationType.getContentType();
         }
         
         Object valueToSuppress = null;
         boolean suppressNulls = false;
 
         if (methodProps != null) {
             switch (methodProps) {
             case :
                 valueToSuppress = getDefaultValue(propDef.getName(), am);
                 if (valueToSuppress == null) {
                     suppressNulls = true;
                 } else {
                     // [JACKSON-531]: Allow comparison of arrays too...
                     if (valueToSuppress.getClass().isArray()) {
                         valueToSuppress = Comparators.getArrayComparator(valueToSuppress);
                    }
                }
                break;
            case :
                // always suppress nulls
                suppressNulls = true;
                // but possibly also 'empty' values:
                valueToSuppress = .;
                break;
            case :
                suppressNulls = true;
                // fall through
            case // default
                // we may still want to suppress empty collections, as per [JACKSON-254]:
                if (declaredType.isContainerType()
                        && !.isEnabled(.)) {
                    valueToSuppress = .;
                }
                break;
            }
        }
        BeanPropertyWriter bpw = new BeanPropertyWriter(propDef,
                am.getClassAnnotations(), declaredType,
                sertypeSerserializationTypesuppressNullsvalueToSuppress);
        
        // [JACKSON-132]: Unwrapping
        if (unwrapper != null) {
            bpw = bpw.unwrappingWriter(unwrapper);
        }
        return bpw;
    }
    
    /*
    /**********************************************************
    /* Helper methods; annotation access
    /**********************************************************
     */

    
Method that will try to determine statically defined type of property being serialized, based on annotations (for overrides), and alternatively declared type (if static typing for serialization is enabled). If neither can be used (no annotations, dynamic typing), returns null.
    protected JavaType findSerializationType(Annotated aboolean useStaticTypingJavaType declaredType)
    {
        // [JACKSON-120]: Check to see if serialization type is fixed
        Class<?> serClass = .findSerializationType(a);
        if (serClass != null) {
            // Must be a super type to be usable
            Class<?> rawDeclared = declaredType.getRawClass();
            if (serClass.isAssignableFrom(rawDeclared)) {
                declaredType = declaredType.widenBy(serClass);
            } else {
                /* 18-Nov-2010, tatu: Related to fixing [JACKSON-416], an issue with such
                 *   check is that for deserialization more specific type makes sense;
                 *   and for serialization more generic. But alas JAXB uses but a single
                 *   annotation to do both... Hence, we must just discard type, as long as
                 *   types are related
                 */
                if (!rawDeclared.isAssignableFrom(serClass)) {
                    throw new IllegalArgumentException("Illegal concrete-type annotation for method '"+a.getName()+"': class "+serClass.getName()+" not a super-type of (declared) class "+rawDeclared.getName());
                }
                /* 03-Dec-2010, tatu: Actually, ugh, to resolve [JACKSON-415] may further relax this
                 *   and actually accept subtypes too for serialization. Bit dangerous in theory
                 *   but need to trust user here...
                 */
                declaredType = .constructSpecializedType(declaredTypeserClass);
            }
            useStaticTyping = true;
        }
        JavaType secondary = BeanSerializerFactory.modifySecondaryTypesByAnnotation(adeclaredType);
        if (secondary != declaredType) {
            useStaticTyping = true;
            declaredType = secondary;
        }
        
        /* [JACKSON-114]: if using static typing, declared type is known
         * to be the type...
         */
        if (!useStaticTyping) {
            if (typing != null) {
                useStaticTyping = (typing == ..);
            }
        }
        return useStaticTyping ? declaredType : null;
    }
    /*
    /**********************************************************
    /* Helper methods for default value handling
    /**********************************************************
     */
    
    protected Object getDefaultBean()
    {
        if ( == null) {
            /* If we can fix access rights, we should; otherwise non-public
             * classes or default constructor will prevent instantiation
             */
            if ( == null) {
                Class<?> cls = .getClassInfo().getAnnotated();
                throw new IllegalArgumentException("Class "+cls.getName()+" has no default constructor; can not instantiate default bean value to support 'properties=JsonSerialize.Inclusion.NON_DEFAULT' annotation");
            }
        }
        return ;
    }
    protected Object getDefaultValue(String nameAnnotatedMember member)
    {
        Object defaultBean = getDefaultBean();
        try {
            return member.getValue(defaultBean);
        } catch (Exception e) {
            return _throwWrapped(enamedefaultBean);
        }
    }
    /*
    /**********************************************************
    /* Helper methods for exception handling
    /**********************************************************
     */
    
    protected Object _throwWrapped(Exception eString propNameObject defaultBean)
    {
        Throwable t = e;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        if (t instanceof Errorthrow (Errort;
        if (t instanceof RuntimeExceptionthrow (RuntimeExceptiont;
        throw new IllegalArgumentException("Failed to get property '"+propName+"' of default "+defaultBean.getClass().getName()+" instance");
    }
New to GrepCode? Check out our FAQ X