You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
In upgrading to 2.12.5 from Spring Boot 2.4.9's Jackson version (2.11.4), I realized an object is getting serialized differently than it was before.
I am using mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) on my mapper.
The object I want serialized has a @JsonValue annotation on a String returning method. In 2.11.4, when the return value of that method returns a blank string "", it is serialized. In 2.12.5, the "" is not serialized.
Version information
Which Jackson version(s) was this for?
Jackson 2.11.4 --> Jackson 2.12.5.
To Reproduce
My testing code:
// parent class
@Data
@AllArgsConstructor
@NoArgsConstructor
static class ParentObj {
private SimpleTestObject simpleObj;
private int num;
}
// problem class
@Data
@AllArgsConstructor
@NoArgsConstructor
static class SimpleTestObject {
private String str;
@JsonValue
public String toString() {
return str;
}
}
// Call the following for the 2 Jackson versions ...
// ObjectMapper mapper = new ObjectMapper();
// mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// SimpleTestObject obj = new SimpleTestObject() ... [and modify its string for different calls]
// ParentObj parent = new ParentObj(obj, 1);
// print mapper.writeValueAsString(parent)
// Results in 2.11.4:
// with str = null --> "{"simpleObj":null,"num":1}"
// with str = "" (empty string) --> "{"simpleObj":"","num":1}"
// with str = "hi" --> "{"simpleObj":"hi","num":1}"
// Results in 2.12.5
// with str = null --> "{"num":1}"
// with str = "" (empty string) --> "{"num":1}"
// with str = "hi" --> "{"simpleObj":"hi","num":1}"
Expected behavior
Is this expected? For what it's worth, the serialization is the SAME across versions when @JsonValue is not used.
Additional context
I looked for some sort of change in @JsonValue annotation and in Serialization in 2.12 but I didn't see anything obvious that would cause this. The main reason I upgraded was to try the Blackbird module but found this as a side effect and wanted to find out if it was intended or expected.
The text was updated successfully, but these errors were encountered:
Okay so just to confirm the intention, it is expected that @JsonValue respects serialization inclusion rules in 2.12+? And the serialization inclusion rules are applied after the @JsonValue is returned?
Correct. Supporting various things for @JsonValue annotated values is tricky but the intent here is to make it appear similar to how regular properties' inclusion would work (as if value was included as property and not as replacement for containing type).
Describe the bug
In upgrading to 2.12.5 from Spring Boot 2.4.9's Jackson version (2.11.4), I realized an object is getting serialized differently than it was before.
I am using
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
on my mapper.The object I want serialized has a
@JsonValue
annotation on a String returning method. In 2.11.4, when the return value of that method returns a blank string""
, it is serialized. In 2.12.5, the""
is not serialized.Version information
Which Jackson version(s) was this for?
Jackson 2.11.4 --> Jackson 2.12.5.
To Reproduce
My testing code:
Expected behavior
Is this expected? For what it's worth, the serialization is the SAME across versions when
@JsonValue
is not used.Additional context
I looked for some sort of change in
@JsonValue
annotation and in Serialization in 2.12 but I didn't see anything obvious that would cause this. The main reason I upgraded was to try the Blackbird module but found this as a side effect and wanted to find out if it was intended or expected.The text was updated successfully, but these errors were encountered: