Feature: Inject Values

(see Jira entry JACKSON-406 for details)

Note: as of Jackson 1.9, this feature is only available during Deserialization (reading JSON into POJOs) -- it is possible that similar functionality may be added for serialization in future.

Problem to Solve

Sometimes construction of values requires presence of some contextual information or meta-data, something that is not available from data itself. This may be because a framework requires factories to use, for example. If so, it would be good to have some declarative way to indicate where such values are to be "injected"; as well as have configurable means to provide such Object values.

Solution

Jackson 1.9 adds following thigs:

Example

Here is a simple example of how to use this feature.

First, a POJO we want to deserialize

public class MyValue
{
  @JacksonInject
  private Id id;

  private NamedValue nameValue;

  @JsonCretor
  public MyValue(@JacksonInject("factory") NameFactory factory,
    @JsonProperty("name") String name) {
       nameValue = factory.create(name);
  }
}

which we could instantiate from JSON like, say:

  { "name" : "some name" }

as long as we configured ObjectMapper or ObjectReader like so:

       ObjectMapper mapper = new ObjectMapper();
       Id id = ...;
       NameFactory f = ...;
       InjectableValues inject = new InjectableValues.Std()
           .addValue(Id.class, id)
           .addValue(NameFactory.class, f)
       // or create ObjectReader, use 'withInjectables()'
       MyValue value = mapper.reader(MyValue.class).withInjectableValues(inject).readValue(json);

Notes on above:


CategoryJackson

JacksonFeatureValueInjection (last edited 2011-10-13 02:41:08 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC