1*0ed15c77SAndroid Build Coastguard Worker# Overview 2*0ed15c77SAndroid Build Coastguard Worker 3*0ed15c77SAndroid Build Coastguard WorkerThis project contains the general-purpose data-binding functionality 4*0ed15c77SAndroid Build Coastguard Workerand tree-model for [Jackson Data Processor](http://wiki.fasterxml.com/JacksonHome). 5*0ed15c77SAndroid Build Coastguard WorkerIt builds on [core streaming parser/generator](../../../jackson-core) package, 6*0ed15c77SAndroid Build Coastguard Workerand uses [Jackson Annotations](../../../jackson-annotations) for configuration. 7*0ed15c77SAndroid Build Coastguard WorkerProject is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). 8*0ed15c77SAndroid Build Coastguard Worker 9*0ed15c77SAndroid Build Coastguard WorkerWhile 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. 10*0ed15c77SAndroid Build Coastguard WorkerNaming of classes uses word 'JSON' in many places even though there is no actual hard dependency to JSON format. 11*0ed15c77SAndroid Build Coastguard Worker 12*0ed15c77SAndroid Build Coastguard Worker[](https://travis-ci.org/FasterXML/jackson-databind) [](https://maven-badges.herokuapp.com/maven-central/com.fasterxml.jackson.core/jackson-databind) 13*0ed15c77SAndroid Build Coastguard Worker[](http://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind) 14*0ed15c77SAndroid Build Coastguard Worker[](https://coveralls.io/github/FasterXML/jackson-databind?branch=master) 15*0ed15c77SAndroid Build Coastguard Worker 16*0ed15c77SAndroid Build Coastguard Worker----- 17*0ed15c77SAndroid Build Coastguard Worker 18*0ed15c77SAndroid Build Coastguard Worker# Get it! 19*0ed15c77SAndroid Build Coastguard Worker 20*0ed15c77SAndroid Build Coastguard Worker## Maven 21*0ed15c77SAndroid Build Coastguard Worker 22*0ed15c77SAndroid Build Coastguard WorkerFunctionality of this package is contained in Java package `com.fasterxml.jackson.databind`, and can be used using following Maven dependency: 23*0ed15c77SAndroid Build Coastguard Worker 24*0ed15c77SAndroid Build Coastguard Worker```xml 25*0ed15c77SAndroid Build Coastguard Worker<properties> 26*0ed15c77SAndroid Build Coastguard Worker ... 27*0ed15c77SAndroid Build Coastguard Worker <!-- Use the latest version whenever possible. --> 28*0ed15c77SAndroid Build Coastguard Worker <jackson.version>2.10.0</jackson.version> 29*0ed15c77SAndroid Build Coastguard Worker ... 30*0ed15c77SAndroid Build Coastguard Worker</properties> 31*0ed15c77SAndroid Build Coastguard Worker 32*0ed15c77SAndroid Build Coastguard Worker<dependencies> 33*0ed15c77SAndroid Build Coastguard Worker ... 34*0ed15c77SAndroid Build Coastguard Worker <dependency> 35*0ed15c77SAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.core</groupId> 36*0ed15c77SAndroid Build Coastguard Worker <artifactId>jackson-databind</artifactId> 37*0ed15c77SAndroid Build Coastguard Worker <version>${jackson.version}</version> 38*0ed15c77SAndroid Build Coastguard Worker </dependency> 39*0ed15c77SAndroid Build Coastguard Worker ... 40*0ed15c77SAndroid Build Coastguard Worker</dependencies> 41*0ed15c77SAndroid Build Coastguard Worker``` 42*0ed15c77SAndroid Build Coastguard Worker 43*0ed15c77SAndroid Build Coastguard WorkerSince 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. 44*0ed15c77SAndroid Build Coastguard WorkerIf so, also add: 45*0ed15c77SAndroid Build Coastguard Worker 46*0ed15c77SAndroid Build Coastguard Worker```xml 47*0ed15c77SAndroid Build Coastguard Worker<dependencies> 48*0ed15c77SAndroid Build Coastguard Worker ... 49*0ed15c77SAndroid Build Coastguard Worker <dependency> 50*0ed15c77SAndroid Build Coastguard Worker <!-- Note: core-annotations version x.y.0 is generally compatible with 51*0ed15c77SAndroid Build Coastguard Worker (identical to) version x.y.1, x.y.2, etc. --> 52*0ed15c77SAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.core</groupId> 53*0ed15c77SAndroid Build Coastguard Worker <artifactId>jackson-annotations</artifactId> 54*0ed15c77SAndroid Build Coastguard Worker <version>${jackson.version}</version> 55*0ed15c77SAndroid Build Coastguard Worker </dependency> 56*0ed15c77SAndroid Build Coastguard Worker <dependency> 57*0ed15c77SAndroid Build Coastguard Worker <groupId>com.fasterxml.jackson.core</groupId> 58*0ed15c77SAndroid Build Coastguard Worker <artifactId>jackson-core</artifactId> 59*0ed15c77SAndroid Build Coastguard Worker <version>${jackson.version}</version> 60*0ed15c77SAndroid Build Coastguard Worker </dependency> 61*0ed15c77SAndroid Build Coastguard Worker ... 62*0ed15c77SAndroid Build Coastguard Worker<dependencies> 63*0ed15c77SAndroid Build Coastguard Worker``` 64*0ed15c77SAndroid Build Coastguard Worker 65*0ed15c77SAndroid Build Coastguard Workerbut note that this is optional, and only necessary if there are conflicts between jackson core dependencies through transitive dependencies. 66*0ed15c77SAndroid Build Coastguard Worker 67*0ed15c77SAndroid Build Coastguard Worker## Non-Maven 68*0ed15c77SAndroid Build Coastguard Worker 69*0ed15c77SAndroid Build Coastguard WorkerFor non-Maven use cases, you download jars from [Central Maven repository](http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/). 70*0ed15c77SAndroid Build Coastguard Worker 71*0ed15c77SAndroid Build Coastguard WorkerDatabind jar is also a functional OSGi bundle, with proper import/export declarations, so it can be use on OSGi container as is. 72*0ed15c77SAndroid Build Coastguard Worker 73*0ed15c77SAndroid Build Coastguard Worker----- 74*0ed15c77SAndroid Build Coastguard Worker 75*0ed15c77SAndroid Build Coastguard Worker# Use It! 76*0ed15c77SAndroid Build Coastguard Worker 77*0ed15c77SAndroid Build Coastguard WorkerMore comprehensive documentation can be found from [Jackson-docs](../../../jackson-docs) repository; as well as from [Wiki](../../wiki) of this project. 78*0ed15c77SAndroid Build Coastguard WorkerBut here are brief introductionary tutorials, in recommended order of reading. 79*0ed15c77SAndroid Build Coastguard Worker 80*0ed15c77SAndroid Build Coastguard Worker## 1 minute tutorial: POJOs to JSON and back 81*0ed15c77SAndroid Build Coastguard Worker 82*0ed15c77SAndroid Build Coastguard WorkerThe 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: 83*0ed15c77SAndroid Build Coastguard Worker 84*0ed15c77SAndroid Build Coastguard Worker```java 85*0ed15c77SAndroid Build Coastguard Worker// Note: can use getters/setters as well; here we just use public fields directly: 86*0ed15c77SAndroid Build Coastguard Workerpublic class MyValue { 87*0ed15c77SAndroid Build Coastguard Worker public String name; 88*0ed15c77SAndroid Build Coastguard Worker public int age; 89*0ed15c77SAndroid Build Coastguard Worker // NOTE: if using getters/setters, can keep fields `protected` or `private` 90*0ed15c77SAndroid Build Coastguard Worker} 91*0ed15c77SAndroid Build Coastguard Worker``` 92*0ed15c77SAndroid Build Coastguard Worker 93*0ed15c77SAndroid Build Coastguard Workerwe will need a `com.fasterxml.jackson.databind.ObjectMapper` instance, used for all data-binding, so let's construct one: 94*0ed15c77SAndroid Build Coastguard Worker 95*0ed15c77SAndroid Build Coastguard Worker```java 96*0ed15c77SAndroid Build Coastguard WorkerObjectMapper mapper = new ObjectMapper(); // create once, reuse 97*0ed15c77SAndroid Build Coastguard Worker``` 98*0ed15c77SAndroid Build Coastguard Worker 99*0ed15c77SAndroid Build Coastguard WorkerThe default instance is fine for our use -- we will learn later on how to configure mapper instance if necessary. Usage is simple: 100*0ed15c77SAndroid Build Coastguard Worker 101*0ed15c77SAndroid Build Coastguard Worker```java 102*0ed15c77SAndroid Build Coastguard WorkerMyValue value = mapper.readValue(new File("data.json"), MyValue.class); 103*0ed15c77SAndroid Build Coastguard Worker// or: 104*0ed15c77SAndroid Build Coastguard Workervalue = mapper.readValue(new URL("http://some.com/api/entry.json"), MyValue.class); 105*0ed15c77SAndroid Build Coastguard Worker// or: 106*0ed15c77SAndroid Build Coastguard Workervalue = mapper.readValue("{\"name\":\"Bob\", \"age\":13}", MyValue.class); 107*0ed15c77SAndroid Build Coastguard Worker``` 108*0ed15c77SAndroid Build Coastguard Worker 109*0ed15c77SAndroid Build Coastguard WorkerAnd if we want to write JSON, we do the reverse: 110*0ed15c77SAndroid Build Coastguard Worker 111*0ed15c77SAndroid Build Coastguard Worker```java 112*0ed15c77SAndroid Build Coastguard Workermapper.writeValue(new File("result.json"), myResultObject); 113*0ed15c77SAndroid Build Coastguard Worker// or: 114*0ed15c77SAndroid Build Coastguard Workerbyte[] jsonBytes = mapper.writeValueAsBytes(myResultObject); 115*0ed15c77SAndroid Build Coastguard Worker// or: 116*0ed15c77SAndroid Build Coastguard WorkerString jsonString = mapper.writeValueAsString(myResultObject); 117*0ed15c77SAndroid Build Coastguard Worker``` 118*0ed15c77SAndroid Build Coastguard Worker 119*0ed15c77SAndroid Build Coastguard WorkerSo far so good? 120*0ed15c77SAndroid Build Coastguard Worker 121*0ed15c77SAndroid Build Coastguard Worker## 3 minute tutorial: Generic collections, Tree Model 122*0ed15c77SAndroid Build Coastguard Worker 123*0ed15c77SAndroid Build Coastguard WorkerBeyond dealing with simple Bean-style POJOs, you can also handle JDK `List`s, `Map`s: 124*0ed15c77SAndroid Build Coastguard Worker 125*0ed15c77SAndroid Build Coastguard Worker```java 126*0ed15c77SAndroid Build Coastguard WorkerMap<String, Integer> scoreByName = mapper.readValue(jsonSource, Map.class); 127*0ed15c77SAndroid Build Coastguard WorkerList<String> names = mapper.readValue(jsonSource, List.class); 128*0ed15c77SAndroid Build Coastguard Worker 129*0ed15c77SAndroid Build Coastguard Worker// and can obviously write out as well 130*0ed15c77SAndroid Build Coastguard Workermapper.writeValue(new File("names.json"), names); 131*0ed15c77SAndroid Build Coastguard Worker``` 132*0ed15c77SAndroid Build Coastguard Worker 133*0ed15c77SAndroid Build Coastguard Workeras long as JSON structure matches, and types are simple. 134*0ed15c77SAndroid Build Coastguard WorkerIf you have POJO values, you need to indicate actual type (note: this is NOT needed for POJO properties with `List` etc types): 135*0ed15c77SAndroid Build Coastguard Worker 136*0ed15c77SAndroid Build Coastguard Worker```java 137*0ed15c77SAndroid Build Coastguard WorkerMap<String, ResultValue> results = mapper.readValue(jsonSource, 138*0ed15c77SAndroid Build Coastguard Worker new TypeReference<Map<String, ResultValue>>() { } ); 139*0ed15c77SAndroid Build Coastguard Worker// why extra work? Java Type Erasure will prevent type detection otherwise 140*0ed15c77SAndroid Build Coastguard Worker``` 141*0ed15c77SAndroid Build Coastguard Worker 142*0ed15c77SAndroid Build Coastguard Worker(note: no extra effort needed for serialization, regardless of generic types) 143*0ed15c77SAndroid Build Coastguard Worker 144*0ed15c77SAndroid Build Coastguard WorkerBut wait! There is more! 145*0ed15c77SAndroid Build Coastguard Worker 146*0ed15c77SAndroid Build Coastguard WorkerWhile dealing with `Map`s, `List`s and other "simple" Object types (Strings, Numbers, Booleans) can be simple, Object traversal can be cumbersome. 147*0ed15c77SAndroid Build Coastguard WorkerThis is where Jackson's [Tree model](https://github.com/FasterXML/jackson-databind/wiki/JacksonTreeModel) can come in handy: 148*0ed15c77SAndroid Build Coastguard Worker 149*0ed15c77SAndroid Build Coastguard Worker```java 150*0ed15c77SAndroid Build Coastguard Worker// can be read as generic JsonNode, if it can be Object or Array; or, 151*0ed15c77SAndroid Build Coastguard Worker// if known to be Object, as ObjectNode, if array, ArrayNode etc: 152*0ed15c77SAndroid Build Coastguard WorkerObjectNode root = mapper.readTree("stuff.json"); 153*0ed15c77SAndroid Build Coastguard WorkerString name = root.get("name").asText(); 154*0ed15c77SAndroid Build Coastguard Workerint age = root.get("age").asInt(); 155*0ed15c77SAndroid Build Coastguard Worker 156*0ed15c77SAndroid Build Coastguard Worker// can modify as well: this adds child Object as property 'other', set property 'type' 157*0ed15c77SAndroid Build Coastguard Workerroot.with("other").put("type", "student"); 158*0ed15c77SAndroid Build Coastguard WorkerString json = mapper.writeValueAsString(root); 159*0ed15c77SAndroid Build Coastguard Worker 160*0ed15c77SAndroid Build Coastguard Worker// with above, we end up with something like as 'json' String: 161*0ed15c77SAndroid Build Coastguard Worker// { 162*0ed15c77SAndroid Build Coastguard Worker// "name" : "Bob", "age" : 13, 163*0ed15c77SAndroid Build Coastguard Worker// "other" : { 164*0ed15c77SAndroid Build Coastguard Worker// "type" : "student" 165*0ed15c77SAndroid Build Coastguard Worker// } 166*0ed15c77SAndroid Build Coastguard Worker// } 167*0ed15c77SAndroid Build Coastguard Worker``` 168*0ed15c77SAndroid Build Coastguard Worker 169*0ed15c77SAndroid Build Coastguard WorkerTree 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*0ed15c77SAndroid Build Coastguard Worker 171*0ed15c77SAndroid Build Coastguard Worker## 5 minute tutorial: Streaming parser, generator 172*0ed15c77SAndroid Build Coastguard Worker 173*0ed15c77SAndroid Build Coastguard WorkerAs 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. 174*0ed15c77SAndroid Build Coastguard WorkerIt 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*0ed15c77SAndroid Build Coastguard Worker 176*0ed15c77SAndroid Build Coastguard WorkerFor in-depth explanation, look at [Jackson Core component](https://github.com/FasterXML/jackson-core). 177*0ed15c77SAndroid Build Coastguard WorkerBut let's look at a simple teaser to whet your appetite. 178*0ed15c77SAndroid Build Coastguard Worker 179*0ed15c77SAndroid Build Coastguard Worker```java 180*0ed15c77SAndroid Build Coastguard WorkerJsonFactory f = mapper.getFactory(); // may alternatively construct directly too 181*0ed15c77SAndroid Build Coastguard Worker 182*0ed15c77SAndroid Build Coastguard Worker// First: write simple JSON output 183*0ed15c77SAndroid Build Coastguard WorkerFile jsonFile = new JsonFile("test.json"); 184*0ed15c77SAndroid Build Coastguard WorkerJsonGenerator g = f.createGenerator(jsonFile); 185*0ed15c77SAndroid Build Coastguard Worker// write JSON: { "message" : "Hello world!" } 186*0ed15c77SAndroid Build Coastguard Workerg.writeStartObject(); 187*0ed15c77SAndroid Build Coastguard Workerg.writeStringField("message", "Hello world!"); 188*0ed15c77SAndroid Build Coastguard Workerg.writeEndObject(); 189*0ed15c77SAndroid Build Coastguard Workerg.close(); 190*0ed15c77SAndroid Build Coastguard Worker 191*0ed15c77SAndroid Build Coastguard Worker// Second: read file back 192*0ed15c77SAndroid Build Coastguard WorkerJsonParser p = f.createParser(jsonFile); 193*0ed15c77SAndroid Build Coastguard Worker 194*0ed15c77SAndroid Build Coastguard WorkerJsonToken t = p.nextToken(); // Should be JsonToken.START_OBJECT 195*0ed15c77SAndroid Build Coastguard Workert = p.nextToken(); // JsonToken.FIELD_NAME 196*0ed15c77SAndroid Build Coastguard Workerif ((t != JsonToken.FIELD_NAME) || !"message".equals(p.getCurrentName())) { 197*0ed15c77SAndroid Build Coastguard Worker // handle error 198*0ed15c77SAndroid Build Coastguard Worker} 199*0ed15c77SAndroid Build Coastguard Workert = p.nextToken(); 200*0ed15c77SAndroid Build Coastguard Workerif (t != JsonToken.VALUE_STRING) { 201*0ed15c77SAndroid Build Coastguard Worker // similarly 202*0ed15c77SAndroid Build Coastguard Worker} 203*0ed15c77SAndroid Build Coastguard WorkerString msg = p.getText(); 204*0ed15c77SAndroid Build Coastguard WorkerSystem.out.printf("My message to you is: %s!\n", msg); 205*0ed15c77SAndroid Build Coastguard Workerp.close(); 206*0ed15c77SAndroid Build Coastguard Worker 207*0ed15c77SAndroid Build Coastguard Worker``` 208*0ed15c77SAndroid Build Coastguard Worker 209*0ed15c77SAndroid Build Coastguard Worker## 10 minute tutorial: configuration 210*0ed15c77SAndroid Build Coastguard Worker 211*0ed15c77SAndroid Build Coastguard WorkerThere are two entry-level configuration mechanisms you are likely to use: 212*0ed15c77SAndroid Build Coastguard Worker[Features](https://github.com/FasterXML/jackson-databind/wiki/JacksonFeatures) and [Annotations](https://github.com/FasterXML/jackson-annotations). 213*0ed15c77SAndroid Build Coastguard Worker 214*0ed15c77SAndroid Build Coastguard Worker### Commonly used Features 215*0ed15c77SAndroid Build Coastguard Worker 216*0ed15c77SAndroid Build Coastguard WorkerHere are examples of configuration features that you are most likely to need to know about. 217*0ed15c77SAndroid Build Coastguard Worker 218*0ed15c77SAndroid Build Coastguard WorkerLet's start with higher-level data-binding configuration. 219*0ed15c77SAndroid Build Coastguard Worker 220*0ed15c77SAndroid Build Coastguard Worker```java 221*0ed15c77SAndroid Build Coastguard Worker// SerializationFeature for changing how JSON is written 222*0ed15c77SAndroid Build Coastguard Worker 223*0ed15c77SAndroid Build Coastguard Worker// to enable standard indentation ("pretty-printing"): 224*0ed15c77SAndroid Build Coastguard Workermapper.enable(SerializationFeature.INDENT_OUTPUT); 225*0ed15c77SAndroid Build Coastguard Worker// to allow serialization of "empty" POJOs (no properties to serialize) 226*0ed15c77SAndroid Build Coastguard Worker// (without this setting, an exception is thrown in those cases) 227*0ed15c77SAndroid Build Coastguard Workermapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); 228*0ed15c77SAndroid Build Coastguard Worker// to write java.util.Date, Calendar as number (timestamp): 229*0ed15c77SAndroid Build Coastguard Workermapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 230*0ed15c77SAndroid Build Coastguard Worker 231*0ed15c77SAndroid Build Coastguard Worker// DeserializationFeature for changing how JSON is read as POJOs: 232*0ed15c77SAndroid Build Coastguard Worker 233*0ed15c77SAndroid Build Coastguard Worker// to prevent exception when encountering unknown property: 234*0ed15c77SAndroid Build Coastguard Workermapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 235*0ed15c77SAndroid Build Coastguard Worker// to allow coercion of JSON empty String ("") to null Object value: 236*0ed15c77SAndroid Build Coastguard Workermapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 237*0ed15c77SAndroid Build Coastguard Worker``` 238*0ed15c77SAndroid Build Coastguard Worker 239*0ed15c77SAndroid Build Coastguard WorkerIn addition, you may need to change some of low-level JSON parsing, generation details: 240*0ed15c77SAndroid Build Coastguard Worker 241*0ed15c77SAndroid Build Coastguard Worker```java 242*0ed15c77SAndroid Build Coastguard Worker// JsonParser.Feature for configuring parsing settings: 243*0ed15c77SAndroid Build Coastguard Worker 244*0ed15c77SAndroid Build Coastguard Worker// to allow C/C++ style comments in JSON (non-standard, disabled by default) 245*0ed15c77SAndroid Build Coastguard Worker// (note: with Jackson 2.5, there is also `mapper.enable(feature)` / `mapper.disable(feature)`) 246*0ed15c77SAndroid Build Coastguard Workermapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); 247*0ed15c77SAndroid Build Coastguard Worker// to allow (non-standard) unquoted field names in JSON: 248*0ed15c77SAndroid Build Coastguard Workermapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); 249*0ed15c77SAndroid Build Coastguard Worker// to allow use of apostrophes (single quotes), non standard 250*0ed15c77SAndroid Build Coastguard Workermapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); 251*0ed15c77SAndroid Build Coastguard Worker 252*0ed15c77SAndroid Build Coastguard Worker// JsonGenerator.Feature for configuring low-level JSON generation: 253*0ed15c77SAndroid Build Coastguard Worker 254*0ed15c77SAndroid Build Coastguard Worker// to force escaping of non-ASCII characters: 255*0ed15c77SAndroid Build Coastguard Workermapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true); 256*0ed15c77SAndroid Build Coastguard Worker``` 257*0ed15c77SAndroid Build Coastguard Worker 258*0ed15c77SAndroid Build Coastguard WorkerFull set of features are explained on [Jackson Features](https://github.com/FasterXML/jackson-databind/wiki/JacksonFeatures) page. 259*0ed15c77SAndroid Build Coastguard Worker 260*0ed15c77SAndroid Build Coastguard Worker### Annotations: changing property names 261*0ed15c77SAndroid Build Coastguard Worker 262*0ed15c77SAndroid Build Coastguard WorkerThe simplest annotation-based approach is to use `@JsonProperty` annotation like so: 263*0ed15c77SAndroid Build Coastguard Worker 264*0ed15c77SAndroid Build Coastguard Worker```java 265*0ed15c77SAndroid Build Coastguard Workerpublic class MyBean { 266*0ed15c77SAndroid Build Coastguard Worker private String _name; 267*0ed15c77SAndroid Build Coastguard Worker 268*0ed15c77SAndroid Build Coastguard Worker // without annotation, we'd get "theName", but we want "name": 269*0ed15c77SAndroid Build Coastguard Worker @JsonProperty("name") 270*0ed15c77SAndroid Build Coastguard Worker public String getTheName() { return _name; } 271*0ed15c77SAndroid Build Coastguard Worker 272*0ed15c77SAndroid Build Coastguard Worker // note: it is enough to add annotation on just getter OR setter; 273*0ed15c77SAndroid Build Coastguard Worker // so we can omit it here 274*0ed15c77SAndroid Build Coastguard Worker public void setTheName(String n) { _name = n; } 275*0ed15c77SAndroid Build Coastguard Worker} 276*0ed15c77SAndroid Build Coastguard Worker``` 277*0ed15c77SAndroid Build Coastguard Worker 278*0ed15c77SAndroid Build Coastguard WorkerThere are other mechanisms to use for systematic naming changes: see [Custom Naming Convention](https://github.com/FasterXML/jackson-databind/wiki/JacksonCustomNamingConvention) for details. 279*0ed15c77SAndroid Build Coastguard Worker 280*0ed15c77SAndroid Build Coastguard WorkerNote, too, that you can use [Mix-in Annotations](https://github.com/FasterXML/jackson-databind/wiki/JacksonMixinAnnotations) to associate all annotations. 281*0ed15c77SAndroid Build Coastguard Worker 282*0ed15c77SAndroid Build Coastguard Worker### Annotations: Ignoring properties 283*0ed15c77SAndroid Build Coastguard Worker 284*0ed15c77SAndroid Build Coastguard WorkerThere are two main annotations that can be used to to ignore properties: `@JsonIgnore` for individual properties; and `@JsonIgnoreProperties` for per-class definition 285*0ed15c77SAndroid Build Coastguard Worker 286*0ed15c77SAndroid Build Coastguard Worker```java 287*0ed15c77SAndroid Build Coastguard Worker// means that if we see "foo" or "bar" in JSON, they will be quietly skipped 288*0ed15c77SAndroid Build Coastguard Worker// regardless of whether POJO has such properties 289*0ed15c77SAndroid Build Coastguard Worker@JsonIgnoreProperties({ "foo", "bar" }) 290*0ed15c77SAndroid Build Coastguard Workerpublic class MyBean 291*0ed15c77SAndroid Build Coastguard Worker{ 292*0ed15c77SAndroid Build Coastguard Worker // will not be written as JSON; nor assigned from JSON: 293*0ed15c77SAndroid Build Coastguard Worker @JsonIgnore 294*0ed15c77SAndroid Build Coastguard Worker public String internal; 295*0ed15c77SAndroid Build Coastguard Worker 296*0ed15c77SAndroid Build Coastguard Worker // no annotation, public field is read/written normally 297*0ed15c77SAndroid Build Coastguard Worker public String external; 298*0ed15c77SAndroid Build Coastguard Worker 299*0ed15c77SAndroid Build Coastguard Worker @JsonIgnore 300*0ed15c77SAndroid Build Coastguard Worker public void setCode(int c) { _code = c; } 301*0ed15c77SAndroid Build Coastguard Worker 302*0ed15c77SAndroid Build Coastguard Worker // note: will also be ignored because setter has annotation! 303*0ed15c77SAndroid Build Coastguard Worker public int getCode() { return _code; } 304*0ed15c77SAndroid Build Coastguard Worker} 305*0ed15c77SAndroid Build Coastguard Worker``` 306*0ed15c77SAndroid Build Coastguard Worker 307*0ed15c77SAndroid Build Coastguard WorkerAs with renaming, note that annotations are "shared" between matching fields, getters and setters: if only one has `@JsonIgnore`, it affects others. 308*0ed15c77SAndroid Build Coastguard WorkerBut it is also possible to use "split" annotations, to for example: 309*0ed15c77SAndroid Build Coastguard Worker 310*0ed15c77SAndroid Build Coastguard Worker```java 311*0ed15c77SAndroid Build Coastguard Workerpublic class ReadButDontWriteProps { 312*0ed15c77SAndroid Build Coastguard Worker private String _name; 313*0ed15c77SAndroid Build Coastguard Worker @JsonProperty public void setName(String n) { _name = n; } 314*0ed15c77SAndroid Build Coastguard Worker @JsonIgnore public String getName() { return _name; } 315*0ed15c77SAndroid Build Coastguard Worker} 316*0ed15c77SAndroid Build Coastguard Worker``` 317*0ed15c77SAndroid Build Coastguard Worker 318*0ed15c77SAndroid Build Coastguard Workerin 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*0ed15c77SAndroid Build Coastguard Worker 320*0ed15c77SAndroid Build Coastguard WorkerFor 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. 321*0ed15c77SAndroid Build Coastguard Worker 322*0ed15c77SAndroid Build Coastguard Worker### Annotations: using custom constructor 323*0ed15c77SAndroid Build Coastguard Worker 324*0ed15c77SAndroid Build Coastguard WorkerUnlike many other data-binding packages, Jackson does not require you to define "default constructor" (constructor that does not take arguments). 325*0ed15c77SAndroid Build Coastguard WorkerWhile it will use one if nothing else is available, you can easily define that an argument-taking constructor is used: 326*0ed15c77SAndroid Build Coastguard Worker 327*0ed15c77SAndroid Build Coastguard Worker```java 328*0ed15c77SAndroid Build Coastguard Workerpublic class CtorBean 329*0ed15c77SAndroid Build Coastguard Worker{ 330*0ed15c77SAndroid Build Coastguard Worker public final String name; 331*0ed15c77SAndroid Build Coastguard Worker public final int age; 332*0ed15c77SAndroid Build Coastguard Worker 333*0ed15c77SAndroid Build Coastguard Worker @JsonCreator // constructor can be public, private, whatever 334*0ed15c77SAndroid Build Coastguard Worker private CtorBean(@JsonProperty("name") String name, 335*0ed15c77SAndroid Build Coastguard Worker @JsonProperty("age") int age) 336*0ed15c77SAndroid Build Coastguard Worker { 337*0ed15c77SAndroid Build Coastguard Worker this.name = name; 338*0ed15c77SAndroid Build Coastguard Worker this.age = age; 339*0ed15c77SAndroid Build Coastguard Worker } 340*0ed15c77SAndroid Build Coastguard Worker} 341*0ed15c77SAndroid Build Coastguard Worker``` 342*0ed15c77SAndroid Build Coastguard Worker 343*0ed15c77SAndroid Build Coastguard WorkerConstructors are especially useful in supporting use of 344*0ed15c77SAndroid Build Coastguard Worker[Immutable objects](http://www.cowtowncoder.com/blog/archives/2010/08/entry_409.html). 345*0ed15c77SAndroid Build Coastguard Worker 346*0ed15c77SAndroid Build Coastguard WorkerAlternatively, you can also define "factory methods": 347*0ed15c77SAndroid Build Coastguard Worker 348*0ed15c77SAndroid Build Coastguard Worker```java 349*0ed15c77SAndroid Build Coastguard Workerpublic class FactoryBean 350*0ed15c77SAndroid Build Coastguard Worker{ 351*0ed15c77SAndroid Build Coastguard Worker // fields etc omitted for brewity 352*0ed15c77SAndroid Build Coastguard Worker 353*0ed15c77SAndroid Build Coastguard Worker @JsonCreator 354*0ed15c77SAndroid Build Coastguard Worker public static FactoryBean create(@JsonProperty("name") String name) { 355*0ed15c77SAndroid Build Coastguard Worker // construct and return an instance 356*0ed15c77SAndroid Build Coastguard Worker } 357*0ed15c77SAndroid Build Coastguard Worker} 358*0ed15c77SAndroid Build Coastguard Worker``` 359*0ed15c77SAndroid Build Coastguard Worker 360*0ed15c77SAndroid Build Coastguard WorkerNote that use of a "creator method" (`@JsonCreator` with `@JsonProperty` annotated arguments) does not preclude use of setters: you 361*0ed15c77SAndroid Build Coastguard Workercan mix and match properties from constructor/factory method with ones that 362*0ed15c77SAndroid Build Coastguard Workerare set via setters or directly using fields. 363*0ed15c77SAndroid Build Coastguard Worker 364*0ed15c77SAndroid Build Coastguard Worker## Tutorial: fancier stuff, conversions 365*0ed15c77SAndroid Build Coastguard Worker 366*0ed15c77SAndroid Build Coastguard WorkerOne useful (but not very widely known) feature of Jackson is its ability 367*0ed15c77SAndroid Build Coastguard Workerto 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*0ed15c77SAndroid Build Coastguard Worker 369*0ed15c77SAndroid Build Coastguard WorkerConversions work between any compatible types, and invocation is as simple as: 370*0ed15c77SAndroid Build Coastguard Worker 371*0ed15c77SAndroid Build Coastguard Worker```java 372*0ed15c77SAndroid Build Coastguard WorkerResultType result = mapper.convertValue(sourceObject, ResultType.class); 373*0ed15c77SAndroid Build Coastguard Worker``` 374*0ed15c77SAndroid Build Coastguard Worker 375*0ed15c77SAndroid Build Coastguard Workerand as long as source and result types are compatible -- that is, if to-JSON, from-JSON sequence would succeed -- things will "just work". 376*0ed15c77SAndroid Build Coastguard WorkerBut here are couple of potentially useful use cases: 377*0ed15c77SAndroid Build Coastguard Worker 378*0ed15c77SAndroid Build Coastguard Worker```java 379*0ed15c77SAndroid Build Coastguard Worker// Convert from List<Integer> to int[] 380*0ed15c77SAndroid Build Coastguard WorkerList<Integer> sourceList = ...; 381*0ed15c77SAndroid Build Coastguard Workerint[] ints = mapper.convertValue(sourceList, int[].class); 382*0ed15c77SAndroid Build Coastguard Worker// Convert a POJO into Map! 383*0ed15c77SAndroid Build Coastguard WorkerMap<String,Object> propertyMap = mapper.convertValue(pojoValue, Map.class); 384*0ed15c77SAndroid Build Coastguard Worker// ... and back 385*0ed15c77SAndroid Build Coastguard WorkerPojoType pojo = mapper.convertValue(propertyMap, PojoType.class); 386*0ed15c77SAndroid Build Coastguard Worker// decode Base64! (default byte[] representation is base64-encoded String) 387*0ed15c77SAndroid Build Coastguard WorkerString base64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"; 388*0ed15c77SAndroid Build Coastguard Workerbyte[] binary = mapper.convertValue(base64, byte[].class); 389*0ed15c77SAndroid Build Coastguard Worker``` 390*0ed15c77SAndroid Build Coastguard Worker 391*0ed15c77SAndroid Build Coastguard WorkerBasically, 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*0ed15c77SAndroid Build Coastguard Worker 393*0ed15c77SAndroid Build Coastguard Worker# Contribute! 394*0ed15c77SAndroid Build Coastguard Worker 395*0ed15c77SAndroid Build Coastguard WorkerWe would love to get your contribution, whether it's in form of bug reports, Requests for Enhancement (RFE), documentation, or code patches. 396*0ed15c77SAndroid Build Coastguard WorkerThe primary mechanism for all of above is [GitHub Issues system](https://github.com/FasterXML/jackson-databind/issues). 397*0ed15c77SAndroid Build Coastguard Worker 398*0ed15c77SAndroid Build Coastguard Worker## Basic rules for Code Contributions 399*0ed15c77SAndroid Build Coastguard Worker 400*0ed15c77SAndroid Build Coastguard WorkerThere 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*0ed15c77SAndroid Build Coastguard Worker 402*0ed15c77SAndroid Build Coastguard Worker## Limitation on Dependencies by Core Components 403*0ed15c77SAndroid Build Coastguard Worker 404*0ed15c77SAndroid Build Coastguard WorkerOne additional limitation exists for so-called core components (streaming api, jackson-annotations and jackson-databind): no additional dependendies are allowed beyond: 405*0ed15c77SAndroid Build Coastguard Worker 406*0ed15c77SAndroid Build Coastguard Worker* Core components may rely on any methods included in the supported JDK 407*0ed15c77SAndroid Build Coastguard Worker * Minimum JDK version was 1.5 until (and including) version 2.3 408*0ed15c77SAndroid Build Coastguard Worker * Minimum JDK version was 1.6 for Jackson 2.4 - 2.7 (inclusive) for all core components 409*0ed15c77SAndroid Build Coastguard Worker * Minimum is still 1.6 for `jackson-annotations` and `jackson-core`, for all remaining Jackson 2.x versions 410*0ed15c77SAndroid Build Coastguard Worker * Minimum JDK version is 1.7 for Jackson 2.7 - 2.10 of `jackson-databind` and most non-core components 411*0ed15c77SAndroid Build Coastguard Worker* Jackson-databind (this package) depends on the other two (annotations, streaming). 412*0ed15c77SAndroid Build Coastguard Worker 413*0ed15c77SAndroid Build Coastguard WorkerThis means that anything that has to rely on additional APIs or libraries needs to be built as an extension, 414*0ed15c77SAndroid Build Coastguard Workerusually a Jackson module. 415*0ed15c77SAndroid Build Coastguard Worker 416*0ed15c77SAndroid Build Coastguard Worker 417*0ed15c77SAndroid Build Coastguard Worker## Branches 418*0ed15c77SAndroid Build Coastguard Worker 419*0ed15c77SAndroid Build Coastguard Worker`master` branch is for developing the next major Jackson version -- 3.0 -- but there 420*0ed15c77SAndroid Build Coastguard Workerare active maintenance branches in which much of development happens: 421*0ed15c77SAndroid Build Coastguard Worker 422*0ed15c77SAndroid Build Coastguard Worker* `2.10` is the current stable minor 2.x version 423*0ed15c77SAndroid Build Coastguard Worker* `2.9` is for selected backported fixes 424*0ed15c77SAndroid Build Coastguard Worker 425*0ed15c77SAndroid Build Coastguard WorkerOlder branches are usually not maintained after being declared as closed 426*0ed15c77SAndroid Build Coastguard Workeron [Jackson Releases](https://github.com/FasterXML/jackson/wiki/Jackson-Releases) page, 427*0ed15c77SAndroid Build Coastguard Workerbut exist just in case a rare emergency patch is needed. 428*0ed15c77SAndroid Build Coastguard WorkerAll released versions have matching git tags (`jackson-dataformats-binary-2.9.10`). 429*0ed15c77SAndroid Build Coastguard Worker 430*0ed15c77SAndroid Build Coastguard Worker----- 431*0ed15c77SAndroid Build Coastguard Worker 432*0ed15c77SAndroid Build Coastguard Worker# Differences from Jackson 1.x 433*0ed15c77SAndroid Build Coastguard Worker 434*0ed15c77SAndroid Build Coastguard WorkerProject 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. 435*0ed15c77SAndroid Build Coastguard WorkerWe 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*0ed15c77SAndroid Build Coastguard Worker 437*0ed15c77SAndroid Build Coastguard WorkerMain differences compared to 1.0 "mapper" jar are: 438*0ed15c77SAndroid Build Coastguard Worker 439*0ed15c77SAndroid Build Coastguard Worker* Maven build instead of Ant 440*0ed15c77SAndroid Build Coastguard Worker* Java package is now `com.fasterxml.jackson.databind` (instead of `org.codehaus.jackson.map`) 441*0ed15c77SAndroid Build Coastguard Worker 442*0ed15c77SAndroid Build Coastguard Worker----- 443*0ed15c77SAndroid Build Coastguard Worker 444*0ed15c77SAndroid Build Coastguard Worker# Further reading 445*0ed15c77SAndroid Build Coastguard Worker 446*0ed15c77SAndroid Build Coastguard Worker* [Overall Jackson Docs](../../../jackson-docs) 447*0ed15c77SAndroid Build Coastguard Worker* [Project wiki page](https://github.com/FasterXML/jackson-databind/wiki) 448*0ed15c77SAndroid Build Coastguard Worker 449*0ed15c77SAndroid Build Coastguard WorkerRelated: 450*0ed15c77SAndroid Build Coastguard Worker 451*0ed15c77SAndroid Build Coastguard Worker* [Core annotations](https://github.com/FasterXML/jackson-annotations) package defines annotations commonly used for configuring databinding details 452*0ed15c77SAndroid Build Coastguard Worker* [Core parser/generator](https://github.com/FasterXML/jackson-core) package defines low-level incremental/streaming parsers, generators 453