On Thu, Jun 16, 2022 at 3:10 AM [email protected] <[email protected]> wrote:
>
> Hello,
>
> I have several classes (MyClassA, MyClassB...) sharing an interface 
> (MyInterface), and I want to customize the inclusion setting for those POJOs, 
> using my custom filter (MyValueFilter).
>
> This is what I have so far:
>
> MAPPER.setSerializationInclusion(Include.NON_EMPTY);
> MAPPER.configOverride(MyInterface.class).setIncludeAsProperty(
>                 JsonInclude.Value.construct(Include.CUSTOM, 
> Include.CUSTOM).withValueFilter(MyValueFilter.class));
>
> The issue is that during serialization, config override lookup is based only 
> on the POJO class, not the interface it is implementing, so the lookup is not 
> returning the config override.

Correct, this is the behavior. Object hierarchy is not traversed in
case of configOverrides.
For annotations the situation is different as they are "flattened"
during processing so they are aggregated over hierarchy.

One thing you could consider are "mix-in annotations", ones that you
can attach to any class or interface, similar to adding them directly.

> If I use MAPPER.configOverride(MyClassA.class) instead of 
> MAPPER.configOverride(MyInterface.class), this is working as expected, but of 
> course only for MyClassA (not MyClasssB, MyClassC, etc.).

Yes.

> Is there a method to override inclusion setting for all POJOs implementing a 
> given interface?

No, there isn't.

> Side question: what I'm trying to achieve is filtering out "empty" POJO, ie. 
> a POJO where all its fields are empty.
> MAPPER.setSerializationInclusion(Include.NON_EMPTY) is properly skipping the 
> POJO empty fields, but I still get '"pojoName": { }' in my JSON output when 
> all the fields are empty. I would like to define a method to determine if 
> this POJO is empty. For Map, .isEmpty() is called, for String, .lenght() etc. 
> I'd like to have the same mechanism to skip my POJOs, based on my own 
> function.

That sounds reasonable on its own; for now your best bet would be `
JsonInclude.Value.construct(Include.CUSTOM)` as you were trying to do.

Alternatively, use of `Include.NON_DEFAULT` might work: it would
require that POJO in question implements/overrides `equals()`,
as well as having a default (no-arguments) constructor. If so, I think
the logic is that if given POJO value equals to "default" value (one
constructed with default no-arg constructor), it will be considered
"default value" and filtered out.

As to "empty", problem is that POJO types do not, in general, have any
shared method to use unlike, `Collection`s and so on.

I hope this helps,

-+ Tatu +-

>
> Thanks!
> Clément
>
> --
> 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/b2d1e676-32e3-4cc3-8893-a9c12f6844fan%40googlegroups.com.

-- 
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/CAL4a10jmSo4_163i%2BM2rG1tTp-3Gw9A17DuhVm2dDf753FAV5w%40mail.gmail.com.

Reply via email to