• Stars
    star
    1,029
  • Rank 44,769 (Top 0.9 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 13 years ago
  • Updated 24 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Core annotations (annotations that only depend on jackson-core) for Jackson data processor

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. 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 repository.

Full Listing of Jackson Annotations details all available annotations; Project Wiki gives more details.

Project is licensed under Apache License 2.0.

Build (github) Maven Central Javadoc Tidelift OpenSSF  Scorecard


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 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:

<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)

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:

public class Name {
  @JsonProperty("firstName")
  public String _first_name;
}

would result in JSON like:

{ "firstName" : "Bob" }

instead of

{ "_first_name" : "Bob" }

Annotations for Ignoring properties

Sometimes POJOs contain properties that you do not want to write out, so you can do:

public class Value {
  public int value;
  @JsonIgnore public int internalValue;
}

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" })
public class Value {
  public int value;
}

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)
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:

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:

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:

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'':

@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:

{ "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

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:

@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
public class POJOWithFields {
  private int value;
}

or, to disable auto-detection of fields altogether:

@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 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.


Further reading

Project-specific documentation:

Backwards compatibility:

Related:

  • Databinding module has more documentation, since it is the main user of annotations.

More Repositories

1

jackson

Main Portal page for the Jackson project
9,099
star
2

jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Java
3,522
star
3

jackson-core

Core part of Jackson that defines Streaming API as well as basic shared abstractions
Java
2,263
star
4

jackson-module-kotlin

Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Kotlin
1,123
star
5

jackson-docs

Documentation for the Jackson JSON processor.
727
star
6

jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Java
570
star
7

jackson-module-scala

Add-on module for Jackson (https://github.com/FasterXML/jackson) to support Scala-specific datatypes
Scala
501
star
8

jackson-dataformats-text

Uber-project for (some) standard Jackson textual format backends: csv, properties, yaml (xml to be added in future)
Java
405
star
9

jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Java
400
star
10

jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
Java
369
star
11

jackson-datatype-hibernate

Add-on module for Jackson JSON processor which handles Hibernate (https://www.hibernate.org/) datatypes; and specifically aspects of lazy-loading
Java
318
star
12

jackson-dataformats-binary

Uber-project for standard Jackson binary format backends: avro, cbor, ion, protobuf, smile
Java
313
star
13

aalto-xml

Ultra-high performance non-blocking XML processor (Stax API + extensions)
Java
292
star
14

java-classmate

Library for introspecting generic type information of types, member/static methods, fields. Especially useful for POJO/Bean introspection.
Java
258
star
15

jackson-jr

Stand-alone data-binding module designed as a light-weight (and -featured) alternative to `jackson-databind`: will only deal with "Maps, Lists, Strings, wrappers and Java Beans" (jr-objects), or simple read-only trees (jr-stree)
Java
239
star
16

woodstox

The gold standard Stax XML API implementation. Now at Github.
Java
226
star
17

jackson-dataformat-csv

(DEPRECATED) -- moved under: https://github.com/FasterXML/jackson-dataformats-text
Java
194
star
18

jackson-modules-base

Uber-project for foundational modules of Jackson that build directly on core components but nothing else; not including data format or datatype modules
Java
167
star
19

jackson-datatype-joda

Extension module to properly support full datatype set of Joda datetime library
Java
140
star
20

jackson-dataformat-yaml

Jackson module to add YAML backend (parser/generator adapters)
Java
138
star
21

jackson-jaxrs-providers

Multi-module project that contains Jackson-based "old" JAX-RS (ones under `javax.ws.rs`) providers for JSON, XML, YAML, Smile, CBOR formats
Java
111
star
22

smile-format-specification

New home for Smile format (https://en.wikipedia.org/wiki/Smile_(data_interchange_format))
92
star
23

jackson-datatype-jsr310

(DEPRECATED) -- moved under `jackson-modules-java8` repo: https://github.com/FasterXML/jackson-modules-java8
Java
92
star
24

jackson-module-afterburner

(DEPRECATED) -- moved under `jackson-modules-base`
Java
91
star
25

jackson-datatypes-collections

Jackson project that contains various collection-oriented datatype libraries: Eclipse Collections, Guava, HPPC, PCollections
Java
77
star
26

jackson-datatype-guava

(DEPRECATED) -- moved under `jackson-datatypes-collections`
Java
68
star
27

jackson-datatype-jdk8

(DEPRECATED) -- moved under `jackson-modules-java8`
Java
57
star
28

jackson-bom

Bill of materials POM for Jackson projects
Logos
54
star
29

jackson-datatype-json-org

(DEPRECATED) Support for org.json data types, to ease migration out of code that uses them
Java
49
star
30

jackson-dataformat-smile

(DEPRECATED) -- moved under `jackson-dataformats-binary
Java
44
star
31

jackson-module-jaxb-annotations

(DEPRECATED!!!) Moved to: https://github.com/FasterXML/jackson-modules-base/
Java
44
star
32

StaxMate

StaxMate: Automatic Shifting for Streaming XML Processing
Java
41
star
33

stax2-api

Extension API for Stax, Java pull-parsing API (STreaming Api for Xml)
Java
40
star
34

jackson-dataformat-cbor

(DEPRECATED) -- moved under `jackson-dataformats-binary`
Java
38
star
35

jackson-dataformat-avro

(DEPRECATED) -- moved under `jackson-dataformats-binary
Java
38
star
36

jackson-module-parameter-names

(DEPRECATED) -- moved under `jackson-modules-java8`
Java
33
star
37

jackson-dataformat-protobuf

(DEPRECATED) -- moved under `jackson-dataformats-binary`
Java
32
star
38

jackson-module-mrbean

(DEPRECATED) -- moved under `jackson-modules-base`
Java
27
star
39

jackson-datatypes-misc

Collection of common Jackson datatype modules not part of other multi-project repos
Java
22
star
40

TransiStore

Distributed data store for transient (temporary, time-bound) data. Based on ClusterMate/StoreMate
Java
21
star
41

jackson-datatype-jsr353

(DEPRECATED) -- moved under `jackson-datatypes-misc` https://github.com/FasterXML/jackson-datatypes-misc/
Java
19
star
42

jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18
star
43

jackson-benchmarks

Project that contains JMH-based micro-benchmarks to help with optimizations
Java
15
star
44

jackson-jakarta-rs-providers

Multi-module project that contains Jackson-based "new" Jakarta-RS (nee "JAX-RS" -- ones under `jakarta.ws.rs`) providers for JSON, XML, YAML, Smile, CBOR formats
Java
14
star
45

jackson-dataformat-properties

(DEPRECATED) -- moved under `jackson-dataformats-text`
Java
13
star
46

jackson-datatype-hppc

(DEPRECATED) -- moved under `jackson-datatypes-collections`
Java
9
star
47

oss-parent

Grandpa pom for all projects under FasterXML git umbrella
8
star
48

jackson-datatype-jdk7

(DEPRECATED) -- included in `jackson-databind` as of Jackson 2.7
Java
7
star
49

jvm-json-benchmark

Performance benchmark suite that compares data-binding (JSON<->POJO) performance of Java JSON libraries. Uses Japex benchmark framework for running tests and visualizing results.
Java
7
star
50

Hacktoberfest2020

Central repository for FasterXML activities related to Hacktoberfest 2020 by DigitalOcean (https://hacktoberfest.digitalocean.com/)
6
star
51

jackson-module-guice

(DEPRECATED) -- moved under `jackson-modules-base`
Java
5
star
52

jackson3-dev

Repository for planning and tracking development of Jackson 3.0, with bigger API changes
5
star
53

jackson-module-paranamer

(DEPRECATED) -- moved under `jackson-modules-base`
Java
5
star
54

jackson-parent

Parent pom for all core Jackson components (but only those -- users should use `jackson-bom`)
5
star
55

jackson-schema-maven-plugin

Maven plug-in for generation JSON Schemas using Jackson library and extension modules
Java
4
star
56

jackson-jdk11-compat-test

Test project for verifying Jackson's support of JDK9+ Module system
Java
3
star
57

jackson-dataformat-thrift

Not Yet A Thing -- placeholder for possible future implementation
Java
3
star
58

jackson-integration-tests

Project that contains integration tests across Jackson components
Java
3
star
59

jackson-jdk17-compat-test

Test suite for JDK 16 compatibility of Jackson components, mainly jackson-databind
Java
3
star
60

jackson-tools

Collection of command-line tools related to Jackson data processor, such as format decoders
Java
2
star
61

jackson-dataformat-ini

(Not Yet a Thing -- Placholder!) Data format implementation for "ini files", used on Windows, Python
Java
2
star
62

OmniPipe

Distributed data queue implementation that builds on ClusterMate/StoreMate foundation
2
star
63

jackson-jdk6-compat-test

Test project to verify JDK6 compatibility of Jackson versions 2.7 and anbove
Java
2
star
64

jackson-module-osgi

(DEPRECATED) -- moved under `jackson-modules-base`
Java
2
star
65

jackson-gh-actions

Repository for reusable Github workflow actions for Jackson project
2
star
66

Woodstox4

Backup repository for older versions of Woodstox (pre-5.0), migrated from Codehaus
Java
1
star