Class BeanFilter.Builder

java.lang.Object
org.apache.juneau.BeanFilter.Builder
Enclosing class:
BeanFilter

public static class BeanFilter.Builder extends Object
Builder class.
  • Constructor Details

    • Builder

      protected Builder(Class<?> beanClass)
      Constructor.
      Parameters:
      beanClass - The bean class that this filter applies to.
  • Method Details

    • applyAnnotations

      Applies the information in the specified list of @Bean annotations to this filter.
      Parameters:
      annotations - The annotations to apply.
      Returns:
      This object.
    • typeName

      Bean dictionary type name.

      Specifies the dictionary type name for this bean.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { typeName("mybean"); } } // Register it with a serializer or parser. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Produces: "{_type:'mybean', ...}" String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • implClass

      public BeanFilter.Builder implClass(Class<?> value)
      Bean implementation class.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • interfaceClass

      Bean interface class. Identifies a class to be used as the interface class for this and all subclasses.

      When specified, only the list of properties defined on the interface class will be used during serialization.
      Additional properties on subclasses will be ignored.

      // Parent class public abstract class A { public String f0 = "f0"; } // Sub class public class A1 extends A { public String f1 = "f1"; } // Define our filter. public class AFilter extends Builder<A> { public AFilter() { interfaceClass(A.class); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(AFilter.class) .build(); // Use it. A1 a1 = new A1(); String json = serializer.serialize(a1); assertEquals("{f0:'f0'}", json); // Note f1 is not serialized

      Note that this filter can be used on the parent class so that it filters to all child classes, or can be set individually on the child classes.

      See Also:
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • stopClass

      public BeanFilter.Builder stopClass(Class<?> value)
      Bean stop class.

      Identifies a stop class for this class and all subclasses.

      Identical in purpose to the stop class specified by Introspector.getBeanInfo(Class, Class).
      Any properties in the stop class or in its base classes will be ignored during analysis.

      For example, in the following class hierarchy, instances of C3 will include property p3, but not p1 or p2.

      Example:

      public class C1 { public int getP1(); } public class C2 extends C1 { public int getP2(); } public class C3 extends C2 { public int getP3(); } // Define our filter. public class C3Filter extends Builder<C3> { public C3Filter() { stopClass(C2.class); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(C3Filter.class) .build(); // Serializes property 'p3', but NOT 'p1' or 'p2'. String json = serializer.serialize(new C3());

      See Also:
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • sortProperties

      public BeanFilter.Builder sortProperties(boolean value)
      Sort bean properties.

      When true, all bean properties will be serialized and access in alphabetical order.
      Otherwise, the natural order of the bean properties is used which is dependent on the JVM vendor.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { sortProperties(); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Properties will be sorted alphabetically. String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this property.
      The default is false.
      Returns:
      This object.
    • sortProperties

      Sort bean properties.

      Shortcut for calling sortProperties(true).

      See Also:
      Returns:
      This object.
    • findFluentSetters

      Find fluent setters.

      When enabled, fluent setters are detected on beans.

      Fluent setters must have the following attributes:

      • Public.
      • Not static.
      • Take in one parameter.
      • Return the bean itself.
      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { findFluentSetters(); } }

      See Also:
      Returns:
      This object.
    • propertyNamer

      Bean property namer

      The class to use for calculating bean property names.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { // Use Dashed-Lower-Case property names. // (e.g. "foo-bar-url" instead of "fooBarURL") propertyNamer(PropertyNamerDLC.class); } } // Register it with a serializer or parser. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Properties names will be Dashed-Lower-Case. String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this setting.
      The default is BasicPropertyNamer.
      Returns:
      This object.
    • properties

      Bean property includes.

      Specifies the set and order of names of properties associated with the bean class.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { properties("foo,bar,baz"); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Only serializes the properties 'foo', 'bar', and 'baz'. String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this setting.
      Values can contain comma-delimited list of property names.
      Returns:
      This object.
    • excludeProperties

      Bean property excludes.

      Specifies properties to exclude from the bean class.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { excludeProperties("foo,bar"); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Serializes all properties except for 'foo' and 'bar'. String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this setting.
      Values can contain comma-delimited list of property names.
      Returns:
      This object.
    • readOnlyProperties

      Read-only bean properties.

      Specifies one or more properties on a bean that are read-only despite having valid getters. Serializers will serialize such properties as usual, but parsers will silently ignore them.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { readOnlyProperties("foo,bar"); } } // Register it with a parser. ReaderParser parser = JsonParser .create() .beanFilters(MyFilter.class) .build(); // Parsers all properties except for 'foo' and 'bar'. MyBean bean = parser.parse("...", MyBean.class);

      See Also:
      Parameters:
      value - The new value for this setting.
      Values can contain comma-delimited list of property names.
      Returns:
      This object.
    • writeOnlyProperties

      Write-only bean properties.

      Specifies one or more properties on a bean that are write-only despite having valid setters. Parsers will parse such properties as usual, but serializers will silently ignore them.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { writeOnlyProperties("foo,bar"); } } // Register it with a serializer. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build(); // Serializes all properties except for 'foo' and 'bar'. String json = serializer.serialize(new MyBean());

      See Also:
      Parameters:
      value - The new value for this setting.
      Values can contain comma-delimited list of property names.
      Returns:
      This object.
    • dictionary

      public BeanFilter.Builder dictionary(Class<?>... values)
      Bean dictionary.

      Adds to the list of classes that make up the bean dictionary for this bean.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { // Our bean contains generic collections of Foo and Bar objects. beanDictionary(Foo.class, Bar.class); } } // Register it with a parser. ReaderParser parser = JsonParser .create() .beanFilters(MyFilter.class) .build(); // Instantiate our bean. MyBean bean = parser.parse(json);

      See Also:
      Parameters:
      values - The values to add to this property.
      Returns:
      This object.
    • example

      Example.
      Parameters:
      value - The new value for this property.
      Returns:
      This object.
    • interceptor

      Bean interceptor.

      The interceptor to use for intercepting and altering getter and setter calls.

      Example:

      // Define our filter. public class MyFilter extends Builder<MyBean> { public MyFilter() { // Our bean contains generic collections of Foo and Bar objects. interceptor(AddressInterceptor.class); } } // Register it with a serializer or parser. WriterSerializer serializer = JsonSerializer .create() .beanFilters(MyFilter.class) .build();

      See Also:
      Parameters:
      value - The new value for this setting.
      The default value is BeanInterceptor.
      Returns:
      This object.
    • build

      public BeanFilter build()
      Creates a BeanFilter with settings in this builder class.
      Returns:
      A new BeanFilter instance.