Hey Tatu, that is really great feedback thanks! see below
On Tuesday, August 6, 2019 at 8:16:33 PM UTC-3, Tatu Saloranta wrote: > > On Tue, Aug 6, 2019 at 3:08 PM Pablo Pazos <[email protected] > <javascript:>> 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) > > 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. > 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 +- > -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/708b871c-bf8f-468c-b735-e8a5461b75cb%40googlegroups.com.
