Life is too short to do something that no one wants. This article has been included in “https://www.yourbatman.cn”, and there shall be a Spring technology stack, MyBatis, middleware, such as the JVM, small and beautiful “column” study for free. Pay attention to the public number [” BAT utopia “] one by one to break, in-depth grasp,

preface

Hello everyone, I’m Brother A. In the last article, It’s time to say goodbye to Fastjson. Brother A has said goodbye to Fastjson. From the beginning of this series, we will go into the Jackson library together. Of course, it is not a single claim that it is the best JSON library in the world.


A project needs only one JSON library

JSON libraries in Java, you’ve “at least” used/heard of these three: Jackson, Gson, and Fastjson. An independent project, according to the principle of least dependence, should have only one JSON library is sufficient. But here’s the thing: you can look at your own projects, and most of them have two JSON libraries, or three or more…

Note: in the “same project”, if there are multiple implementations of the same function, it is not a good phenomenon. This will make management appear confusing (for example, the formatting of dates is not convenient to achieve unity), if there are more than one exit, want to close it is a big problem

As a “qualified” architect/engineer, maintaining minimal dependency (conformance dependency) is the norm because of the huge benefits of simple consistency. The reason is simple: two Java programmers will communicate/collaborate more effectively than one Java + one Python.

So what, which JSON library should we choose? The obvious answer is Jackson. Because it performs very well in all aspects, it is the most popular and best JSON library in the world. There’s a barrier to using Jackson as the only JSON library in your project (just think about why you’re using Fastjson), so here it is, and good luck crossing that barrier and standardizating your use to “help you add a mainstream core strength, which is what this column is all about.”


Jackson is the best JSON library in the world

Jackson is a simple, powerful, Java-based “application library”. It makes it easy to swap “Java objects” with “Json objects” (XML documents or other formats). The Jackson community is relatively active and updates quickly. The Jackson library has the following features:

  • High performance and stability: low memory footprint, good parsing performance for large/small JSON strings, large/small objects
  • High popularity: Is the default choice for many popular frameworks
  • Easy to use: Provides high-level apis that greatly simplify everyday use cases
  • No need to manually create your own mappings: Most serialization-time mappings to Java types are built in
  • Clean JSON: JSON created is clean, compact, and small in size
  • No third party dependencies: Only JDK dependencies
  • “Spring Ecosystem Support” : Jackson is the default JSON/XML parser for the Spring family.

Version convention: The version used for this column is 2.11.0 (released 2020-04), and the GAV is as follows:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.11.1</version>
</dependency>
Copy the code

In order to maintain version consistency, subsequent Spring Boot (2.3.0.release) /Spring Framework (5.2.6.release) uses the latest version, since Jackson is built in the same version as this column.


The body of the

Jackson it belongs to the group fasterXML. In the spirit of going back to its roots, take a look at the organization: a screenshot of the Fasterxml website is shown below

FasterXML is the XML parser for Woodstox streams, the JSON parser for Jackson streams, the Aalto non-blocking XML parser, andThe growingThe utility library and extension family behind the business.

As a highly popular open source library, this website page should refresh your knowledge. It’s not that it doesn’t have much content, but the fact that it’s all posted on Github is the main way to get to know it.

This seems to be a popular trend: more and more open source apps are using Github as their Home Page

The FasterXML organization has a number of “direct projects” :

  1. com.fasterxml.jackson
  2. com.fasterxml.uuid
  3. com.fasterxml.woodstox
  4. .

Apparently, “this column will focus only on Project Jackson,” the most famous and important part of the organization.


website

The first step to learning about a new technology should be to look at its website. As explained above, FasterXML hosts the first page of each project on Github, and Jackson is no exception. Jackson has a description of himself on his website:

Jackson’s “Java(or JVM platform)” standard JSON library, or Java’s “Best JSON Parser,” or simply “Java’s JSON.”

From this name on the domain-style exposed, NB is not good enough to see its status and popularity in JSON parsing, of course, the main confidence


More importantly, Jackson is a set of “data processing (not just JSON)” tools for the JVM platform, including “first class” JSON parsers/JSON generators, data binding libraries (POJOs to and from JSON); It also provides related modules to support Avro, BSON, CBOR, CSV, Smile, Properties, Protobuf, XML or YAML data formats, and even supports the setting of big data format modules.


Branches: 1.x and 2.x

Jackson has two main branches:

  • 1. X branch, 1.9.13. In maintenance mode, only bug fixes released (last Jul, 2013)
  • 2. X is a version under development (2.0.0 released in Mar, 2012)

Note: These two major releases use “different Java package names” and Maven GAV, so they are not compatible with each other, “but can coexist peacefully.” A project can rely on both versions without conflict. This is by design, and this strategy was chosen to migrate 2.x from 1.x more smoothly

Note: Now that it’s 2020, 1.x is arguably obsolete (except for very old projects), so this column won’t be covering the 1.x version.

Active 2.x branch description

Currently, 2.x branch has the following active branches:

  • 2.12: The next minor release, currently under development
  • 2.11: Current Stable version. A branch that is actively maintained will actively and continuously release patches
  • 2.10: The last stable branch will release patches without stopping maintenance
  • 2.9: Inactive branches, only micropatches will be released, still active but less active
  • Other 2.x branches: Only major security patches will be released
  • Master: The main branch. The next major branch, 3.0, is under rapid development

Note: The difference between small branches like 2.11.0 and 2.11.x can be ignored


Module is introduced

Jackson is an open source, open community. Most of the projects/modules listed below are “led” by the Jackson development team, but there are also some members from the Jackson community


Three core modules

Core Modules are the basis on which extension modules are built. Jackson currently has 3 core modules:

GroupId core modules are: < groupId > com. Fasterxml. Jackson. The core < / groupId >, artifactId see each module as shown in the below

  • Streaming Processing module (jackson-core) : Defines apis for the underlying processing flow: JsonPaser, JsonGenerator, etc., and containsSpecific to jsonThe implementation of the.
  • Annotations Standard Module (jackson-annotations) : includes standard Jackson annotations
  • Databind data binding module (jackson-databind) : Data binding (and object serialization) support on streaming package;It depends on the two modules aboveIs also the module where Jackson’s high-level apis such as ObjectMapper reside

For application-level development, we will only use the Databind data binding module, so it is the focus of this series. Here are the key “third party modules”.


Data type module

These extensions is Jackson plug-in modules (through ObjectMapper registerModule registered (), hereinafter the same), and by adding the serializer and deserializer to support Java libraries, which are used in a variety of data types, So that the Jackson Databind package (ObjectMapper/ObjectReader/ObjectWriter) can read, write, and convert these types smoothly.

Some third-party modules are directly led and maintained by Jackson officials (mainstream modules), while others are purely community efforts. Now we will introduce the functions of each module according to these two categories:

Official direct maintenance:

Description: the official maintain unity of the data type module groupId: < groupId > com. Fasterxml. Jackson. The datatype < / groupId >, and the version number is consistent with the major version number

  • Standard collection data type module:
    • Guava: A collection data type that supports Guava
    • Slightly HPPC:
    • PCollections: Omitted (new support for Jackson 2.7)
  • Hibernate: Support some Hibernate features, such as lazy loading, proxy proxy, etc
  • Joda: Supports the Joda date/time data type
  • JDK7: support for JDK7.
  • Java8: It is divided into three sub-modules to support Java8
    • jackson-module-parameter-names: This module has access to the names of constructor and method parameters, allowing omission@JsonProperty(Of course, you must add the compile parameter:-parameters)
    • jackson-datatype-jsr310: Supports the JSR310 time API added to Java8
    • jackson-datatype-jdk8: in addition to Java8 time API other API support, such asOptional
  • JSR – 353 / org. Json: slightly

Unofficial direct maintenance:

Note: The groupId of these modules that are maintained unofficially is variable, each module may be different, and their version number does not follow the official major version number

  • Jackson-datatype -bolts: Support for Yandex Bolts collection types
  • Jackson-datatype -commons-lang3: Supports some types in Apache Commons Lang V3
  • Jackson – datatype – money: supportjavax.money
  • Jackson-datatype -json-lib: for remotejson-libSupport for this library
  • .

Data format module

Data Format Modules Provide support for “other than JSON” Data formats. Most of them simply implement the Streaming API abstraction so that the data binding components can be used as-is.

Official direct maintenance:

Note: the data format of the module the groupId is < the groupId > com. Fasterxml. Jackson. Dataformat < / groupId >, and follow the major version number

  • Avro CBOR/Ion/Protobuf/primer (binary JSON) : these all belong to the binary data format, their artifactId as follows:<artifactId>jackson-dataformat-[FORMAT]</artifactId>
  • CSV/Properties/ “XML/YAML” : these formats are familiar, the same support for these commonly used text formats

“Unofficial direct maintenance:” because the module of unofficial direct maintenance is too partial, so it is omitted


Other JVM platform languages

Jackson is a “JVM platform” parser, according to the official website, so the language level is not limited to Java itself, but also covers the other two major JVM languages: Kotlin and Scala

Description: the groupId are: < groupId > com. Fasterxml. Jackson. The module < / groupId >, the version number follow the major version number

  • Jackson-module-kotlin: Processes the kotlin source type
  • Jackson-module-scala_ [Scala version number] : Handles Scala source types

Schema support

Jackson annotations define the expected properties and expected processing for poJOs, and allow the generation of “external schemas” in addition to Jackson’s own use for reading/writing JSON and other formats. Some of the functionality is covered in the data format extensions described above, but there are also many separate schema tools, such as:

  • Ant Task for JSON Schema Generation: When using Apache Ant, use the Jackson library and extension module to generate JSON from Java classes
  • Jackson-json-schema-maven-plugin: Maven plugin used to generate JSON
  • .

Note: this part will not be mentioned later in order not to confuse the main content because there are too few practical application scenarios


Jackson Jr (for mobile)

While Jackson Databind (such as ObjectMapper) is a good choice for general purpose data binding, its “footprint (Jar package size)” and “startup overhead” can be problematic in some areas: for mobile, especially for light use (read or write). In this case, the full Jackson API is unacceptable.

For all of these reasons, Jackson officials decided to create a “simpler, smaller” library: Jackson Jr. It is still built on top of the Streaming API, but does not rely on Databind and annotation. As a result, its size (JAR and runtime memory usage) is much smaller, and its API is very compact, making it suitable for mobile applications such as apps.

<dependency>
    <groupId>com.fasterxml.jackson.jr</groupId>
    <artifactId>jackson-jr-objects</artifactId>
</dependency>
Copy the code

It only relies on the Jackson-core module, so the volume control is very good. Jackson’s three core modules alone have a combined size of around 1700KB (320 + 70 + 1370). The Jackson Jr is limited to 95KB (less than 500KB even with the core 320).

For the development of Java background we are not sensitive to memory, easy to use, powerful is the absolute truth. So Jackson-Jr is just a brief introduction here and won’t be covered later in this column.


Bug report

Jackson is stable and safe, but there are no saints. FasterXML Jackson-Databind Remote Code Execution (CVE-2019-12384


Java JSON library comparison

There are a number of JSON libraries out there, and to put together some Java people’s opinions on which libraries to use, here are some links to existing independent comparisons for your reference:

  • Top 7 Open-Source JSON-binding providers
  • Be a Lazy but a Productive Android Developer, Part 3: JSON Parsing Library
  • “Can anyone recommend a good Java JSON library” (Linked-In group)
  • “Which JSON library to use on Android?”

Note: several reference links posted here are reference articles given by the official website, are more authoritative foreign literature.

Of course, if you’re Chinese, you’re probably more interested in Jackson vs. Fastjson, so don’t worry (though we’ve already done that), it’s a main course later in this column, which will be covered in more detail.


conclusion

This article combines the official website to understand the overall picture of Jackson and grasp the functional modules provided by Jackson from a comprehensive perspective, which provides an index for the subsequent explanation of this column.

Jackson’s “fast upgrade and module support” is enough to see how active his community is. And to cater to the market, it has offered commercially supported services since version 2.10: working with Tidelift to provide commercial support and maintenance for users to build open source dependencies for applications. Save time, reduce risk, and improve code health (commercial support is available for a fee).

I believe that you have a general understanding of Jackson from this article, and it is no surprise that you should be interested in learning it. When you in-depth study will find that its “elegant design, strong scalability”, not the general domestic class library can match. If Fastjson is a good JSON library, Jackson is an even better JSON ecosystem.


This column is paid in CSDN and is open to the public for free. Welcome to pay attention to brother A’s public account [BAT Utopia].


Pay attention to A brother

Author A brother (YourBatman)
Personal site www.yourbatman.cn
E-mail [email protected]
WeChat fsx641385712
Active platform
The public, BAT’s Utopia (ID: BAT-utopia)
Knowledge of the planet BAT’s Utopia
Daily Article recommendations Daily Article recommendations
BAT’s Utopia