JSON serialization questions

33 views
Skip to first unread message

Pablo Pazos

unread,
Aug 6, 2019, 6:08:09 PM8/6/19
to jackson-user
Hi all, I'm new to Jackson, and I was assigned to a project that uses it.

Our project A uses an external project B, where the model I need to serialize is defined. Most of the articles I read about Jackson rely heavily on adding annotations for doing the JSON serialization. Some issues I'm analyzing are:

+ the model is on an external project B, I would prefer not to touch it and define the serialization in our project A (that we control)

+ the model has a lot of classes for a very complex structure, and we need a custom JSON serialization from that structure, not the default where all the attributes appear on the JSON, so adding annotations would require touching every single class on a project that we don't manage

A related issue is, with annotations, I'm not sure what happens if we need to have two slightly different JSON serializations from the same class model.


So I'm trying to understand if it's possible to define serializations for each class in an external way (that is not tightly coupled with each class, I mean, without changing all the code and adding extra Jackson dependencies to those classes). Is that possible with Jackson?


Any pointers are very welcome!

Tatu Saloranta

unread,
Aug 6, 2019, 7:16:33 PM8/6/19
to jackson-user
On Tue, Aug 6, 2019 at 3:08 PM Pablo Pazos <pabl...@gmail.com> wrote:
>
> Hi all, I'm new to Jackson, and I was assigned to a project that uses it.
>
> Our project A uses an external project B, where the model I need to serialize is defined. Most of the articles I read about Jackson rely heavily on adding annotations for doing the JSON serialization. Some issues I'm analyzing are:
>
> + the model is on an external project B, I would prefer not to touch it and define the serialization in our project A (that we control)

This is quite common, yes.

> + the model has a lot of classes for a very complex structure, and we need a custom JSON serialization from that structure, not the default where all the attributes appear on the JSON, so adding annotations would require touching every single class on a project that we don't manage

One obvious feature that just addresses the inability to modify target
classes is so-called "mix-in annotations": ability to associate
annotations from one class/interface onto another one(s). See f.ex:

* https://medium.com/@shankar.ganesh.1234/jackson-mixin-a-simple-guide-to-a-powerful-feature-d984341dc9e2
* https://dzone.com/articles/jackson-mixin-to-the-rescue


> A related issue is, with annotations, I'm not sure what happens if we need to have two slightly different JSON serializations from the same class model.
>

This can be supported, although depending on exactly what is needed
may require use of multiple differently configured ObjectMappers (for
example to use different mix-ins).

> So I'm trying to understand if it's possible to define serializations for each class in an external way (that is not tightly coupled with each class, I mean, without changing all the code and adding extra Jackson dependencies to those classes). Is that possible with Jackson?
>
>
> Any pointers are very welcome!

Ok, so beyond general-purpose approach of mix-ins, there are a few
ways for filtering out things you do not want, using things like JSON
Views, JSON Filters. Here are some articles:

* http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html
(general approaches)
* http://www.cowtowncoder.com/blog/archives/2011/09/entry_461.html
(deeper into JSON Filters)
* https://www.baeldung.com/jackson-serialize-field-custom-criteria
(Some of the same, plus more advanced `BeanSerializerModifier` that
can change inclusion, and also (re)naming)

So maybe some of these helps you get started.

Beyond this, it's good to keep in mind that Jackson can not only
expose JSON as POJOs (typed Java Objects with rigid structure) but
also as "trees" -- JsonNode -- and sometimes one works better than
other. JsonNode -based approach works well for most dynamic cases
where you need to change names, structure.

But you don't have to select just one approach: you can actually
combine them, and convert between values: ObjectMapper.convertValue()
/ .valueToTree() / .treeToValue() can combine across models. While use
of 2 different models may seem more complicated it can actually
simplify your processing in many cases: POJOs can often get most of
the things done, but you might want to do some pre- or post-cleanup in
tree model. And you can of course get to either one from JSON, as well
as produce JSON from any model Jackson supports (plus many, many other
formats).

I hope this helps,

-+ Tatu +-

Pablo Pazos

unread,
Aug 6, 2019, 7:50:32 PM8/6/19
to jackson-user
Hey Tatu, that is really great feedback thanks!

see below
The structure I'm dealing with is actually a tree, with some generic and repeating parts. What is not clear is if the JsonNode is for marshalling (what I need) or parsing, because I've seen some examples that use that for parsing a JSON into a node tree.

Tatu Saloranta

unread,
Aug 9, 2019, 10:47:29 PM8/9/19
to jackson-user
JsonNode is internal representation or data model (like DOM is for
XML) so you can both get JSON read as JsonNode and construct it by
hand (or some combination), and eventually can write it out as JSON or
event use `convertValue()` to get to POJOs (similar longer sequence of
serializing as JSON, then re-parsing into POJOs).

Or you can think of it similar to "untyped" content where you read
JSON into Maps, Lists etc.
It represents logical content with structure that matches logical
structure of JSON content.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages