Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.impl;
  
  import java.util.*;
  
Simple com.fasterxml.jackson.databind.ser.PropertyFilter implementation that only uses property name to determine whether to serialize property as is, or to filter it out.

Use of this class as the base implementation for any custom com.fasterxml.jackson.databind.ser.PropertyFilter implementations is strongly encouraged, because it can provide default implementation for any methods that may be added in com.fasterxml.jackson.databind.ser.PropertyFilter (as unfortunate as additions may be).

 
 public abstract class SimpleBeanPropertyFilter
     implements BeanPropertyFilterPropertyFilter
         // sub-classes must also implement java.io.Serializable
 {
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     protected SimpleBeanPropertyFilter() { }

    
Factory method to construct filter that filters out all properties except ones includes in set
 
     public static SimpleBeanPropertyFilter filterOutAllExcept(Set<Stringproperties) {
         return new FilterExceptFilter(properties);
     }
 
     public static SimpleBeanPropertyFilter filterOutAllExcept(String... propertyArray) {
         HashSet<Stringproperties = new HashSet<String>(propertyArray.length);
         Collections.addAll(propertiespropertyArray);
         return new FilterExceptFilter(properties);
     }
 
     public static SimpleBeanPropertyFilter serializeAllExcept(Set<Stringproperties) {
         return new SerializeExceptFilter(properties);
     }
 
     public static SimpleBeanPropertyFilter serializeAllExcept(String... propertyArray) {
         HashSet<Stringproperties = new HashSet<String>(propertyArray.length);
         Collections.addAll(propertiespropertyArray);
         return new SerializeExceptFilter(properties);
     }

    
 
     @SuppressWarnings("deprecation")
     public static PropertyFilter from(final BeanPropertyFilter src)
     {
         return new PropertyFilter() {
             @Override
             public void serializeAsField(Object pojoJsonGenerator jgen,
                     SerializerProvider provPropertyWriter writer)
                 throws Exception {
                 src.serializeAsField(pojojgenprov, (BeanPropertyWriterwriter);
             }
 
             @Override
             public void depositSchemaProperty(PropertyWriter writer,
                     ObjectNode propertiesNodeSerializerProvider provider)
                 throws JsonMappingException {
                 src.depositSchemaProperty((BeanPropertyWriterwriterpropertiesNodeprovider);
             }
 
             @Override
             public void depositSchemaProperty(PropertyWriter writer,
                     JsonObjectFormatVisitor objectVisitor,
                 SerializerProvider providerthrows JsonMappingException {
                 src.depositSchemaProperty((BeanPropertyWriterwriterobjectVisitorprovider);
             }
 
             @Override
             public void serializeAsElement(Object elementValue,
                     JsonGenerator jgenSerializerProvider prov,
                     PropertyWriter writerthrows Exception {
                 // not needed; element filtering only available through new interfaces
                 throw new UnsupportedOperationException();
             }
             
         };
     }
 
     /*
     /**********************************************************
    /* Methods for sub-classes
    /**********************************************************
     */

    
Method called to determine whether property will be included (if 'true' returned) or filtered out (if 'false' returned)
    protected abstract boolean include(BeanPropertyWriter writer);

    

Since:
2.3
    protected abstract boolean include(PropertyWriter writer);

    
Method that defines what to do with container elements; default implementation simply writes them out.

Since:
2.3
    protected boolean includeElement(Object elementValue) {
        return true;
    }
    
    /*
    /**********************************************************
    /* BeanPropertyFilter (deprecated) implementation
    /**********************************************************
     */
    
    @Deprecated
    @Override
    public void serializeAsField(Object beanJsonGenerator jgen,
            SerializerProvider providerBeanPropertyWriter writerthrows Exception
    {
        if (include(writer)) {
            writer.serializeAsField(beanjgenprovider);
        } else if (!jgen.canOmitFields()) { // since 2.3
            writer.serializeAsOmittedField(beanjgenprovider);
        }
    }
    @Deprecated
    @Override
    public void depositSchemaProperty(BeanPropertyWriter writer,
            ObjectNode propertiesNodeSerializerProvider provider)
        throws JsonMappingException
    {
        if (include(writer)) {
            writer.depositSchemaProperty(propertiesNodeprovider);
        }
    }
    @Deprecated
    @Override
    public void depositSchemaProperty(BeanPropertyWriter writer,
            JsonObjectFormatVisitor objectVisitorSerializerProvider provider)
        throws JsonMappingException
    {
        if (include(writer)) {
            writer.depositSchemaProperty(objectVisitor);
        }
    }
    /*
    /**********************************************************
    /* PropertyFilter implementation
    /**********************************************************
     */
    
    @Override
    public void serializeAsField(Object pojoJsonGenerator jgen,
            SerializerProvider providerPropertyWriter writer)
        throws Exception
    {
        if (include(writer)) {
            writer.serializeAsField(pojojgenprovider);
        } else if (!jgen.canOmitFields()) { // since 2.3
            writer.serializeAsOmittedField(pojojgenprovider);
        }
    }
    @Override
    public void serializeAsElement(Object elementValueJsonGenerator jgenSerializerProvider provider,
            PropertyWriter writer)
        throws Exception
    {
        if (includeElement(elementValue)) {
            writer.serializeAsElement(elementValuejgenprovider);
        }
    }
    
    @Deprecated
    @Override
    public void depositSchemaProperty(PropertyWriter writer,
            ObjectNode propertiesNodeSerializerProvider provider)
            throws JsonMappingException
    {
        if (include(writer)) {
            writer.depositSchemaProperty(propertiesNodeprovider);
        }
    }
    @Override
    public void depositSchemaProperty(PropertyWriter writer,
            JsonObjectFormatVisitor objectVisitor,
            SerializerProvider providerthrows JsonMappingException 
    {
        if (include(writer)) {
            writer.depositSchemaProperty(objectVisitor);
        }
    }
    /*
    /**********************************************************
    /* Sub-classes
    /**********************************************************
     */

    
Filter implementation which defaults to filtering out unknown properties and only serializes ones explicitly listed.
    public static class FilterExceptFilter
        extends SimpleBeanPropertyFilter
        implements java.io.Serializable
    {
        private static final long serialVersionUID = 1L;

        
Set of property names to serialize.
        protected final Set<String_propertiesToInclude;
        public FilterExceptFilter(Set<Stringproperties) {
             = properties;
        }
        @Override
        protected boolean include(BeanPropertyWriter writer) {
            return .contains(writer.getName());
        }
        @Override
        protected boolean include(PropertyWriter writer) {
            return .contains(writer.getName());
        }
    }

    
Filter implementation which defaults to serializing all properties, except for ones explicitly listed to be filtered out.
    public static class SerializeExceptFilter
        extends SimpleBeanPropertyFilter
        implements java.io.Serializable
    {
        private static final long serialVersionUID = 1L;

        
Set of property names to filter out.
        protected final Set<String_propertiesToExclude;
        public SerializeExceptFilter(Set<Stringproperties) {
             = properties;
        }
        @Override
        protected boolean include(BeanPropertyWriter writer) {
            return !.contains(writer.getName());
        }
        @Override
        protected boolean include(PropertyWriter writer) {
            return !.contains(writer.getName());
        }
    }
New to GrepCode? Check out our FAQ X