(including use of fully custom exclusion filter)

When serializing content as JSON (or another format Jackson supports), Jackson by default just writes out all property values as they are.
Many users want to change this behavior so that certain property values should be “filtered out”, not written as output. For example so that only properties with non-null values should not be written — this can help reduce message and sparse data record sizes.

Jackson offers multiple ways to limit writing of output by various “filtering” mechanisms: for an overview read “Every day Jackson usage, part 3: Filtering properties” (written 10 years ago butcovers most choices). …


(how to serialize contents of List<String> in alphabetic order, using “helper” List subtype, custom JsonSerializer)

On Customizing Jackson value Serialization

Jackson-databind allows a wide range of customization options for formatting output of many types, both using configuration settings and annotations. But there are sometimes cases where more customization is necessary.

In case of non-structured (scalar) types and user-defined POJOs, it is quite easy to alternative just implement custom JsonSerializer if bigger changes to output are required. Such serializers may then be easily added using SimpleModule like so:

ObjectMapper mapper = JsonMapper.builder()
.addModule(new SimpleModule()
.addSerializer(new MyValueSerializer()
).build();

or using @JsonSerialize(using = MyValueSerializer.class) either on property, or…


“How to use jackson-dataformat-csv”

First there was just JSON

Although Jackson started simply as a JSON parser library (see “Brief history of Jackson”), over the years it has grown to support dozen other data formats using extensions called (dataformat) Modules. At high level the API used is identical to that of reading and writing JSON — jackson-databind package and its API is format-agnostic — although there are typically some configuration settings that vary. So in theory it should be very easy to start using Jackson with one of supported (*) formats.

In practice, however, some formats are handled very similar to JSON (for example “native JSON” formats like CBOR…


(which Guava versions are supported by “jackson-datatype-guava”)

Jackson support of Guava datatypes: basics

Jackson core components like databind do not use Guava for anything: not because it was not a very useful library (it is!), but to keep Jackson core components dependency-free (*).
But there is an extension module available to let Jackson users use databind to read and write many Guava values: jackson-datatype-guava extension modules.
It is registered like so:

ObjectMapper mapper = JsonMapper.builder()
.addModule(new GuavaModule())
.build(); // Jackson 2.10+
// or with earlier Jackson versions:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());

and after doing this you can read and write many of standard Guava datatypes.

For example:

ImmutableList<Integer> list =…


(“Just Use Jackson YAML Mapper!”)

First, a disclaimer: I do not know if this is a good idea or not — and I definitely do not promote its application, if any, at this point. :)

Background: JSON from not JSON

A user presented me with a use case for Jackson JSON parsing, wherein query parameters can contain String and Number values, like so:

http://foo.bar.com?id=125&name=Bob

and wanted to just parse them into JsonNode:

JsonNode nameNode = mapper.readTree(request.getParam("name"));

This does not — and should not — work as String Bob is not valid JSON (all JSON String values MUST be quoted).

But in YAML Norway is False!

But it got me thinking of YAML which is blessed AND…


(aka Streaming Read/Write of Long JSON data streams)

JSON specification defines “JSON Values” as just “[JSON] Objects and Arrays” (*), and most JSON usage examples focus on that: for example, how to read an HTTP Request where payload is a JSON Object, how to write matching HTTP Response where payload is similarly a JSON Object.
There are countless articles covering such usage: with Jackson you would usually use something like this:

final JsonMapper mapper = new JsonMapper();
MyRequest reqOb = mapper.readValue(httpRequest.getInputStream(), MyRequest.class);
// or: JsonNode reqAsTree = mapper.readTree(...)
// ... processing, and then
MyResponse respOb = new MyResponse(.....);
mapper.writeValue(httpRequest.getOutputStream(), respOb);

or maybe the actual usage would be by…


(or, as Monty Python gang used to say, “And Now for Something Completely Different” — a minor break from my usual CS/Java writing)

Background: dissing my ethnic food roots

Over time, a few colleagues and friends in US have asked me about Finnish cuisine: what is it like, what are the good dishes etc. I have tended to answer along the lines of the (in)famous Churchill’s quote on Royal Navy (“rum and sodomy”), suggesting that Finnish food tradition consists mostly of variations meat and potatoes with a strong undercurrent of underseasoning.

While there is some truth to that quip, I have grown to realize over time…


aka POJOs are optional

As most Java developers know, Jackson is a very powerful library for converting between Plain Old Java Objects (POJOs) and various data formats, most notably JSON. By defining Java object — and especially with optional annotations — you can construct elaborate data structures to match almost any use case.
There is quite a bit of documentation showing how to do that; for example:

But while useful and often convenient, you do not really HAVE TO define Java classes write or read JSON with Jackson: for “casual” usage there are…


aka ObjectMapper.builder().[configure].build()

First the disclaimer: as I write this (end of Jan 2021), Jackson 3.0 is still very much Work-in-Progress (see earlier Jackson 3.0 vision): even the first release candidate is months away. My hope is to release the final 3.0 some time during 2021. Until then you can see the progress on master branches of Jackson repos and build locally, kick the tires, but should not use it for any real work.

With that out of the way, let’s look at the first major new feature that has been implemented for 3.0 (also listed on Jackson 3.0 …


State of Jackson 3.0 as of early 2021

The Jackson project is by now almost 14 years old: the first public version was released in the summer of 2007. A lot has happened over the years: a two dozen “minor” releases (ones with new features) from 1.0 through to 2.12 and more than 100 patch releases (ones with only bug fixes) have been released over the years.

But in addition there has been one transition known as the “major version upgrade”, in which the new version is not backwards-compatible with the older version. This occurred with the release of Jackson 2.0.0 in February of 2012.

Jackson Major versions: 1.x and 2.x

There have been…

@cowtowncoder

Open Source developer, most known for Jackson data processor (nee “JSON library”), author of many, many other OSS libraries for Java, from ClassMate to Woodstox

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store