Feature: TypeFactory should have methods for constructing any JavaType instances

(see Jira entry JACKSON-229 for details)

As per 1.4 (and prior) versions, the only way to define generic type to bind to was to sub-class TypeReference object, like so:

  List<MyBean> result = mapper.readValue(src, new TypeReference<List<MyBean>>() { });   

While this works for many cases, it does not allow dynamic configuration of types, since such inner class declarations can only be used when type is known statically, when writing code. Additionally this may not be most readable method.

So why not allow constructing equivalent types with TypeFactory?

With 1.5, you can replace above piece of code with:

  JavaType type = TypeFactory.parametricType(List.class, MyBean.class);
  // or; type = TypeFactory.collectionType(List.class, MyBean.class)
  List<MyBean> result = mapper.readValue(src, type);

Factory methods that TypeFactory has can obviously be chained and nested as appropriate to produce any valid JavaType.


CategoryJackson

JacksonFeatureConstructingTypes (last edited 2010-03-13 01:31:20 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC