Annotation Type JsonFormat
Annotation can be used on Classes (types) as well, for modified default behavior, possibly overridden by per-property annotations.
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 2.20, known special handling includes:
(NOTE: list is incomplete)
- For Date/Time types:
- Choice between textual
Stringrepresentation and numeric alternatives (timestamps) withshapeproperty (shape can beJsonFormat.Shape.STRINGorJsonFormat.Shape.NUMBER; for some types alsoJsonFormat.Shape.ARRAY) - {code timezone} may be used to override either Jackson default (UTC) or time-date values timezone
- {code lenient} may be used to enable/disable lenient (vs strict) parsing
of values (default being
lenient - For
DateorCalendar:patternmay specifySimpleDateFormat-compatible pattern definitions. - For
java.time.*types:patternmay specifyDateTimeFormatter-compatible pattern definitions. - For
org.joda.time.*typespatternmay specify Joda'sorg.joda.time.format.DateTimeFormat-compatible pattern definitions.
- Choice between textual
Enums: ShapesJsonFormat.Shape.STRINGandJsonFormat.Shape.NUMBERcan be used to change between numeric (index) and textual (name ortoString()); but it is also possible to useJsonFormat.Shape.OBJECTto serialize (but not deserialize)Enums 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.Collections can be serialized as (and deserialized from) JSON Objects, ifJsonFormat.Shape.OBJECTis used. NOTE: can ONLY be used as class annotation; will not work as per-property annotation.Numbersubclasses can useJsonFormat.Shape.STRINGto serialize to a string. This is useful to prevent large numeric values from being rounded to their closest double values when deserialized by JSON parsers (for instanceJSON.parse()in web browsers) that do not support numbers with more than 53 bits of precision. When serializingNumberto a string, it is possible to specify radix, the numeric base used to output the number in.They can also be serialized to full objects if
JsonFormat.Shape.OBJECTis 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.
- Since:
- 2.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSet of features that can be enabled/disabled for property annotated.static classstatic enumValue enumeration used for indicating preferred Shape; translates loosely to JSON types, with some extra values to indicate less precise choices (i.e.static classHelper class used to contain information from a singleJsonFormatannotation. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionProperty that indicates whether "lenient" handling should be enabled or disabled.Localeto use for serialization (if needed).Datatype-specific additional piece of configuration that may be used to further refine formatting aspects.intProperty that indicates the numeric base used to outputNumberproperties whenJsonFormat.Shape.STRINGis specified.Structure to use for serialization: definition of mapping depends on datatype, but usually has straight-forward counterpart in data format (JSON).TimeZoneto use for serialization (if needed).Set ofJsonFormat.Features to explicitly enable with respect to handling of annotated property.Set ofJsonFormat.Features to explicitly disable with respect to handling of annotated property. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringValue that indicates that defaultLocale(from deserialization or serialization context) should be used: annotation does not define value to use.static final intThis is a marker signaling that a configured default radix should be used, which typically means 10, when serializingNumberproperties withJsonFormat.Shape.STRING.static final StringValue 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:
-
DEFAULT_RADIX
static final int DEFAULT_RADIXThis is a marker signaling that a configured default radix should be used, which typically means 10, when serializingNumberproperties withJsonFormat.Shape.STRING.- Since:
- 2.21
- 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 forDateserialization; however, exact use is determined by specificJsonSerializer(2.x) /ValueSerializer(3.x)- 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 localeLocaleto use for serialization (if needed). Special value ofDEFAULT_LOCALEcan 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 timezoneTimeZoneto use for serialization (if needed). Special value ofDEFAULT_TIMEZONEcan 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.
- Since:
- 2.9
- Default:
DEFAULT
-
radix
int radixProperty that indicates the numeric base used to outputNumberproperties whenJsonFormat.Shape.STRINGis specified. For example, if 2 is used, then the output will be a binary representation of a number as a string, and with 16, the number will be outputted in the hexadecimal form.- Since:
- 2.21
- Default:
-1
-
with
JsonFormat.Feature[] withSet ofJsonFormat.Features to explicitly enable with respect to handling of annotated property. This will have precedence over possible global configuration.- Since:
- 2.6
- Default:
{}
-
without
JsonFormat.Feature[] withoutSet ofJsonFormat.Features to explicitly disable with respect to handling of annotated property. This will have precedence over possible global configuration.- Since:
- 2.6
- Default:
{}
-