On Tue, Aug 6, 2019 at 4:50 PM Pablo Pazos <[email protected]> wrote: > > 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]> 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.
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 +- -- 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/CAL4a10gfUn0DXQwjqCRdyuEGB6bEGkUyy%3DoFR%2BLAyAXoKB0-tg%40mail.gmail.com.
