-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add test case for using JsonUnwrapped on fields of the same class #5182
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
base: 2.x
Are you sure you want to change the base?
Conversation
Some thoughts...
Because that's how it's configured? Current result is what I would expect from plain configuration of |
.addSerializer(ValueAndMap.class, new ValueAndMapSerializer()) | ||
.addDeserializer(ValueAndMap.class, new ValueAndMapDeserializer())); | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad idea for tests -- should almost never be used: often masks legitimate problems (in production use useful for defensive handling).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. Removing this, however, will throw a UnrecognizedPropertyException
Unrecognized field "prop1_l10n"
The idea was to see what the configuration was actually doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, so it did hide a failure.
o.prop2.value = "bar"; | ||
o.prop2.value_l10n = Map.of("c", "d"); | ||
|
||
// currently produces JSON object with duplicate keys JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess ideally it'd fail with exception? Since definition would instruct output like this.
What puzzles me is the use of
that gets called if annotation indicates need; similarly for
but as I said, these are rarely needed. So I would just remove |
@JooHyukKim thank you for your quick responses
Yes, I had thought this too. But that does not result in what I would like to see public class MyFancyClass {
@JsonUnwrapped(prefix = "name_")
private LocalizedField name;
@JsonUnwrapped(prefix = "description_")
private LocalizedField description;
// ...
} Will create {
"name_value": "name", // name.value
"name_value_l10n": { // name.value_l10n
"de": "name",
"fr": "nom"
},
"description_value": "description", // description.value
"description_value_l10n": { // description.value_l10n
"de": "Beschreibung",
"fr": "description"
}
}
That's actually my question. If I had the full JSON node, I could select the properties I want to parse. But this is not the case. Is there a way to get the parent node in a JsonUnwrapped property for custom deserialization?
I did not expect this. Just wanted to mention it. Wouldn't it be correct to throw a |
Actually, as per my note on the issue (FasterXML/jackson#278), you might be able to change things overriding method |
One of |
Honestly name_value name_value_l10n seem reasonable. You gotta meet half way. Can you try both serialxiation deserialziation roundtrip with prefix configured? It might work @kariem |
See FasterXML/jackson#278 for context
I would like to include a bean into a class multiple times. For serialization, this bean should prefix its fields in a specific way so that I can reuse this bean in other classes too.
This is the JSON representation I want to use
The pairs
name
andname_l10n
should be from the same bean instance. The java representation should be simple and reusableI have provided some test cases to demonstrate the problems I have encountered so far.
testUnwrappingWithDifferentProperties
testUnwrappingWithCustomSerializer
testUnwrappingWithCustomDeserializer