• Stars
    star
    239
  • Rank 168,763 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated about 1 month ago

Reviews

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

Repository Details

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)

Overview

Jackson jr is a compact alternative to full Jackson Databind component. It implements a subset of functionality, for example for cases where:

  1. Size of jar matters (jackson-jr-objects is bit over 100 kB)
  2. Startup time matters (jackson-jr has very low initialization overhead)

In addition to basic datatypes (core JDK types like Lists, Maps, wrapper types), package supports reading and writing of standard Java Beans (implementation that mimics standard JDK Bean Introspection): that is, subset of POJOs that define setters/getters and/or public fields. And starting with 2.11 there is even optional support for a subset of Jackson annotations via optional jackson-jr-annotation-support extension.

Jackson-jr also adds composer implementation that can be used to construct JSON output with builder-style API, but without necessarily having to build an in-memory representation: instead, it can directly use streaming-api for direct output. It is also possible to build actual in-memory JSON String or byte[] representation, if that is preferable.

Main Jackson-jr artifact (jackson-jr-objects) itself is currently about 120 kB in size, and only depends on Jackson Streaming API package. Combined size, for "all" jar, is bit over 500 kB (of which streaming API is about 350 kB), for use cases where a single jar is preferred over more modular approach. Finally, use of jar minimizers like ProGuard can bring the jar size down even further, by renaming and removing debug information.

License

Good old Apache License.

Packaging

Project is composed of multiple Maven sub-modules, each corresponding to a jar:

  • jr-objects contains the "core" databinding implementation, and is commonly the only dependency to use
    • Depends on jackson-core for low-level reading/writing
  • jr-stree contains a simple TreeCodec implementation, with which it is possible to read JSON as TreeNodes (see more below)
  • jr-retrofit2 contains jackson-jr - based handlers for Retrofit 2 library
    • Depends on jackson-jr and Retrofit API jars, and indirectly on jackson-core
  • jr-annotation-support contains extension with support for a subset of core Jackson annotations
  • jr-all creates an "uber-jar" that contains individual modules along with all their dependencies:
    • jr-objects classes as-is, without relocating
    • jr-stree classes as-is, without relocating
    • Jackson streaming (jackson-core) contents relocated ("shaded"), for private use by jackson-jr
    • Does NOT contain jr-retrofit2 or jr-annotation-support components

If you are not sure which package to use, the answer is usually jr-objects, and build system (maven, gradle) will fetch the dependency needed. jr-all jar is only used if the single-jar deployment (self-contained, no external dependencies) is needed.

Status

Build Status Maven Central Javadoc

Usage

Reading/writing Simple Objects, Beans, List/arrays thereof

Functionality of this package is contained in Java package com.fasterxml.jackson.jr.ob.

All functionality is accessed through main JSON Object; you can either used singleton JSON.std, or construct individual objects -- either way, JSON instances are ALWAYS immutable and hence thread-safe.

We can start by reading JSON

String INPUT = "{\"a\":[1,2,{\"b\":true},3],\"c\":3}";
Object ob = JSON.std.anyFrom(INPUT);
// or
Map<String,Object> map = JSON.std.mapFrom(INPUT);
// or
MyBean bean = JSON.std.beanFrom(MyBean.class, INPUT);

from any of the usual input sources (InputStream, Reader, String or byte[] that contains JSON, URL, JsonParser); and can write same Objects as JSON:

String json = JSON.std.asString(map);
JSON.std.write(ob, new File("/tmp/stuff.json");
// and with indentation; but skip writing of null properties
byte[] bytes = JSON.std
    .with(Feature.PRETTY_PRINT_OUTPUT)
    .without(Feature.WRITE_NULL_PROPERTIES)
    .asBytes(bean);

and may also read Lists and arrays of simple and Bean types:

List<MyType> beans = JSON.std.listOfFrom(MyType.class, INPUT);

(writing of Lists and arrays works without addition effort: just pass List/array as-is)

Reading "streaming JSON" (LD-JSON)

Version 2.10 added ability to read Streaming JSON content. See "Jackson 2.10 features" (section "Jackson-jr feature expansion") for full example, but basic reading is done using new ValueIterator abstraction:

File input = new File("json-stream.ldjson");
try (ValueIterator<Bean> it = JSON.std.beanSequenceFrom(Bean.class, input)) {
  while ((Bean bean = it.nextValue()) != null) {
    // do something with 'bean'
  }
}

Writing with composers

An alternative method exists for writing: "fluent" style output can be used as follows:

String json = JSON.std
  .with(JSON.Feature.PRETTY_PRINT_OUTPUT)
  .composeString()
  .startObject()
    .put("a", 1)
    .startArrayField("arr")
      .add(1).add(2).add(3)
    .end()
    .startObjectField("ob")
      .put("x", 3)
      .put("y", 4)
      .startArrayField("args").add("none").end()
    .end()
    .put("last", true)
  .end()
  .finish();

would produce (since pretty-printing is enabled)

{
  "a" : 1,
  "arr" : [1,2,3],
  "ob" : {
    "x" : 3,
    "y" : 4,
    "args" : ["none"]
  },
  "last" : true
}

Reading/writing JSON Trees

Jackson jr allows pluggable "tree models", and also provides one implementation, jr-stree. Usage for jr-stree is by configuring JSON with codec, and then using treeFrom and write methods like so:

JSON json = JSON.std.with(new JacksonJrsTreeCodec());
TreeNode root = json.treeFrom("{\"value\" : [1, 2, 3]}");
assertTrue(root.isObject());
TreeNode array = root.get("value");
assertTrue(array.isArray());
JrsNumber n = (JrsNumber) array.get(1);
assertEquals(2, n.getValue().intValue());

String json = json.asString(root);

Note that jr-stree implementation is a small minimalistic implementation with immutable nodes. It is most useful for simple reading use cases.

It is however possible to write your own TreeCodec implementations that integrate seamlessly, and in future other tree models may be offered as part of jackson-jr, or via other libraries.

Designing your Beans

To support readability and writability of your own types, your Java objects must either:

  • 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
  • Define single-argument constructor if binding from JSON String (single-String argument) or JSON integer number (single-long or Long argument)

Note that although getters and setters need to be public (since JDK Bean Introspection does not find any other methods), constructors may have any access right, including private.

Starting with version 2.8, public fields may also be used (although their discovery may be disabled using JSON.Feature.USE_FIELDS) as an alternative: this is useful when limiting number of otherwise useless "getter" and "setter" methods.

NEW! Jackson-jr 2.11 introduce jackson-jr-annotation-support extension (see more below) which allows use of Jackson annotations like @JsonProperty, @JsonIgnore and even @JsonAutoDetect for even more granular control of inclusion, naming and renaming.

Customizing behavior with Features

There are many customizable features you can use with JSON object; see Full List of Features for details. But usage itself is via fluent methods like so:

String json = JSON.std
  .with(JSON.Feature.PRETTY_PRINT_OUTPUT)
  .without(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS)
  .asString(...);

Adding custom value readers, writers

Version 2.10 added ability to add custom ValueReaders and ValueWriters, to allow pluggable support for types beyond basic JDK types and Beans.

See section "Jackson-jr ValueReaders" of Jackson-jr 2.10 improvements for an explanation of how to add custom ValueReaders and ValueWriters

You can also check out unit test

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/CustomValueReadersTest.java

for sample usage.

There is also a blog post Enable support for java.time.* with Jackson-jr which shows how to write custom readers/writers; in this case ones for Java 8 date/time types, but the concept is general.

Using (some of) Jackson annotations

Jackson 2.11 added a new extension (a JacksonJrExtension) -- jr-annotation-support -- that adds support for a subset of Jackson annotations. See jr-annotation-support/README.md for details of this extension, but basic usage is by registering extension:

import com.fasterxml.jackson.jr.annotationsupport.JacksonAnnotationExtension;

JSON json = JSON.builder()
    .register(JacksonAnnotationExtension.std)
    .build();

and then using JSON instance as usual.

Get it!

You can use Maven dependency like:

<dependency>
  <groupId>com.fasterxml.jackson.jr</groupId>
  <artifactId>jackson-jr-objects</artifactId>
  <version>2.13.0</version>
</dependency>

and then you can also download jars via Central Maven repository.

Or you can also clone the project and build it locally with mvn clean install.

Alternatively if you want a single jar deployment, you can use jackson-jr-all jar which embeds jackson-core (repackaged using Shade plug-in, so as not to conflict with "vanilla" jackson-core):

https://repo1.maven.org/maven2/com/fasterxml/jackson/jr/jackson-jr-all/

Performance

Initial performance testing using JVM Serializers benchmark suggests that it is almost as fast as full Jackson databind -- additional overhead for tests is 5-10% for both serialization and deserialization. So performance is practically identical.

In fact, when only handling Lists and Maps style content, speed jackson-jr speed fully matches jackson-databind performance (Bean/POJO case is where full databinding's extensive optimizations help more). So performance should be adequate, and choice should be more based on functionality, convenience and deployment factors.

About the only thing missing is that there is no equivalent to Afterburner, which can further speed up databind by 20-30%, for most performance-sensitive systems.

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

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
Java
1,029
star
6

jackson-docs

Documentation for the Jackson JSON processor.
727
star
7

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
8

jackson-module-scala

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

jackson-dataformats-text

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

jackson-modules-java8

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

jackson-module-jsonSchema

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

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
13

jackson-dataformats-binary

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

aalto-xml

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

java-classmate

Library for introspecting generic type information of types, member/static methods, fields. Especially useful for POJO/Bean introspection.
Java
258
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