Feature: Materialized Beans

(functionality known as "Mr Bean")

Big Idea

There are many use cases where implement of a Bean (value) class is very trivial: for example, let's say we would want to access a Bean defined by interface:

   1   public interface Coordinates {
   2     public double getX();
   3     public double getY();
   4   }

one could define Class:

   1   public class CoordinatesImpl implements Coordinates {
   2      private double x, y;
   3 
   4      public CoordinatesImpl() { }
   5 
   6     public double getX() { return x; }
   7     public double getY() { return y; }
   8  
   9     // setters or equivalent needed to deserialize values in 
  10     public void setX(double x) { this.x = x; }
  11     public void setY(double y) { this.y = y; }
  12 
  13   }

As can be seen from above, actual definition of implementation is little more than "fill-in-blanks" extension of interface. So much so that it should be possible to automate this process.

This feature, then, would be just that: ability to automatically generate classes that implement interfaces (and perhaps abstract classes) such that they can be used as value beans to deserialize from JSON; and later on also serialized (if necessary).

Given that construction of serializers and deserializers is automatic with auto-detection, feature only needs to create implementation classes and not be concerned directly with serialization or deserialization aspects.

Status

This feature has been implemented in Jackson 1.6, see Jira entry JACKSON-41 for more details.

Since implementation uses a repackaged version of ASM library, it was decided to create a new artifact (jar), named "jackson-mrbean" to contain functionality. This jar has no external dependencies, but does depend on "jackson-mapper"(-asl) jar.

Usage

To enable use of bean materializer, there are 2 ways: with Jackson 1.8, you should use MrBeanModule (a generic JacksonFeatureModules module implementation:

  ObjectMapper mapper = new ObjectMapper();
  mapper.registerModule(new MrBeanModule());

but with earlier (1.7, 1.6) versions, you will need to explicitly register handlers via DeserializationConfig:

  ObjectMapper mapper = new ObjectMapper();
  // org.codehaus.jackson.mrbean.AbstractTypeMaterializer, extends org.codehaus.jackson.map.AbstractTypeResolver
  mapper.getDeserializationConfig().setAbstractTypeResolver(new AbstractTypeMaterializer());

Either way, from that point on, all abstract types (interfaces, abstract classes) for which there is no explicit overrides (custom deserializers) will be materialized automatically; except for Collection and Map types (which will use default bindings). That is, this only applies to Bean types.


CategoryJackson

JacksonFeatureMaterializedBeans (last edited 2011-02-19 19:30:55 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC