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.BeanPropertyFilter implementation that only uses property name to determine whether to serialize property as is, or to filter it out.
 
 public abstract class SimpleBeanPropertyFilter
     implements BeanPropertyFilter // 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);
     }
 
     /*
     /**********************************************************
     /* 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);
 
     public void serializeAsField(Object beanJsonGenerator jgen,
             SerializerProvider providerBeanPropertyWriter writerthrows Exception
     {
         if (include(writer)) {
             writer.serializeAsField(beanjgenprovider);
         }
     }
 
     
     public void depositSchemaProperty(BeanPropertyWriter writer,
             ObjectNode propertiesNodeSerializerProvider provider)
         throws JsonMappingException
     {
         if (include(writer)) {
             writer.depositSchemaProperty(propertiesNodeprovider);
         }
     }
 
     public void depositSchemaProperty(BeanPropertyWriter writer,
             JsonObjectFormatVisitor objectVisitorSerializerProvider provider)
         throws 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());
        }
    }

    
Filter implementation which defaults to serializing all properties, except for ones explicitly listed to be filtered out.
    public static class SerializeExceptFilter
        extends SimpleBeanPropertyFilter
    {
        
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());
        }
    }
New to GrepCode? Check out our FAQ X