@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER}) @Retention(value=RUNTIME) public @interface JsonProperty
Value ("") indicates that the name of field (or, derived name of an accessor method (setter / getter)) is to be used as the property name without any modifications; a non-empty value can be used to specify a different name. Property name refers to the name used externally, as the property name in JSON objects (as opposed to internal name of field in Java Object).
NOTE: annotation with non-empty Value can NOT be used if declaring multiple Java fields in a single declaration like:
public class POJO { \@JsonProperty("a") public int a, b, c;since it would associate same annotation for all fields, leading to name collision.
Starting with Jackson 2.6 this annotation may also be
used to change serialization of Enum
like so:
public enum MyEnum { @JsonProperty("theFirstValue") THE_FIRST_VALUE, @JsonProperty("another_value") ANOTHER_VALUE; }as an alternative to using
JsonValue
annotation.
Starting with Jackson 2.12 it is also possible to specify namespace
of property: this property is only used by certain format backends (most
notably XML).
Modifier and Type | Fields and Description |
---|---|
static int |
INDEX_UNKNOWN
Marker value used to indicate that no index has been specified.
|
static String |
USE_DEFAULT_NAME
Special value that indicates that handlers should use the default
name (derived from method or field name) for property.
|
Modifier and Type | Optional Element and Description |
---|---|
JsonProperty.Access |
access
Optional property that may be used to change the way visibility of
accessors (getter, field-as-getter) and mutators (constructor parameter,
setter, field-as-setter) is determined, either so that otherwise
non-visible accessors (like private getters) may be used; or that
otherwise visible accessors are ignored.
|
String |
defaultValue
Property that may be used to document expected default value
for the property: most often used as source information for generating
schemas (like JSON Schema or protobuf/thrift schema), or documentation.
|
int |
index
Property that indicates numerical index of this property (relative
to other properties specified for the Object).
|
String |
namespace
Optional namespace to use with data formats that support such
concept (specifically XML); if so, used with
value() to
construct fully-qualified name. |
boolean |
required
Property that indicates whether a value (which may be explicit
null) is expected for property during deserialization or not.
|
String |
value
Defines name of the logical property, i.e.
|
public static final String USE_DEFAULT_NAME
public static final int INDEX_UNKNOWN
public abstract String value
public abstract boolean required
BeanDeserialized
should indicate
this as a validity problem (usually by throwing an exception,
but this may be sent via problem handlers that can try to
rectify the problem, for example, by supplying a default
value).
Note that as of 2.6, this property is only used for Creator Properties, to ensure existence of property value in JSON: for other properties (ones injected using a setter or mutable field), no validation is performed. Support for those cases may be added in the future. State of this property is exposed via introspection, and its value is typically used by Schema generators, such as one for JSON Schema.
Also note that the required value must come directly from the
input source (e.g., JSON) and not from secondary sources, such as
defaulting logic or absent value providers.
If secondary sources are expected to supply the value,
this property should be set to false
. This is important because
validation of required
properties occurs before the application of
secondary sources.
public abstract int index
public abstract String defaultValue
It is possible that in future this annotation could be used for value
defaulting, and especially for default values of Creator properties,
since they support required()
in 2.6 and above.
public abstract JsonProperty.Access access
Default value os JsonProperty.Access.AUTO
which means that access is determined
solely based on visibility and other annotations.
Copyright © 2008–2024 FasterXML. All rights reserved.