2 | 2 |
Jackson jr is a compact alternative to full [Jackson Databind](../../../jackson-databind) component.
|
3 | 3 |
It implements a subset of functionality, for example for cases where:
|
4 | 4 |
|
5 | |
1. Size of jar matters (jackson-jr size is about 100 kB)
|
6 | |
2. Startup time matters (jackson-jr has very low initialization overhead)
|
|
5 |
1. Size of jar matters (`jackson-jr-objects` is bit over 100 kB)
|
|
6 |
2. Startup time matters (`jackson-jr` has very low initialization overhead)
|
7 | 7 |
|
8 | 8 |
In addition to basic datatypes (core JDK types like `List`s, `Map`s, wrapper types),
|
9 | 9 |
package supports reading and writing of standard Java Beans (implementation that mimics standard
|
10 | 10 |
JDK Bean Introspection): that is,
|
11 | |
subset of POJOs that define setters/getters (starting with Jackson-jr `2.8`)
|
12 | |
you can alternatively use `public` fields).
|
13 | |
|
14 | |
Jackson jr also adds `composer` implementation that can be used to
|
|
11 |
subset of POJOs that define setters/getters and/or `public` fields.
|
|
12 |
And with 2.11 there is even optional support for a subset of Jackson annotations
|
|
13 |
via optional `jackson-jr-annotatin-support` extension.
|
|
14 |
|
|
15 |
Jackson-jr also adds `composer` implementation that can be used to
|
15 | 16 |
construct JSON output with builder-style API, but without necessarily having
|
16 | 17 |
to build an in-memory representation: instead, it can directly use `streaming-api`
|
17 | 18 |
for direct output. It is also possible to build actual in-memory
|
18 | 19 |
JSON `String` or `byte[]` representation, if that is preferable.
|
19 | 20 |
|
20 | |
Jackson jr artifact itself is currently about 95 kB in size, and only depends on
|
|
21 |
Main Jackson-jr artifact (`jackson-jr-objects`) itself is currently about 120 kB in size, and only depends on
|
21 | 22 |
[Jackson Streaming API](../../../jackson-core) package.
|
22 | |
Combined size, for "all" jar, is about 400 kB (of which streaming API is about 300 kB),
|
|
23 |
Combined size, for "all" jar, is bit over 500 kB (of which streaming API is about 350 kB),
|
23 | 24 |
for use cases where a single jar is preferred over more modular approach.
|
24 | 25 |
Finally, use of jar minimizers like [ProGuard](http://proguard.sourceforge.net/) can bring the jar
|
25 | 26 |
size down even further, by renaming and removing debug information.
|
|
92 | 93 |
```
|
93 | 94 |
|
94 | 95 |
(writing of `List`s and arrays works without addition effort: just pass List/array as-is)
|
|
96 |
|
|
97 |
### Reading "streaming JSON" (LD-JSON)
|
|
98 |
|
|
99 |
Version 2.10 added ability to read [Streaming JSON](https://en.wikipedia.org/wiki/JSON_streaming) content.
|
|
100 |
See ["Jackson 2.10 features"](https://medium.com/@cowtowncoder/jackson-2-10-features-cd880674d8a2) (section "Jackson-jr feature expansion") for full example, but basic
|
|
101 |
reading is done using new `ValueIterator` abstraction:
|
|
102 |
|
|
103 |
```
|
|
104 |
File input = new File("json-stream.ldjson");
|
|
105 |
try (ValueIterator<Bean> it = JSON.std.beanSequenceFrom(Bean.class, input)) {
|
|
106 |
while ((Bean bean = it.nextValue()) != null) {
|
|
107 |
// do something with 'bean'
|
|
108 |
}
|
|
109 |
}
|
|
110 |
```
|
95 | 111 |
|
96 | 112 |
### Writing with composers
|
97 | 113 |
|
|
159 | 175 |
|
160 | 176 |
To support readability and writability of your own types, your Java objects must either:
|
161 | 177 |
|
162 | |
* Implement Bean style accesors (getters for accessing data to write and/or setter for binding JSON data into objects), and define no-argument (default) constructor, OR
|
|
178 |
* Implement Bean style accessors (getters for accessing data to write and/or setter for binding JSON data into objects), and define no-argument (default) constructor, OR
|
163 | 179 |
* Define single-argument constructor if binding from JSON String (single-String argument) or JSON integer number (single-`long` or `Long` argument)
|
164 | 180 |
|
165 | 181 |
Note that although getters and setters need to be public (since JDK Bean Introspection does not find any other methods),
|
166 | 182 |
constructors may have any access right, including `private`.
|
167 | 183 |
|
168 | |
Also: starting with version 2.8, `public` fields may also be used (although their
|
|
184 |
Starting with version 2.8, `public` fields may also be used (although their
|
169 | 185 |
discovery may be disabled using `JSON.Feature.USE_FIELDS`) as an alternative:
|
170 | 186 |
this is useful when limiting number of otherwise useless "getter" and "setter"
|
171 | 187 |
methods.
|
|
188 |
|
|
189 |
NEW! Jackson-jr 2.11 introduce `jackson-jr-annotation-support` extension (see more below)
|
|
190 |
which allows use of Jackson annotations like `@JsonProperty`, `@JsonIgnore` and even `@JsonAutoDetect` for even more granular control of inclusion, naming and renaming.
|
172 | 191 |
|
173 | 192 |
### Customizing behavior with Features
|
174 | 193 |
|
|
181 | 200 |
.asString(...);
|
182 | 201 |
```
|
183 | 202 |
|
|
203 |
### Adding custom value readers, writers
|
|
204 |
|
|
205 |
Version 2.10 added ability to add custom `ValueReader`s and `ValueWriter`s, to
|
|
206 |
allow pluggable support for types beyond basic JDK types and Beans.
|
|
207 |
|
|
208 |
See section "Jackson-jr ValueReaders" of [Jackson-jr 2.10 improvements](https://cowtowncoder.medium.com/jackson-2-10-jackson-jr-improvements-9eb5bb7b35f) for an explanation of how to add custom `ValueReader`s and `ValueWriter`s
|
|
209 |
|
|
210 |
You can also check out unit test
|
|
211 |
|
|
212 |
jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/CustomValueReadersTest.java
|
|
213 |
|
|
214 |
for sample usage.
|
|
215 |
|
|
216 |
### Using (some of) Jackson annotations
|
|
217 |
|
|
218 |
Jackson 2.11 added a new extension (a `JacksonJrExtension`) -- `jr-annotation-support` -- that adds support for a subset of Jackson annotations.
|
|
219 |
See [jr-annotation-support/README.md](../../tree/master/jr-annotation-support) for details of this extension, but basic usage is by registering extension:
|
|
220 |
|
|
221 |
```
|
|
222 |
import com.fasterxml.jackson.jr.annotationsupport.JacksonAnnotationExtension;
|
|
223 |
|
|
224 |
JSON json = JSON.builder()
|
|
225 |
.register(JacksonAnnotationExtension.std)
|
|
226 |
.build();
|
|
227 |
```
|
|
228 |
|
|
229 |
and then using `JSON` instance as usual.
|
|
230 |
|
184 | 231 |
## Get it!
|
185 | 232 |
|
186 | 233 |
You can use Maven dependency like:
|
|
189 | 236 |
<dependency>
|
190 | 237 |
<groupId>com.fasterxml.jackson.jr</groupId>
|
191 | 238 |
<artifactId>jackson-jr-objects</artifactId>
|
192 | |
<version>2.9.0</version>
|
|
239 |
<version>2.12.0</version>
|
193 | 240 |
</dependency>
|
194 | 241 |
```
|
195 | 242 |
|