xref: /aosp_15_r20/external/jackson-annotations/README.md (revision 2bf6642460ffb10303bd46207a4555f36d9e5945)
1*2bf66424SCole Faust# Overview
2*2bf66424SCole Faust
3*2bf66424SCole FaustThis project contains general purpose annotations for
4*2bf66424SCole FaustJackson Data Processor, used on value and handler types.
5*2bf66424SCole FaustThe only annotations not included are ones that require dependency
6*2bf66424SCole Faustto the [Databind package](../../../jackson-databind).
7*2bf66424SCole Faust
8*2bf66424SCole FaustProject contains versions 2.0 and above: source code for earlier (1.x) versions is available from [Jackson-1](../../../jackson-1) SVN repository.
9*2bf66424SCole FaustNote that with version 1.x these annotations were part of the 'core jar'.
10*2bf66424SCole Faust
11*2bf66424SCole Faust[Full Listing of Jackson Annotations](../../wiki/Jackson-Annotations) details all available annotations;
12*2bf66424SCole Faust[Project Wiki](../../wiki) gives more details.
13*2bf66424SCole Faust
14*2bf66424SCole FaustProject is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
15*2bf66424SCole Faust
16*2bf66424SCole Faust
17*2bf66424SCole Faust[![Build Status](https://travis-ci.org/FasterXML/jackson-annotations.png?branch=master)](https://travis-ci.org/FasterXML/jackson-annotations) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-annotations/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-annotations)
18*2bf66424SCole Faust[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.fasterxml.jackson.core/jackson-annotations/badge.svg)](http://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-annotations)
19*2bf66424SCole Faust
20*2bf66424SCole Faust-----
21*2bf66424SCole Faust
22*2bf66424SCole Faust## Usage, general
23*2bf66424SCole Faust
24*2bf66424SCole Faust### Improvements over typical Java annotations
25*2bf66424SCole Faust
26*2bf66424SCole FaustIn addition to regular usage (see below), there are couple of noteworthy improvements Jackson does:
27*2bf66424SCole Faust
28*2bf66424SCole Faust* [Mix-in annotations](../../wiki/Mixin-Annotations) allow associating annotations on third-party classes ''without modifying classes''.
29*2bf66424SCole Faust* Jackson annotations support full inheritance: meaning that you can ''override annotation definitions'', and not just class annotations but also method/field annotations!
30*2bf66424SCole Faust
31*2bf66424SCole Faust### Maven, Java package
32*2bf66424SCole Faust
33*2bf66424SCole FaustAll annotations are in Java package `com.fasterxml.jackson.annotation`.
34*2bf66424SCole FaustTo use annotations, you need to use Maven dependency:
35*2bf66424SCole Faust
36*2bf66424SCole Faust```xml
37*2bf66424SCole Faust<dependency>
38*2bf66424SCole Faust  <groupId>com.fasterxml.jackson.core</groupId>
39*2bf66424SCole Faust  <artifactId>jackson-annotations</artifactId>
40*2bf66424SCole Faust  <version>${jackson-annotations-version}</version>
41*2bf66424SCole Faust</dependency>
42*2bf66424SCole Faust```
43*2bf66424SCole Faust
44*2bf66424SCole Faustor download jars from Maven repository (or via quick links on [Wiki](../../wiki))
45*2bf66424SCole Faust
46*2bf66424SCole Faust## Usage, simple
47*2bf66424SCole Faust
48*2bf66424SCole FaustLet's start with simple use cases: renaming or ignoring properties, and modifying types that are used.
49*2bf66424SCole Faust
50*2bf66424SCole FaustNote: while examples only show field properties, same annotations would work with method (getter/setter) properties.
51*2bf66424SCole Faust
52*2bf66424SCole Faust### Annotations for renaming properties
53*2bf66424SCole Faust
54*2bf66424SCole FaustOne of most common tasks is to change JSON name used for a property: for example:
55*2bf66424SCole Faust
56*2bf66424SCole Faust```java
57*2bf66424SCole Faustpublic class Name {
58*2bf66424SCole Faust  @JsonProperty("firstName")
59*2bf66424SCole Faust  public String _first_name;
60*2bf66424SCole Faust}
61*2bf66424SCole Faust```
62*2bf66424SCole Faust
63*2bf66424SCole Faustwould result in JSON like:
64*2bf66424SCole Faust
65*2bf66424SCole Faust```json
66*2bf66424SCole Faust{ "firstName" : "Bob" }
67*2bf66424SCole Faust```
68*2bf66424SCole Faust
69*2bf66424SCole Faustinstead of
70*2bf66424SCole Faust
71*2bf66424SCole Faust```json
72*2bf66424SCole Faust{ "_first_name" : "Bob" }
73*2bf66424SCole Faust```
74*2bf66424SCole Faust
75*2bf66424SCole Faust### Annotations for Ignoring properties
76*2bf66424SCole Faust
77*2bf66424SCole FaustSometimes POJOs contain properties that you do not want to write out, so you can do:
78*2bf66424SCole Faust
79*2bf66424SCole Faust```java
80*2bf66424SCole Faustpublic class Value {
81*2bf66424SCole Faust  public int value;
82*2bf66424SCole Faust  @JsonIgnore public int internalValue;
83*2bf66424SCole Faust}
84*2bf66424SCole Faust```
85*2bf66424SCole Faust
86*2bf66424SCole Faustand get JSON like:
87*2bf66424SCole Faust
88*2bf66424SCole Faust```json
89*2bf66424SCole Faust{ "value" : 42 }
90*2bf66424SCole Faust```
91*2bf66424SCole Faust
92*2bf66424SCole Faustor, you may get properties in JSON that you just want to skip: if so, you can use:
93*2bf66424SCole Faust
94*2bf66424SCole Faust```java
95*2bf66424SCole Faust@JsonIgnoreProperties({ "extra", "uselessValue" })
96*2bf66424SCole Faustpublic class Value {
97*2bf66424SCole Faust  public int value;
98*2bf66424SCole Faust}
99*2bf66424SCole Faust```
100*2bf66424SCole Faust
101*2bf66424SCole Faustwhich would be able to handle JSON like:
102*2bf66424SCole Faust
103*2bf66424SCole Faust```json
104*2bf66424SCole Faust{ "value" : 42, "extra" : "fluffy", "uselessValue" : -13 }
105*2bf66424SCole Faust```
106*2bf66424SCole Faust
107*2bf66424SCole FaustFinally, you may even want to just ignore any "extra" properties from JSON (ones for which there is no counterpart in POJO). This can be done by adding:
108*2bf66424SCole Faust
109*2bf66424SCole Faust```java
110*2bf66424SCole Faust@JsonIgnoreProperties(ignoreUnknown=true)
111*2bf66424SCole Faustpublic class PojoWithAny {
112*2bf66424SCole Faust  public int value;
113*2bf66424SCole Faust}
114*2bf66424SCole Faust```
115*2bf66424SCole Faust
116*2bf66424SCole Faust### Annotations for choosing more/less specific types
117*2bf66424SCole Faust
118*2bf66424SCole FaustSometimes the type Jackson uses when reading or writing a property is not quite what you want:
119*2bf66424SCole Faust
120*2bf66424SCole Faust* When reading (deserializing), declared type may be a general type, but you know which exact implementation type to use
121*2bf66424SCole Faust* When writing (serializing), Jackson will by default use the specific runtime type; but you may not want to include all information from that type but rather just contents of its supertype.
122*2bf66424SCole Faust
123*2bf66424SCole FaustThese cases can be handled by following annotations:
124*2bf66424SCole Faust
125*2bf66424SCole Faust```java
126*2bf66424SCole Faustpublic class ValueContainer {
127*2bf66424SCole Faust  // although nominal type is 'Value', we want to read JSON as 'ValueImpl'
128*2bf66424SCole Faust  @JsonDeserialize(as=ValueImpl.class)
129*2bf66424SCole Faust  public Value value;
130*2bf66424SCole Faust
131*2bf66424SCole Faust  // although runtime type may be 'AdvancedType', we really want to serialize
132*2bf66424SCole Faust  // as 'BasicType'; two ways to do this:
133*2bf66424SCole Faust  @JsonSerialize(as=BasicType.class)
134*2bf66424SCole Faust  // or could also use: @JsonSerialize(typing=Typing.STATIC)
135*2bf66424SCole Faust  public BasicType another;
136*2bf66424SCole Faust}
137*2bf66424SCole Faust```
138*2bf66424SCole Faust
139*2bf66424SCole Faust-----
140*2bf66424SCole Faust
141*2bf66424SCole Faust## Usage, intermediate
142*2bf66424SCole Faust
143*2bf66424SCole Faust### Using constructors or factory methods
144*2bf66424SCole Faust
145*2bf66424SCole FaustBy default, Jackson tries to use the "default" constructor (one that takes no arguments), when creating value instances. But you can also choose to use another constructor, or a static factory method to create instance. To do this, you will need to use annotation `@JsonCreator`, and possibly `@JsonProperty` annotations to bind names to arguments:
146*2bf66424SCole Faust
147*2bf66424SCole Faust```java
148*2bf66424SCole Faustpublic class CtorPOJO {
149*2bf66424SCole Faust   private final int _x, _y;
150*2bf66424SCole Faust
151*2bf66424SCole Faust   @JsonCreator
152*2bf66424SCole Faust   public CtorPOJO(@JsonProperty("x") int x, @JsonProperty("y") int y) {
153*2bf66424SCole Faust      _x = x;
154*2bf66424SCole Faust      _y = y;
155*2bf66424SCole Faust   }
156*2bf66424SCole Faust}
157*2bf66424SCole Faust```
158*2bf66424SCole Faust
159*2bf66424SCole Faust`@JsonCreator` can be used similarly for static factory methods.
160*2bf66424SCole FaustBut there is also an alternative usage, which is so-called "delegating" creator:
161*2bf66424SCole Faust
162*2bf66424SCole Faust```java
163*2bf66424SCole Faustpublic class DelegatingPOJO {
164*2bf66424SCole Faust   private final int _x, _y;
165*2bf66424SCole Faust
166*2bf66424SCole Faust   @JsonCreator
167*2bf66424SCole Faust   public DelegatingPOJO(Map<String,Object> delegate) {
168*2bf66424SCole Faust      _x = (Integer) delegate.get("x");
169*2bf66424SCole Faust      _y = (Integer) delegate.get("y");
170*2bf66424SCole Faust   }
171*2bf66424SCole Faust}
172*2bf66424SCole Faust```
173*2bf66424SCole Faust
174*2bf66424SCole Faustthe difference being that the creator method can only take one argument, and that argument must NOT have `@JsonProperty` annotation.
175*2bf66424SCole Faust
176*2bf66424SCole Faust### Handling polymorphic types
177*2bf66424SCole Faust
178*2bf66424SCole FaustIf you need to read and write values of Objects where there are multiple possible subtypes (i.e. ones that exhibit polymorphism), you may need to enable inclusion of type information. This is needed so that Jackson can read back correct Object type when deserializing (reading JSON into Objects).
179*2bf66424SCole FaustThis can be done by adding `@JsonTypeInfo` annotation on ''base class'':
180*2bf66424SCole Faust
181*2bf66424SCole Faust```java
182*2bf66424SCole Faust// Include Java class name ("com.myempl.ImplClass") as JSON property "class"
183*2bf66424SCole Faust@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
184*2bf66424SCole Faustpublic abstract class BaseClass {
185*2bf66424SCole Faust}
186*2bf66424SCole Faust
187*2bf66424SCole Faustpublic class Impl1 extends BaseClass {
188*2bf66424SCole Faust  public int x;
189*2bf66424SCole Faust}
190*2bf66424SCole Faustpublic class Impl2 extends BaseClass {
191*2bf66424SCole Faust  public String name;
192*2bf66424SCole Faust}
193*2bf66424SCole Faust
194*2bf66424SCole Faustpublic class PojoWithTypedObjects {
195*2bf66424SCole Faust  public List<BaseClass> items;
196*2bf66424SCole Faust}
197*2bf66424SCole Faust```
198*2bf66424SCole Faust
199*2bf66424SCole Faustand this could result in serialized JSON like:
200*2bf66424SCole Faust
201*2bf66424SCole Faust```json
202*2bf66424SCole Faust{ "items" : [
203*2bf66424SCole Faust  { "class":"Impl2", "name":"Bob" },
204*2bf66424SCole Faust  { "class":"Impl1", "x":13 }
205*2bf66424SCole Faust]}
206*2bf66424SCole Faust```
207*2bf66424SCole Faust
208*2bf66424SCole FaustNote that this annotation has lots of configuration possibilities: for more information check out
209*2bf66424SCole Faust[Intro to polymorphic type handling](http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html)
210*2bf66424SCole Faust
211*2bf66424SCole Faust### Changing property auto-detection
212*2bf66424SCole Faust
213*2bf66424SCole FaustThe default Jackson property detection rules will find:
214*2bf66424SCole Faust
215*2bf66424SCole Faust* All ''public'' fields
216*2bf66424SCole Faust* All ''public'' getters ('getXxx()' methods)
217*2bf66424SCole Faust* All setters ('setXxx(value)' methods), ''regardless of visibility'')
218*2bf66424SCole Faust
219*2bf66424SCole FaustBut if this does not work, you can change visibility levels by using annotation `@JsonAutoDetect`.
220*2bf66424SCole FaustIf you wanted, for example, to auto-detect ALL fields (similar to how packages like GSON work), you could do:
221*2bf66424SCole Faust
222*2bf66424SCole Faust```java
223*2bf66424SCole Faust@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
224*2bf66424SCole Faustpublic class POJOWithFields {
225*2bf66424SCole Faust  private int value;
226*2bf66424SCole Faust}
227*2bf66424SCole Faust```
228*2bf66424SCole Faust
229*2bf66424SCole Faustor, to disable auto-detection of fields altogether:
230*2bf66424SCole Faust
231*2bf66424SCole Faust```java
232*2bf66424SCole Faust@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.NONE)
233*2bf66424SCole Faustpublic class POJOWithNoFields {
234*2bf66424SCole Faust  // will NOT be included, unless there is access 'getValue()'
235*2bf66424SCole Faust  public int value;
236*2bf66424SCole Faust}
237*2bf66424SCole Faust```
238*2bf66424SCole Faust
239*2bf66424SCole Faust-----
240*2bf66424SCole Faust
241*2bf66424SCole Faust# Further reading
242*2bf66424SCole Faust
243*2bf66424SCole FaustProject-specific documentation:
244*2bf66424SCole Faust
245*2bf66424SCole Faust* [Full Listing of Jackson Annotations](../../wiki/Jackson-Annotations) details all available annotations.
246*2bf66424SCole Faust* [Other documentation](../../wiki)
247*2bf66424SCole Faust
248*2bf66424SCole FaustUsage:
249*2bf66424SCole Faust
250*2bf66424SCole Faust* You can make Jackson 2 use Jackson 1 annotations with [jackson-legacy-introspector](https://github.com/Laures/jackson-legacy-introspector)
251*2bf66424SCole Faust
252*2bf66424SCole FaustRelated:
253*2bf66424SCole Faust
254*2bf66424SCole Faust* [Databinding](https://github.com/FasterXML/jackson-databind) module has more documentation, since it is the main user of annotations.
255*2bf66424SCole FaustIn addition, here are other useful links:
256*2bf66424SCole Faust* [Jackson Project Home](http://wiki.fasterxml.com/JacksonHome)
257*2bf66424SCole Faust * [Annotation documentation at FasterXML Wiki](http://wiki.fasterxml.com/JacksonAnnotations) covers 1.x annotations as well as 2.0
258*2bf66424SCole Faust
259*2bf66424SCole Faust
260