1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
# Overview
This project contains general purpose annotations for
Jackson Data Processor, used on value and handler types.
The only annotations not included are ones that require dependency
to the [Databind package](../../../jackson-databind).
Note that only annotations themselves (and related value classes) are included, but no functionality
that uses annotations.
Project contains versions 2.0 and above: source code for earlier (1.x) versions is available from [Jackson-1](../../../jackson-1) repository.
[Full Listing of Jackson Annotations](../../wiki/Jackson-Annotations) details all available annotations;
[Project Wiki](../../wiki) gives more details.
Project is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
[](https://github.com/FasterXML/jackson-annotations/actions/workflows/main.yml)
[](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-annotations)
[](http://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-annotations)
[](https://tidelift.com/subscription/pkg/maven-com-fasterxml-jackson-core-jackson-annotations?utm_source=maven-com-fasterxml-jackson-core-jackson-annotations&utm_medium=referral&utm_campaign=readme)
-----
## Usage, general
### Improvements over typical Java annotations
In addition to regular usage (see below), there are couple of noteworthy improvements Jackson does:
* [Mix-in annotations](../../wiki/Mixin-Annotations) allow associating annotations on third-party classes ''without modifying classes''.
* Jackson annotations support full inheritance: meaning that you can ''override annotation definitions'', and not just class annotations but also method/field annotations!
### Maven, Java package
All annotations are in Java package `com.fasterxml.jackson.annotation`.
To use annotations, you need to use Maven dependency:
```xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations-version}</version>
</dependency>
```
or download jars from Maven repository (or via quick links on [Wiki](../../wiki))
## Usage, simple
Let's start with simple use cases: renaming or ignoring properties, and modifying types that are used.
Note: while examples only show field properties, same annotations would work with method (getter/setter) properties.
### Annotations for renaming properties
One of most common tasks is to change JSON name used for a property: for example:
```java
public class Name {
@JsonProperty("firstName")
public String _first_name;
}
```
would result in JSON like:
```json
{ "firstName" : "Bob" }
```
instead of
```json
{ "_first_name" : "Bob" }
```
### Annotations for Ignoring properties
Sometimes POJOs contain properties that you do not want to write out, so you can do:
```java
public class Value {
public int value;
@JsonIgnore public int internalValue;
}
```
and get JSON like:
```json
{ "value" : 42 }
```
or, you may get properties in JSON that you just want to skip: if so, you can use:
```java
@JsonIgnoreProperties({ "extra", "uselessValue" })
public class Value {
public int value;
}
```
which would be able to handle JSON like:
```json
{ "value" : 42, "extra" : "fluffy", "uselessValue" : -13 }
```
Finally, 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:
```java
@JsonIgnoreProperties(ignoreUnknown=true)
public class PojoWithAny {
public int value;
}
```
### Annotations for choosing more/less specific types
Sometimes the type Jackson uses when reading or writing a property is not quite what you want:
* When reading (deserializing), declared type may be a general type, but you know which exact implementation type to use
* 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.
These cases can be handled by following annotations:
```java
public class ValueContainer {
// although nominal type is 'Value', we want to read JSON as 'ValueImpl'
@JsonDeserialize(as=ValueImpl.class)
public Value value;
// although runtime type may be 'AdvancedType', we really want to serialize
// as 'BasicType'; two ways to do this:
@JsonSerialize(as=BasicType.class)
// or could also use: @JsonSerialize(typing=Typing.STATIC)
public BasicType another;
}
```
-----
## Usage, intermediate
### Using constructors or factory methods
By 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:
```java
public class CtorPOJO {
private final int _x, _y;
@JsonCreator
public CtorPOJO(@JsonProperty("x") int x, @JsonProperty("y") int y) {
_x = x;
_y = y;
}
}
```
`@JsonCreator` can be used similarly for static factory methods.
But there is also an alternative usage, which is so-called "delegating" creator:
```java
public class DelegatingPOJO {
private final int _x, _y;
@JsonCreator
public DelegatingPOJO(Map<String,Object> delegate) {
_x = (Integer) delegate.get("x");
_y = (Integer) delegate.get("y");
}
}
```
the difference being that the creator method can only take one argument, and that argument must NOT have `@JsonProperty` annotation.
### Handling polymorphic types
If 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).
This can be done by adding `@JsonTypeInfo` annotation on ''base class'':
```java
@JsonTypeInfo(use=Id.MINIMAL_CLASS, include=As.PROPERTY, property="type") // Include Java class simple-name as JSON property "type"
@JsonSubTypes({@Type(Car.class), @Type(Aeroplane.class)}) // Required for deserialization only
public abstract class Vehicle {
}
public class Car extends Vehicle {
public String licensePlate;
}
public class Aeroplane extends Vehicle {
public int wingSpan;
}
public class PojoWithTypedObjects {
public List<Vehicle> items;
}
```
which gives serialized JSON like:
```json
{ "items": [
{ "type": "Car", "licensePlate": "X12345" },
{ "type": "Aeroplane", "wingSpan": 13 }
]}
```
Alternatively, `@JsonTypeInfo(use=DEDUCTION)` can be used to avoid requiring the 'type' field. For deserialization, types are _deduced_ based
on the fields available. Exceptions will be raised if subtypes do not have a distinct signature of fieldnames or JSON does
not resolve to single known signature.
Note that `@JsonTypeInfo` has lots of configuration possibilities: for more information check out
[Intro to polymorphic type handling](http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html)
### Changing property auto-detection
The default Jackson property detection rules will find:
* All ''public'' fields
* All ''public'' getters ('getXxx()' methods)
* All setters ('setXxx(value)' methods), ''regardless of visibility'')
But if this does not work, you can change visibility levels by using annotation `@JsonAutoDetect`.
If you wanted, for example, to auto-detect ALL fields (similar to how packages like GSON work), you could do:
```java
@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
public class POJOWithFields {
private int value;
}
```
or, to disable auto-detection of fields altogether:
```java
@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.NONE)
public class POJOWithNoFields {
// will NOT be included, unless there is access 'getValue()'
public int value;
}
```
-----
## Support
### Community support
Jackson components are supported by the Jackson community through mailing lists, Gitter forum, Github issues. See [Participation, Contributing](../../../jackson#participation-contributing) for full details.
### Enterprise support
Available as part of the Tidelift Subscription.
The maintainers of `jackson-annotations` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/maven-com-fasterxml-jackson-core-jackson-annotations?utm_source=maven-com-fasterxml-jackson-core-jackson-annotations&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
-----
## Further reading
Project-specific documentation:
* [Full Listing of Jackson Annotations](../../wiki/Jackson-Annotations) details all available annotations.
* [jackson-annotations wiki](../../wiki)
Backwards compatibility:
* You can make Jackson 2 use Jackson 1 annotations with [jackson-legacy-introspector](https://github.com/Laures/jackson-legacy-introspector)
Related:
* [Databinding](https://github.com/FasterXML/jackson-databind) module has more documentation, since it is the main user of annotations.
|