Package com.fasterxml.jackson.annotation
Annotation Interface JsonFormat
@Target({ANNOTATION_TYPE,FIELD,METHOD,PARAMETER,TYPE})
@Retention(RUNTIME)
public @interface JsonFormat
General-purpose annotation used for configuring details of how
values of properties are to be serialized.
Unlike most other Jackson annotations, annotation does not
have specific universal interpretation: instead, effect depends on datatype
of property being annotated (or more specifically, deserializer
and serializer being used).
Common uses include choosing between alternate representations -- for example,
whether Date
is to be serialized as number (Java timestamp)
or String (such as ISO-8601 compatible time value) -- as well as configuring
exact details with pattern()
property.
As of Jackson 3.0, known special handling includes:
Date
orCalendar
: Shape can beJsonFormat.Shape.STRING
orJsonFormat.Shape.NUMBER
; pattern may containSimpleDateFormat
-compatible pattern definition.java.time.*
: Types in thejava.time
package can useJsonFormat.Shape.STRING
for serialization and deserialization. WhenJsonFormat.Shape.STRING
is used, the pattern property typically followsDateTimeFormatter
-compatible formatting rules.- Can be used on Classes (types) as well, for modified default behavior, possibly overridden by per-property annotation
Enum
s: ShapesJsonFormat.Shape.STRING
andJsonFormat.Shape.NUMBER
can be used to change between numeric (index) and textual (name ortoString()
); but it is also possible to useJsonFormat.Shape.OBJECT
to serialize (but not deserialize)Enum
s as JSON Objects (as if they were POJOs). NOTE: serialization as JSON Object only works with class annotation; will not work as per-property annotation.Collection
s can be serialized as (and deserialized from) JSON Objects, ifJsonFormat.Shape.OBJECT
is used. NOTE: can ONLY be used as class annotation; will not work as per-property annotation.Number
subclasses can be serialized as full objects ifJsonFormat.Shape.OBJECT
is used. Otherwise the default behavior of serializing to a scalar number value will be preferred. NOTE: can ONLY be used as class annotation; will not work as per-property annotation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Set of features that can be enabled/disabled for property annotated.static class
static enum
Value enumeration used for indicating preferred Shape; translates loosely to JSON types, with some extra values to indicate less precise choices (i.e. allowing one of multiple actual shapes)static class
Helper class used to contain information from a singleJsonFormat
annotation. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionProperty that indicates whether "lenient" handling should be enabled or disabled.Locale
to use for serialization (if needed).Datatype-specific additional piece of configuration that may be used to further refine formatting aspects.Structure to use for serialization: definition of mapping depends on datatype, but usually has straight-forward counterpart in data format (JSON).TimeZone
to use for serialization (if needed).Set ofJsonFormat.Feature
s to explicitly enable with respect to handling of annotated property.Set ofJsonFormat.Feature
s to explicitly disable with respect to handling of annotated property. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Value that indicates that defaultLocale
(from deserialization or serialization context) should be used: annotation does not define value to use.static final String
Value that indicates that defaultTimeZone
(from deserialization or serialization context) should be used: annotation does not define value to use.
-
Field Details
-
DEFAULT_LOCALE
Value that indicates that defaultLocale
(from deserialization or serialization context) should be used: annotation does not define value to use.- See Also:
-
DEFAULT_TIMEZONE
Value that indicates that defaultTimeZone
(from deserialization or serialization context) should be used: annotation does not define value to use.NOTE: default here does NOT mean JVM defaults but Jackson databindings default, usually UTC, but may be changed on
ObjectMapper
.- See Also:
-
-
Element Details
-
pattern
String patternDatatype-specific additional piece of configuration that may be used to further refine formatting aspects. This may, for example, determine low-level format String used forDate
serialization; however, exact use is determined by specificJsonSerializer
- Default:
- ""
-
shape
JsonFormat.Shape shapeStructure to use for serialization: definition of mapping depends on datatype, but usually has straight-forward counterpart in data format (JSON). Note that commonly only a subset of shapes is available; and if 'invalid' value is chosen, defaults are usually used.- Default:
- ANY
-
locale
String localeLocale
to use for serialization (if needed). Special value ofDEFAULT_LOCALE
can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (Locale.getDefault()
) unless explicitly set to another locale.- Default:
- "##default"
-
timezone
String timezoneTimeZone
to use for serialization (if needed). Special value ofDEFAULT_TIMEZONE
can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system default (UTC) unless explicitly set to another timezone.- Default:
- "##default"
-
lenient
OptBoolean lenientProperty that indicates whether "lenient" handling should be enabled or disabled. This is relevant mostly for deserialization of some textual datatypes, especially date/time types.Note that underlying default setting depends on datatype (or more precisely deserializer for it): for most date/time types, default is for leniency to be enabled.
- Default:
- DEFAULT
-
with
JsonFormat.Feature[] withSet ofJsonFormat.Feature
s to explicitly enable with respect to handling of annotated property. This will have precedence over possible global configuration.- Default:
- {}
-
without
JsonFormat.Feature[] withoutSet ofJsonFormat.Feature
s to explicitly disable with respect to handling of annotated property. This will have precedence over possible global configuration.- Default:
- {}
-