Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonValue not considered when evaluating inclusion #2909

Closed
chrylis opened this issue Oct 30, 2020 · 7 comments
Closed

@JsonValue not considered when evaluating inclusion #2909

chrylis opened this issue Oct 30, 2020 · 7 comments
Milestone

Comments

@chrylis
Copy link

chrylis commented Oct 30, 2020

I have an object (a links holder) that is essentially a container around a java.util.Map. The map is annotated with @JsonValue, and this behaves as expected (the map is dumped as a JSON object inline).

However, I only want this included when the map is non-empty, so I annotated the getLinks() method with @JsonInclude(NON_EMPTY). However, it appears that the inclusion filter is applied before the value substitution, so it is always included even as {}.

I'm using Jackson 2.11.2 (managed via Spring Boot).

@Data
class Wrapper {
  @JsonValue
  Map<String, String> values;
}

@Data
class TopLevel {
  @JsonInclude(NON_EMPTY)
  Wrapper nested;
}
@chrylis chrylis added the to-evaluate Issue that has been received but not yet evaluated label Oct 30, 2020
@cowtowncoder
Copy link
Member

Interesting. Fixing this might be as simple (?) as adding functional isEmpty() in JsonValueSerializer....

@cowtowncoder cowtowncoder added 2.12 good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project and removed to-evaluate Issue that has been received but not yet evaluated labels Oct 30, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0-rc2 milestone Oct 31, 2020
@cowtowncoder cowtowncoder removed the good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project label Oct 31, 2020
@cowtowncoder
Copy link
Member

Actually, while the basic idea is to indeed implement isEmpty(), details get rather tricky (due to existing workarounds).
I think I can solve this but it's not a good starter problem for anyone else.

@cowtowncoder cowtowncoder changed the title @JsonValue not considered when evaluating inclusion @JsonValue not considered when evaluating inclusion Oct 31, 2020
cowtowncoder added a commit that referenced this issue Oct 31, 2020
cowtowncoder added a commit that referenced this issue Nov 1, 2020
@chrylis
Copy link
Author

chrylis commented Nov 11, 2020

Thanks! Should I plan to bump to 2.12.0 when it's out to see the new output?

@cowtowncoder
Copy link
Member

I am planning to release one more rc, 2.12.0-rc2 within 1 week.

@mbenz89
Copy link

mbenz89 commented Jun 4, 2021

Seems not to work on my side. I'm using 2.12.3 and the @JsonValue annotated getter is still serialized to "null"/default-value if I set JsonInclude to NON_NULL/'NON_DEFAULTS` in the ObjectMapper properties or view annotating the relevant value field.

My POJO looks like this:

class Container {
@JsonValue @JsonInclude(Include.NON_NULL)
 public getValue(){
 // compute value
 }
}

Any ideas @cowtowncoder ?

@cowtowncoder
Copy link
Member

@mbenz89 This is a bit different combination than issue here and is not supported. Technical reason is because getValue() is not an accessor in POJO sense (but provides alternate surrogate/delegate representation to serialize); inclusion would be determined by annotation either on returned type (class annotations) or on property that refers to this value type.
If you want, you can file a new issue to request (RFE) for this combination to work; I don't know if that would be easy to implement or not.

@mbenz89
Copy link

mbenz89 commented Jun 29, 2021

@cowtowncoder I see. Thanks a lot for the elaboration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants