Determining deserialized POJO fields at runtime

23 views
Skip to first unread message

Volkan Yazıcı

unread,
Jan 10, 2020, 4:14:55 PM1/10/20
to jackson-user
I have a bunch of POJOs deserialized from JSON strings. Is it possible to instruct Jackson at runtime to indicate which fields need to be deserialized? Consider the following POJO structure rooted at A:

class A {
   
String a1;
   
String a2;
   
String a3;
    B b
;
}

class B {
   
String b1;
   
String b2;
    C c
;
}

class C {
   
String c1;
   
String c2;
}

At compile time I can generate 2^4 * 2^3 * 2^2 view classes to achieve what I want, but that sounds like an overkill. Is there simpler way to achieve this?

Tatu Saloranta

unread,
Jan 11, 2020, 1:45:07 PM1/11/20
to jackson-user
If this was about serialization, there are a few mechanism to do filtering dynamically, but for deserialization there's just JSON Views, and then streaming-level `FilteringParserDelegate` which could probably work but is some work to use.
Specific mechanism from serialization side that would be nice is JSON Filters (`@JsonFilter`), but unfortunately databind-side abstraction is serialization-specific (uses delegation, wherein filter calls write methods on things to pass).

I think what most developers do is to serialize/convert to an intermediate structure (JsonNode, Map), then filter out things they don't want, and then serialize that pruned version as JSON.

-+ Tatu +-


 
Reply all
Reply to author
Forward
0 new messages