Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork338
Core annotations (annotations that only depend on jackson-core) for Jackson data processor
License
FasterXML/jackson-annotations
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project contains general purpose annotations forJackson Data Processor, used on value and handler types.The only annotations not included are ones that require dependencyto theDatabind package.Note that only annotations themselves (and related value classes) are included, but no functionalitythat uses annotations.
Project contains versions 2.0 and above: source code for earlier (1.x) versions is available fromJackson-1 repository.
Full Listing of Jackson Annotations details all available annotations;Project Wiki gives more details.
Project is licensed underApache License 2.0.
Release notes for this component/repo are available inJackson Releases page (unified for all official Jackson components).
NOTE: Annotations module is released with "simple" version like 2.20 without "patch" number -- except for rare case of critical fixes.This change occurred with Jackson 2.20: prior to it, patch number was included but was meaningless: every patch version of a minor release was identical (so,2.18.0 and, say,2.18.4 were identical).
NOTE: Jackson 3.x components rely on 2.x annotations; there are no separate 3.xjackson-annotations versions released (there were RC versions up to 3.0-rc5 but not after that).
In addition to regular usage (see below), there are couple of noteworthy improvements Jackson does:
- Mix-in 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!
- Jackson annotations will also be inherited from interfaces:
- precedence between base class and implemented interfaces is such that base-class has lower precedence than interfaces;
- if multiple interfaces are implemented, then the precedence depends on the order as returned by the JVM.
All annotations are in Java packagecom.fasterxml.jackson.annotation.To use annotations, you need to use Maven dependency:
<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 onWiki)
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.
One of most common tasks is to change JSON name used for a property: for example:
publicclassName {@JsonProperty("firstName")publicString_first_name;}
would result in JSON like:
{"firstName" :"Bob" }instead of
{"_first_name" :"Bob" }Sometimes POJOs contain properties that you do not want to write out, so you can do:
publicclassValue {publicintvalue;@JsonIgnorepublicintinternalValue;}
and get JSON like:
{"value" :42 }or, you may get properties in JSON that you just want to skip: if so, you can use:
@JsonIgnoreProperties({"extra","uselessValue" })publicclassValue {publicintvalue;}
which would be able to handle JSON like:
{"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:
@JsonIgnoreProperties(ignoreUnknown=true)publicclassPojoWithAny {publicintvalue;}
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:
publicclassValueContainer {// although nominal type is 'Value', we want to read JSON as 'ValueImpl'@JsonDeserialize(as=ValueImpl.class)publicValuevalue;// 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)publicBasicTypeanother;}
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:
publicclassCtorPOJO {privatefinalint_x,_y;@JsonCreatorpublicCtorPOJO(@JsonProperty("x")intx,@JsonProperty("y")inty) {_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:
publicclassDelegatingPOJO {privatefinalint_x,_y;@JsonCreatorpublicDelegatingPOJO(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.
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'':
@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 onlypublicabstractclassVehicle {}publicclassCarextendsVehicle {publicStringlicensePlate;}publicclassAeroplaneextendsVehicle {publicintwingSpan;}publicclassPojoWithTypedObjects {publicList<Vehicle>items;}
which gives serialized JSON like:
{"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 arededuced basedon the fields available. Exceptions will be raised if subtypes do not have a distinct signature of fieldnames or JSON doesnot resolve to single known signature.
Note that@JsonTypeInfo has lots of configuration possibilities: for more information check outIntro to polymorphic type handling
The default Jackson property detection rules will find:
- All
publicfields - All
publicgetters (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:
@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)publicclassPOJOWithFields {privateintvalue;}
or, to disable auto-detection of fields altogether:
@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.NONE)publicclassPOJOWithNoFields {// will NOT be included, unless there is access 'getValue()'publicintvalue;}
Jackson components are supported by the Jackson community through mailing lists, Gitter forum, Github issues. SeeParticipation, Contributing for full details.
Available as part of theTidelift Subscription.
The maintainers ofjackson-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.
Project-specific documentation:
- Full Listing of Jackson Annotations details all available annotations.
- jackson-annotations wiki
Backwards compatibility:
- You can make Jackson 2 use Jackson 1 annotations withjackson-legacy-introspector
Related:
- Databinding module has more documentation, since it is the main user of annotations.
About
Core annotations (annotations that only depend on jackson-core) for Jackson data processor
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.