Feature: Serialize with Static Types

(see Jira entry http://jira.codehaus.org/browse/JACKSON-114 for details)

By default (and always, prior to version 1.2 Jackson uses actual runtime type to figure out details of serialization.

But starting with 1.2, it is possible to force type used to be the static (declared) type: declared type of serialized field or declared return type of "getter" method.

Determination of which type detection (static or dynamic) method is used for a property is determined based on following factors (in decreasing order of precedence):

  1. JsonSerialize.typing annotation directly associated with the property; if any

  2. JsonSerialize.typing annotation in type used for serializing bean that contains property; if any

  3. Global default setting, as determined by SerializationConfig.Feature.USE_STATIC_TYPING (settable using ObjectMapper.configure() method) (default is 'false', to indicate that dynamic typing is used)

Determination is done recursively, starting with the "root" object being serialized, and from thereon types are determined recursively. Most commonly last type (global setting) is used.

NOTE: Although you can change the global setting during runtime, it is only guaranteed to affect ObjectMapper in question if done before any serializations are done. Changes done after this may or may not have effect on behavior of that {{{ObjectMapper.

Code example:

interface ValueInterface {
  public int getPublic();
}

class ValueClass implements ValueInterface
{
  public int getPublic() { return 1; }
  public int getPrivate() { return 2; }
}

// wrapper class
@JsonSerialize(typing=JsonSerialize.Typing.STATIC)
class Wrapper
{
  public ValueClass getValue() {
     return new ValueClass();
  }
}

with this definition, output using objectMapper.writeValue(output, new Wrapper()); would look like:

{
  "public" : 1
}

whereas without annotation (or it set to use dynamic typing), we would get:

{
  "public" : 1,
  "private" : 2
}


CategoryJackson

JacksonSerializeWithStaticTypes (last edited 2009-07-18 02:59:10 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC