Direct Field Access

Prior to version 1.2, Jackson only allowed serialization / deserialization of properties inferred by presence of "getter" (getPropertyName) and "setter" (setPropertyName) properties. But starting with 1.2, member fields can also directly represent properties.

By default, a member field can represent a property if it:

  1. Does NOT have associated (and enabled) @JsonIgnore annotation AND

  2. is NOT static or transient AND

  3. is not a synthetic method (so-called "bridge" methods) created by compiler

and it is either

  1. Public AND Field auto-detection is enabled (annotation @JsonAutoDetect for class includes Method.Field -- this is true by default), OR

  2. Has associated property definition annotation; @JsonProperty, @JsonSerialize or @JsonDeserialize

If so, field in question will define a property. Name of the property is same as that of the field unless explicitly defined by associated @JsonProperty (which takes property name as its 'value').

Name collisions

It is NOT legal for multiple fields to define property with the same logical name. Trying to do so will result in an exception being thrown when trying to construct serializer or deserializer during mapping.

Field properties vs. getters/setters

It IS legal to have both setter/getter and field to define property with the same logical name. If so, only setter (for deserialization) or getter (for serialization) is used, and field is ignored. That is; method access has priority over field access. This can not be changed; to force use of field access, you need to ensure that matching method is ignored (usually using @JsonIgnore annotation).


CategoryJackson

JacksonFieldProperties (last edited 2009-07-29 06:31:11 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC