Class BeanFilter.Builder
- Enclosing class:
BeanFilter
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionapplyAnnotations
(List<Bean> annotations) Applies the information in the specified list of@Bean
annotations to this filter.build()
Creates aBeanFilter
with settings in this builder class.dictionary
(Class<?>... values) Bean dictionary.Example.excludeProperties
(String... value) Bean property excludes.Find fluent setters.Bean implementation class.interceptor
(Class<?> value) Bean interceptor.interfaceClass
(Class<?> value) Bean interface class.properties
(String... value) Bean property includes.propertyNamer
(Class<? extends PropertyNamer> value) Bean property namerreadOnlyProperties
(String... value) Read-only bean properties.Sort bean properties.sortProperties
(boolean value) Sort bean properties.Bean stop class.Bean dictionary type name.writeOnlyProperties
(String... value) Write-only bean properties.
-
Constructor Details
-
Builder
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 MyFilterextends Builder<MyBean> {public MyFilter() { typeName("mybean" ); } }// Register it with a serializer or parser. WriterSerializerserializer = JsonSerializer . create () .beanFilters(MyFilter.class ) .build();// Produces: "{_type:'mybean', ...}" Stringjson =serializer .serialize(new MyBean());See Also:
- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
implClass
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 Stringf0 ="f0" ; }// Sub class public class A1extends A {public Stringf1 ="f1" ; }// Define our filter. public class AFilterextends Builder<A> {public AFilter() { interfaceClass(A.class ); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(AFilter.class ) .build();// Use it. A1a1 =new A1(); Stringjson =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
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 propertyp3 , but notp1 orp2 .Example:
public class C1 {public int getP1(); }public class C2extends C1 {public int getP2(); }public class C3extends C2 {public int getP3(); }// Define our filter. public class C3Filterextends Builder<C3> {public C3Filter() { stopClass(C2.class ); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(C3Filter.class ) .build();// Serializes property 'p3', but NOT 'p1' or 'p2'. Stringjson =serializer .serialize(new C3());See Also:
- Parameters:
value
- The new value for this setting.- Returns:
- This object.
-
sortProperties
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 MyFilterextends Builder<MyBean> {public MyFilter() { sortProperties(); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();// Properties will be sorted alphabetically. Stringjson =serializer .serialize(new MyBean());See Also:
- Parameters:
value
- The new value for this property.
The default isfalse .- 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 MyFilterextends Builder<MyBean> {public MyFilter() { findFluentSetters(); } }See Also:
- Returns:
- This object.
-
propertyNamer
Bean property namerThe class to use for calculating bean property names.
Example:
// Define our filter. public class MyFilterextends 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. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();// Properties names will be Dashed-Lower-Case. Stringjson =serializer .serialize(new MyBean());See Also:
- Parameters:
value
- The new value for this setting.
The default isBasicPropertyNamer
.- 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 MyFilterextends Builder<MyBean> {public MyFilter() { properties("foo,bar,baz" ); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();// Only serializes the properties 'foo', 'bar', and 'baz'. Stringjson =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 MyFilterextends Builder<MyBean> {public MyFilter() { excludeProperties("foo,bar" ); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();// Serializes all properties except for 'foo' and 'bar'. Stringjson =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 MyFilterextends Builder<MyBean> {public MyFilter() { readOnlyProperties("foo,bar" ); } }// Register it with a parser. ReaderParserparser = JsonParser .create () .beanFilters(MyFilter.class ) .build();// Parsers all properties except for 'foo' and 'bar'. MyBeanbean =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 MyFilterextends Builder<MyBean> {public MyFilter() { writeOnlyProperties("foo,bar" ); } }// Register it with a serializer. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();// Serializes all properties except for 'foo' and 'bar'. Stringjson =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
Bean dictionary.Adds to the list of classes that make up the bean dictionary for this bean.
Example:
// Define our filter. public class MyFilterextends Builder<MyBean> {public MyFilter() {// Our bean contains generic collections of Foo and Bar objects. beanDictionary(Foo.class , Bar.class ); } }// Register it with a parser. ReaderParserparser = JsonParser .create () .beanFilters(MyFilter.class ) .build();// Instantiate our bean. MyBeanbean =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 MyFilterextends Builder<MyBean> {public MyFilter() {// Our bean contains generic collections of Foo and Bar objects. interceptor(AddressInterceptor.class ); } }// Register it with a serializer or parser. WriterSerializerserializer = JsonSerializer .create () .beanFilters(MyFilter.class ) .build();See Also:
- Parameters:
value
- The new value for this setting.
The default value isBeanInterceptor
.- Returns:
- This object.
-
build
Creates aBeanFilter
with settings in this builder class.- Returns:
- A new
BeanFilter
instance.
-