blob: b8b11f969f495ef1083ff1e25d9de01484bb1114 [file] [log] [blame] [view]
Tatu Saloranta76437822012-03-26 20:01:27 -07001# Overview
Tatu Saloranta90c43522011-12-22 23:25:27 -08002
3This project contains the general-purpose data-binding functionality
Tatu670aad62013-08-28 14:37:02 -07004and tree-model for [Jackson Data Processor](http://wiki.fasterxml.com/JacksonHome).
Tatu62a02dc2013-08-16 11:51:26 -07005It builds on [core streaming parser/generator](../../../jackson-core) package,
Tatub4006fc2013-08-16 11:49:20 -07006and uses [Jackson Annotations](../../../jackson-annotations) for configuration.
Tatu Salorantaccd59e22013-08-26 16:48:56 -07007Project is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
Tatu Saloranta7ae53632012-03-03 09:08:35 -08008
Tatub4006fc2013-08-16 11:49:20 -07009While the original use case for Jackson was JSON data-binding, it can now be used for other data formats as well, as long as parser and generator implementations exist.
10Naming of classes uses word 'JSON' in many places even though there is no actual hard dependency to JSON format.
Tatu Saloranta7ae53632012-03-03 09:08:35 -080011
Tobias Preuss7b90af42014-10-29 09:29:10 +010012[![Build Status](https://travis-ci.org/FasterXML/jackson-databind.svg?branch=master)](https://travis-ci.org/FasterXML/jackson-databind) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-databind/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-databind)
Valeriy.Vyrva8dc5a342016-01-16 14:41:42 +060013[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.fasterxml.jackson.core/jackson-databind/badge.svg)](http://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind)
Tatu Saloranta71fc2082016-04-03 17:34:38 -070014[![Coverage Status](https://coveralls.io/repos/github/FasterXML/jackson-databind/badge.svg?branch=master)](https://coveralls.io/github/FasterXML/jackson-databind?branch=master)
Tatu Saloranta4a814692016-04-03 17:06:18 -070015
Tatu Saloranta477f0c82012-03-21 19:00:09 -070016-----
Tatu Saloranta6d4aa6c2012-03-03 09:29:58 -080017
Tatu Saloranta477f0c82012-03-21 19:00:09 -070018# Get it!
Tatu Saloranta7ae53632012-03-03 09:08:35 -080019
Tatu Saloranta7334d422012-03-21 19:20:38 -070020## Maven
21
Tatu Saloranta280603c2012-03-21 21:50:21 -070022Functionality of this package is contained in Java package `com.fasterxml.jackson.databind`, and can be used using following Maven dependency:
Tatu Saloranta7ae53632012-03-03 09:08:35 -080023
Artur Dryomovfb53adc2013-02-11 22:25:36 +030024```xml
Stephan Schroevers551f8972014-12-15 08:57:54 +010025<properties>
26 ...
27 <!-- Use the latest version whenever possible. -->
Tatu Salorantaf66872d2019-09-25 22:37:54 -070028 <jackson.version>2.10.0</jackson.version>
Stephan Schroevers551f8972014-12-15 08:57:54 +010029 ...
30</properties>
31
32<dependencies>
33 ...
34 <dependency>
35 <groupId>com.fasterxml.jackson.core</groupId>
36 <artifactId>jackson-databind</artifactId>
37 <version>${jackson.version}</version>
38 </dependency>
39 ...
40</dependencies>
Artur Dryomovfb53adc2013-02-11 22:25:36 +030041```
Tatu Saloranta7ae53632012-03-03 09:08:35 -080042
Philipp Scheitc598bf02014-09-22 13:36:36 +020043Since package also depends on `jackson-core` and `jackson-annotations` packages, you will need to download these if not using Maven; and you may also want to add them as Maven dependency to ensure that compatible versions are used.
Tatu Saloranta7334d422012-03-21 19:20:38 -070044If so, also add:
Tatu Saloranta935f2012012-03-03 09:12:52 -080045
Artur Dryomovfb53adc2013-02-11 22:25:36 +030046```xml
Stephan Schroevers551f8972014-12-15 08:57:54 +010047<dependencies>
48 ...
49 <dependency>
50 <!-- Note: core-annotations version x.y.0 is generally compatible with
51 (identical to) version x.y.1, x.y.2, etc. -->
52 <groupId>com.fasterxml.jackson.core</groupId>
53 <artifactId>jackson-annotations</artifactId>
54 <version>${jackson.version}</version>
55 </dependency>
56 <dependency>
57 <groupId>com.fasterxml.jackson.core</groupId>
58 <artifactId>jackson-core</artifactId>
59 <version>${jackson.version}</version>
60 </dependency>
61 ...
62<dependencies>
Artur Dryomovfb53adc2013-02-11 22:25:36 +030063```
Tatu Saloranta935f2012012-03-03 09:12:52 -080064
Tatu Saloranta15357e22012-11-11 15:58:28 -080065but note that this is optional, and only necessary if there are conflicts between jackson core dependencies through transitive dependencies.
66
Tatu Saloranta7334d422012-03-21 19:20:38 -070067## Non-Maven
68
Tatu Saloranta24d2a6c2014-01-04 21:19:09 -080069For non-Maven use cases, you download jars from [Central Maven repository](http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/).
Tatu Saloranta7334d422012-03-21 19:20:38 -070070
71Databind jar is also a functional OSGi bundle, with proper import/export declarations, so it can be use on OSGi container as is.
72
Tatu Saloranta477f0c82012-03-21 19:00:09 -070073-----
74
75# Use It!
Tatu Saloranta7ae53632012-03-03 09:08:35 -080076
Tatu62a02dc2013-08-16 11:51:26 -070077More comprehensive documentation can be found from [Jackson-docs](../../../jackson-docs) repository; as well as from [Wiki](../../wiki) of this project.
78But here are brief introductionary tutorials, in recommended order of reading.
Tatu Saloranta7ae53632012-03-03 09:08:35 -080079
Tatu Saloranta7334d422012-03-21 19:20:38 -070080## 1 minute tutorial: POJOs to JSON and back
Tatu Saloranta7ae53632012-03-03 09:08:35 -080081
Tatube9ae252013-08-16 11:46:38 -070082The most common usage is to take piece of JSON, and construct a Plain Old Java Object ("POJO") out of it. So let's start there. With simple 2-property POJO like this:
Tatu Saloranta3818c6e2012-03-21 22:05:11 -070083
Tatube9ae252013-08-16 11:46:38 -070084```java
85// Note: can use getters/setters as well; here we just use public fields directly:
86public class MyValue {
87 public String name;
88 public int age;
89 // NOTE: if using getters/setters, can keep fields `protected` or `private`
90}
91```
92
Mgamerz02dd6682014-02-03 18:58:52 -070093we will need a `com.fasterxml.jackson.databind.ObjectMapper` instance, used for all data-binding, so let's construct one:
Tatu Saloranta7ae53632012-03-03 09:08:35 -080094
Artur Dryomovfb53adc2013-02-11 22:25:36 +030095```java
96ObjectMapper mapper = new ObjectMapper(); // create once, reuse
97```
Tatu Saloranta54630e82012-03-21 21:36:08 -070098
Tatu Saloranta3818c6e2012-03-21 22:05:11 -070099The default instance is fine for our use -- we will learn later on how to configure mapper instance if necessary. Usage is simple:
Tatu Saloranta54630e82012-03-21 21:36:08 -0700100
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300101```java
102MyValue value = mapper.readValue(new File("data.json"), MyValue.class);
103// or:
104value = mapper.readValue(new URL("http://some.com/api/entry.json"), MyValue.class);
105// or:
106value = mapper.readValue("{\"name\":\"Bob\", \"age\":13}", MyValue.class);
107```
Tatu Saloranta7ae53632012-03-03 09:08:35 -0800108
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700109And if we want to write JSON, we do the reverse:
Tatu Saloranta7ae53632012-03-03 09:08:35 -0800110
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300111```java
112mapper.writeValue(new File("result.json"), myResultObject);
113// or:
114byte[] jsonBytes = mapper.writeValueAsBytes(myResultObject);
115// or:
116String jsonString = mapper.writeValueAsString(myResultObject);
117```
Tatu Saloranta7ae53632012-03-03 09:08:35 -0800118
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700119So far so good?
Tatu Saloranta24553d22012-03-21 21:49:26 -0700120
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700121## 3 minute tutorial: Generic collections, Tree Model
Tatu Saloranta7ae53632012-03-03 09:08:35 -0800122
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700123Beyond dealing with simple Bean-style POJOs, you can also handle JDK `List`s, `Map`s:
Tatu Saloranta7ae53632012-03-03 09:08:35 -0800124
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300125```java
126Map<String, Integer> scoreByName = mapper.readValue(jsonSource, Map.class);
127List<String> names = mapper.readValue(jsonSource, List.class);
Tatu Salorantaf9210682012-03-21 19:22:39 -0700128
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300129// and can obviously write out as well
130mapper.writeValue(new File("names.json"), names);
131```
Tatu Saloranta477f0c82012-03-21 19:00:09 -0700132
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700133as long as JSON structure matches, and types are simple.
134If you have POJO values, you need to indicate actual type (note: this is NOT needed for POJO properties with `List` etc types):
Tatu Saloranta7334d422012-03-21 19:20:38 -0700135
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300136```java
137Map<String, ResultValue> results = mapper.readValue(jsonSource,
Tatu Saloranta26f8fa22013-12-10 10:37:42 -0800138 new TypeReference<Map<String, ResultValue>>() { } );
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300139// why extra work? Java Type Erasure will prevent type detection otherwise
140```
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700141
142(note: no extra effort needed for serialization, regardless of generic types)
Tatu Saloranta7334d422012-03-21 19:20:38 -0700143
144But wait! There is more!
145
146While dealing with `Map`s, `List`s and other "simple" Object types (Strings, Numbers, Booleans) can be simple, Object traversal can be cumbersome.
Mark Woon69394342013-02-27 12:23:06 -0800147This is where Jackson's [Tree model](https://github.com/FasterXML/jackson-databind/wiki/JacksonTreeModel) can come in handy:
Tatu Saloranta7334d422012-03-21 19:20:38 -0700148
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300149```java
150// can be read as generic JsonNode, if it can be Object or Array; or,
151// if known to be Object, as ObjectNode, if array, ArrayNode etc:
152ObjectNode root = mapper.readTree("stuff.json");
153String name = root.get("name").asText();
154int age = root.get("age").asInt();
Tatu Salorantaf9210682012-03-21 19:22:39 -0700155
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300156// can modify as well: this adds child Object as property 'other', set property 'type'
157root.with("other").put("type", "student");
158String json = mapper.writeValueAsString(root);
Tatu Salorantaf9210682012-03-21 19:22:39 -0700159
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300160// with above, we end up with something like as 'json' String:
161// {
162// "name" : "Bob", "age" : 13,
163// "other" : {
164// "type" : "student"
Artem Prigodaae3f7b32014-11-20 18:14:20 +0300165// }
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300166// }
167```
Tatu Saloranta7334d422012-03-21 19:20:38 -0700168
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700169Tree Model can be more convenient than data-binding, especially in cases where structure is highly dynamic, or does not map nicely to Java classes.
170
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700171## 5 minute tutorial: Streaming parser, generator
Tatu Saloranta7334d422012-03-21 19:20:38 -0700172
173As convenient as data-binding (to/from POJOs) can be; and as flexible as Tree model can be, there is one more canonical processing model available: incremental (aka "streaming") model.
174It is the underlying processing model that data-binding and Tree Model both build upon, but it is also exposed to users who want ultimate performance and/or control over parsing or generation details.
175
Mark Woon69394342013-02-27 12:23:06 -0800176For in-depth explanation, look at [Jackson Core component](https://github.com/FasterXML/jackson-core).
Cowtowncoder595c8952015-05-12 17:30:47 -0700177But let's look at a simple teaser to whet your appetite.
Tatu Saloranta48b4b012012-03-21 19:45:56 -0700178
Cowtowncoder595c8952015-05-12 17:30:47 -0700179```java
180JsonFactory f = mapper.getFactory(); // may alternatively construct directly too
Tatu Saloranta477f0c82012-03-21 19:00:09 -0700181
Cowtowncoder595c8952015-05-12 17:30:47 -0700182// First: write simple JSON output
183File jsonFile = new JsonFile("test.json");
184JsonGenerator g = f.createGenerator(jsonFile);
185// write JSON: { "message" : "Hello world!" }
186g.writeStartObject();
187g.writeStringField("message", "Hello world!");
188g.writeEndObject();
189g.close();
190
191// Second: read file back
192JsonParser p = f.createParser(jsonFile);
193
194JsonToken t = p.nextToken(); // Should be JsonToken.START_OBJECT
195t = p.nextToken(); // JsonToken.FIELD_NAME
196if ((t != JsonToken.FIELD_NAME) || !"message".equals(p.getCurrentName())) {
197 // handle error
198}
199t = p.nextToken();
200if (t != JsonToken.VALUE_STRING) {
201 // similarly
202}
203String msg = p.getText();
204System.out.printf("My message to you is: %s!\n", msg);
205p.close();
206
207```
Tatu Saloranta24553d22012-03-21 21:49:26 -0700208
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700209## 10 minute tutorial: configuration
Tatu Saloranta08777102012-03-21 20:19:07 -0700210
211There are two entry-level configuration mechanisms you are likely to use:
Mark Woon69394342013-02-27 12:23:06 -0800212[Features](https://github.com/FasterXML/jackson-databind/wiki/JacksonFeatures) and [Annotations](https://github.com/FasterXML/jackson-annotations).
Tatu Saloranta08777102012-03-21 20:19:07 -0700213
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700214### Commonly used Features
Tatu Saloranta08777102012-03-21 20:19:07 -0700215
Tatu Saloranta24553d22012-03-21 21:49:26 -0700216Here are examples of configuration features that you are most likely to need to know about.
Tatu Saloranta08777102012-03-21 20:19:07 -0700217
Tatu Saloranta24553d22012-03-21 21:49:26 -0700218Let's start with higher-level data-binding configuration.
Tatu Saloranta08777102012-03-21 20:19:07 -0700219
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300220```java
221// SerializationFeature for changing how JSON is written
Tatu Saloranta08777102012-03-21 20:19:07 -0700222
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300223// to enable standard indentation ("pretty-printing"):
224mapper.enable(SerializationFeature.INDENT_OUTPUT);
225// to allow serialization of "empty" POJOs (no properties to serialize)
226// (without this setting, an exception is thrown in those cases)
227mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
228// to write java.util.Date, Calendar as number (timestamp):
229mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Tatu Saloranta08777102012-03-21 20:19:07 -0700230
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300231// DeserializationFeature for changing how JSON is read as POJOs:
Tatu Saloranta08777102012-03-21 20:19:07 -0700232
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300233// to prevent exception when encountering unknown property:
234mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
235// to allow coercion of JSON empty String ("") to null Object value:
236mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
237```
Tatu Saloranta08777102012-03-21 20:19:07 -0700238
239In addition, you may need to change some of low-level JSON parsing, generation details:
240
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300241```java
242// JsonParser.Feature for configuring parsing settings:
Tatu Saloranta08777102012-03-21 20:19:07 -0700243
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300244// to allow C/C++ style comments in JSON (non-standard, disabled by default)
Cowtowncoder690bbab2015-04-03 09:47:07 -0700245// (note: with Jackson 2.5, there is also `mapper.enable(feature)` / `mapper.disable(feature)`)
246mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300247// to allow (non-standard) unquoted field names in JSON:
Cowtowncoder690bbab2015-04-03 09:47:07 -0700248mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300249// to allow use of apostrophes (single quotes), non standard
Cowtowncoder690bbab2015-04-03 09:47:07 -0700250mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
Tatu Saloranta08777102012-03-21 20:19:07 -0700251
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300252// JsonGenerator.Feature for configuring low-level JSON generation:
253
254// to force escaping of non-ASCII characters:
Cowtowncoder690bbab2015-04-03 09:47:07 -0700255mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300256```
Tatu Saloranta08777102012-03-21 20:19:07 -0700257
Mark Woon69394342013-02-27 12:23:06 -0800258Full set of features are explained on [Jackson Features](https://github.com/FasterXML/jackson-databind/wiki/JacksonFeatures) page.
Tatu Salorantab64abf32012-03-21 20:31:18 -0700259
Tatu Saloranta24553d22012-03-21 21:49:26 -0700260### Annotations: changing property names
Tatu Saloranta08777102012-03-21 20:19:07 -0700261
Tatu Salorantab64abf32012-03-21 20:31:18 -0700262The simplest annotation-based approach is to use `@JsonProperty` annotation like so:
263
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300264```java
265public class MyBean {
266 private String _name;
Tatu Salorantab64abf32012-03-21 20:31:18 -0700267
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300268 // without annotation, we'd get "theName", but we want "name":
269 @JsonProperty("name")
270 public String getTheName() { return _name; }
Tatu Salorantab64abf32012-03-21 20:31:18 -0700271
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300272 // note: it is enough to add annotation on just getter OR setter;
273 // so we can omit it here
274 public void setTheName(String n) { _name = n; }
275}
276```
Tatu Salorantab64abf32012-03-21 20:31:18 -0700277
Mark Woon69394342013-02-27 12:23:06 -0800278There are other mechanisms to use for systematic naming changes: see [Custom Naming Convention](https://github.com/FasterXML/jackson-databind/wiki/JacksonCustomNamingConvention) for details.
Tatu Salorantab64abf32012-03-21 20:31:18 -0700279
Mark Woon69394342013-02-27 12:23:06 -0800280Note, too, that you can use [Mix-in Annotations](https://github.com/FasterXML/jackson-databind/wiki/JacksonMixinAnnotations) to associate all annotations.
Tatu Saloranta08777102012-03-21 20:19:07 -0700281
Tatu Saloranta24553d22012-03-21 21:49:26 -0700282### Annotations: Ignoring properties
Tatu Saloranta08777102012-03-21 20:19:07 -0700283
Tatu Salorantab64abf32012-03-21 20:31:18 -0700284There are two main annotations that can be used to to ignore properties: `@JsonIgnore` for individual properties; and `@JsonIgnoreProperties` for per-class definition
Tatu Saloranta08777102012-03-21 20:19:07 -0700285
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300286```java
287// means that if we see "foo" or "bar" in JSON, they will be quietly skipped
288// regardless of whether POJO has such properties
289@JsonIgnoreProperties({ "foo", "bar" })
290public class MyBean
291{
292 // will not be written as JSON; nor assigned from JSON:
293 @JsonIgnore
294 public String internal;
Tatu Salorantab64abf32012-03-21 20:31:18 -0700295
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300296 // no annotation, public field is read/written normally
297 public String external;
Tatu Salorantab64abf32012-03-21 20:31:18 -0700298
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300299 @JsonIgnore
300 public void setCode(int c) { _code = c; }
Tatu Salorantab64abf32012-03-21 20:31:18 -0700301
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300302 // note: will also be ignored because setter has annotation!
303 public int getCode() { return _code; }
304}
305```
Tatu Salorantab64abf32012-03-21 20:31:18 -0700306
307As with renaming, note that annotations are "shared" between matching fields, getters and setters: if only one has `@JsonIgnore`, it affects others.
308But it is also possible to use "split" annotations, to for example:
309
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300310```java
311public class ReadButDontWriteProps {
312 private String _name;
313 @JsonProperty public void setName(String n) { _name = n; }
314 @JsonIgnore public String getName() { return _name; }
315}
316```
Tatu Salorantab64abf32012-03-21 20:31:18 -0700317
318in this case, no "name" property would be written out (since 'getter' is ignored); but if "name" property was found from JSON, it would be assigned to POJO property!
319
320For a more complete explanation of all possible ways of ignoring properties when writing out JSON, check ["Filtering properties"](http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html) article.
Tatu Saloranta08777102012-03-21 20:19:07 -0700321
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700322### Annotations: using custom constructor
323
324Unlike many other data-binding packages, Jackson does not require you to define "default constructor" (constructor that does not take arguments).
325While it will use one if nothing else is available, you can easily define that an argument-taking constructor is used:
326
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300327```java
328public class CtorBean
329{
330 public final String name;
331 public final int age;
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700332
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300333 @JsonCreator // constructor can be public, private, whatever
334 private CtorBean(@JsonProperty("name") String name,
335 @JsonProperty("age") int age)
336 {
337 this.name = name;
338 this.age = age;
339 }
340}
341```
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700342
343Constructors are especially useful in supporting use of
344[Immutable objects](http://www.cowtowncoder.com/blog/archives/2010/08/entry_409.html).
345
346Alternatively, you can also define "factory methods":
347
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300348```java
349public class FactoryBean
350{
351 // fields etc omitted for brewity
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700352
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300353 @JsonCreator
354 public static FactoryBean create(@JsonProperty("name") String name) {
355 // construct and return an instance
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700356 }
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300357}
358```
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700359
ajostergaard8e31a1b2013-08-27 04:56:20 +0100360Note that use of a "creator method" (`@JsonCreator` with `@JsonProperty` annotated arguments) does not preclude use of setters: you
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700361can mix and match properties from constructor/factory method with ones that
362are set via setters or directly using fields.
363
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700364## Tutorial: fancier stuff, conversions
365
366One useful (but not very widely known) feature of Jackson is its ability
367to do arbitrary POJO-to-POJO conversions. Conceptually you can think of conversions as sequence of 2 steps: first, writing a POJO as JSON, and second, binding that JSON into another kind of POJO. Implementation just skips actual generation of JSON, and uses more efficient intermediate representation.
368
Leo Gertsenshteynac21a5c2015-04-20 18:15:05 -0700369Conversions work between any compatible types, and invocation is as simple as:
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700370
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300371```java
372ResultType result = mapper.convertValue(sourceObject, ResultType.class);
373```
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700374
375and as long as source and result types are compatible -- that is, if to-JSON, from-JSON sequence would succeed -- things will "just work".
376But here are couple of potentially useful use cases:
377
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300378```java
Sola8be34a42016-05-12 00:53:10 +0800379// Convert from List<Integer> to int[]
Artur Dryomovfb53adc2013-02-11 22:25:36 +0300380List<Integer> sourceList = ...;
381int[] ints = mapper.convertValue(sourceList, int[].class);
382// Convert a POJO into Map!
383Map<String,Object> propertyMap = mapper.convertValue(pojoValue, Map.class);
384// ... and back
385PojoType pojo = mapper.convertValue(propertyMap, PojoType.class);
386// decode Base64! (default byte[] representation is base64-encoded String)
387String base64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz";
388byte[] binary = mapper.convertValue(base64, byte[].class);
389```
Tatu Saloranta796dbde2012-03-21 21:53:52 -0700390
391Basically, Jackson can work as a replacement for many Apache Commons components, for tasks like base64 encoding/decoding, and handling of "dyna beans" (Maps to/from POJOs).
392
Tatu Saloranta861906d2012-11-19 17:07:31 -0800393# Contribute!
394
395We would love to get your contribution, whether it's in form of bug reports, Requests for Enhancement (RFE), documentation, or code patches.
Mark Woon69394342013-02-27 12:23:06 -0800396The primary mechanism for all of above is [GitHub Issues system](https://github.com/FasterXML/jackson-databind/issues).
Tatu Saloranta861906d2012-11-19 17:07:31 -0800397
398## Basic rules for Code Contributions
399
400There is really just one main rule, which is that to accept any code contribution, we need to get a filled Contributor License Agreement (CLA) from the author. One CLA is enough for any number of contributions, but we need one. Or, rather, companies that use our code want it. It keeps their lawyers less unhappy about Open Source usage.
401
402## Limitation on Dependencies by Core Components
403
404One additional limitation exists for so-called core components (streaming api, jackson-annotations and jackson-databind): no additional dependendies are allowed beyond:
405
Cowtowncoder541a3ca2015-05-13 13:50:16 -0700406* Core components may rely on any methods included in the supported JDK
Tatu Salorantacad22bb2019-09-11 13:20:15 -0700407 * Minimum JDK version was 1.5 until (and including) version 2.3
408 * Minimum JDK version was 1.6 for Jackson 2.4 - 2.7 (inclusive) for all core components
409 * Minimum is still 1.6 for `jackson-annotations` and `jackson-core`, for all remaining Jackson 2.x versions
410 * Minimum JDK version is 1.7 for Jackson 2.7 - 2.10 of `jackson-databind` and most non-core components
Tatu Saloranta861906d2012-11-19 17:07:31 -0800411* Jackson-databind (this package) depends on the other two (annotations, streaming).
412
Cowtowncoder541a3ca2015-05-13 13:50:16 -0700413This means that anything that has to rely on additional APIs or libraries needs to be built as an extension,
414usually a Jackson module.
Tatu Saloranta861906d2012-11-19 17:07:31 -0800415
Tatu Saloranta7b9558e2019-06-17 12:12:27 -0700416
417## Branches
418
419`master` branch is for developing the next major Jackson version -- 3.0 -- but there
420are active maintenance branches in which much of development happens:
421
Tatu Salorantaf66872d2019-09-25 22:37:54 -0700422* `2.10` is the current stable minor 2.x version
423* `2.9` is for selected backported fixes
Tatu Saloranta7b9558e2019-06-17 12:12:27 -0700424
425Older branches are usually not maintained after being declared as closed
426on [Jackson Releases](https://github.com/FasterXML/jackson/wiki/Jackson-Releases) page,
427but exist just in case a rare emergency patch is needed.
Tatu Salorantaf66872d2019-09-25 22:37:54 -0700428All released versions have matching git tags (`jackson-dataformats-binary-2.9.10`).
Tatu Saloranta7b9558e2019-06-17 12:12:27 -0700429
Tatu Saloranta3818c6e2012-03-21 22:05:11 -0700430-----
Tatu Saloranta90c43522011-12-22 23:25:27 -0800431
Tatub4006fc2013-08-16 11:49:20 -0700432# Differences from Jackson 1.x
433
Cowtowncoderc00dd8a2015-07-20 11:36:33 -0700434Project contains versions 2.0 and above: source code for earlier (1.x) versions was available from [Codehaus](http://jackson.codehaus.org) SVN repository, but due to Codehaus closure is currently (July 2015) not officially available.
435We may try to create Jackson1x repository at Github in future (if you care about this, ping Jackson team via mailing lists, or file an issue for this project).
436
Tatub4006fc2013-08-16 11:49:20 -0700437Main differences compared to 1.0 "mapper" jar are:
438
439* Maven build instead of Ant
440* Java package is now `com.fasterxml.jackson.databind` (instead of `org.codehaus.jackson.map`)
441
442-----
443
Tatu Saloranta90c43522011-12-22 23:25:27 -0800444# Further reading
445
Tatu Salorantaae5695c2016-03-01 11:47:20 -0800446* [Overall Jackson Docs](../../../jackson-docs)
Cowtowncoderc00dd8a2015-07-20 11:36:33 -0700447* [Project wiki page](https://github.com/FasterXML/jackson-databind/wiki)
Tatu Saloranta76437822012-03-26 20:01:27 -0700448
449Related:
450
451* [Core annotations](https://github.com/FasterXML/jackson-annotations) package defines annotations commonly used for configuring databinding details
452* [Core parser/generator](https://github.com/FasterXML/jackson-core) package defines low-level incremental/streaming parsers, generators