BeanFilter Class

The {@link oaj.transform.BeanFilter} class is the programmatic equivalent to the {@link oaj.annotation.Bean @Bean} annotation.

In practice, it's usually simpler to use the {@link oaj.annotation.Bean @Bean} and {@link oaj.annotation.Beanp @Beanp} annotations on your bean classes. However, bean filters make it possible to accomplish the same when you can't add annotations to existing code.

Bean filters are defined through {@link oaj.transform.BeanFilterBuilder BeanFilterBuilders}.

In the previous examples, we defined this bean annotation:

@Bean(bpi="street,city,state") public class Address { ... }

The programmatic equivalent would be:

public class AddressFilter extends BeanFilterBuilder<Address> { // Must provide a no-arg constructor! public AddressFilter() { bpi("street,city,state"); // The properties we want exposed. } }

Bean filters are added to serializers and parsers using the following:

- {@link oaj.BeanContext#BEAN_beanFilters}
- {@link oaj.BeanContextBuilder#beanFilters(Object...)}

For example:

// Create a new JSON serializer and associate a bean filter with it. WriterSerializer serializer = JsonSerializer .create() .beanFilters(AddressFilter.class) .build();

Note that if you use the annotation, you do NOT need to set anything on the serializers/parsers. The annotations will be detected and bean filters will automatically be created for them.

The {@link oaj.BeanContextBuilder#beanFilters(Object...)} method also allows you to pass in interfaces. Any class that's not a subclass of {@link oaj.transform.BeanFilterBuilder} get interpreted as bean interface classes. These cause bean implementations of those interfaces to only expose the properties defined on the interface.

// An interface with the 3 properties we want serialized. public interface AddressInterface { public String getStreet(); public String getCity(); public String getState(); } // Our bean implementation. public class Address implements AddressInterface { public String getStreet() {...}; public String getCity() {...}; public String getState() {...}; public String getCountry() {...}; } // Create a new JSON serializer that only exposes street,city,state on Address bean. // The 'country' field will be ignored. WriterSerializer serializer = JsonSerializer .create() .beanFilters(AddressInterface.class) .build();