@JsonUnwrapped completeness: allow more flexible renaming
Prefix and Suffix not sufficient for every use case. While a very narrow use case consider following example:
class Rectangle {
@JsonUnwrapped(prefix="topLeft")
Point topLeft;
@JsonUnwrapped(prefix="bottomRight")
Point bottomRight;
}
For now this would be serialized as: { topLeft.x: 0, topLeft.y: 0, bottomRight.x: 2048. bottomRight.y: 1080 }
From documentation it would seem its not possible to coerce Jackson into serializing it as: { left: 0, top: 0, right: 2048 bottom: 1080, }
I am not entirely sure that this is even possible but I suppose something along the lines of:
class Rectangle {
@JsonUnwrapped(
names = {"x","y"},
renames= {"left", "top"}
)
Point topLeft;
@JsonUnwrapped(
names = {"x","y"},
renames= {"right", "bottom"}
)
Point bottomRight;
}
Would cover all thinkable use cases.
Underlying mechanism does not rely on existing strategies, so technically it should be possible to extend it. As an alternative to two properties, it'd also be possible to define a helper type to do something like
names = { @Rename(from="x", to="left"), @Rename(from="y", to="top")
but I guess that is a matter of preference. (basic java.util.Map can not be used as annotation value, alas).
One potential problem exists however; use of nested @JsonUnwrapped might not work as expected, or might require use of fully expanded names. With prefix/suffix it is easy enough to simply prepend/append; with mapping, matching would probably require care for such cases.
A nice way to provide this functionnality to Jackson users would be to use the same mecanism used by Hibernate with the @AttributeOverride annotation to rename properties of contained object at the point of inclusion.
That would give:
class Rectangle {
@JsonUnwrapped
@JsonAttributeOverride(name = "x", out="left")
@JsonAttributeOverride(name = "y", out="top")
Point topLeft;
@JsonUnwrapped
@JsonAttributeOverride(name = "x", out="right")
@JsonAttributeOverride(name = "y", out="bottom")
Point bottomRight;
}
It would have to work also with non-unwrapped objects so that two different container objects containing the same kind of object can decide what name to give to properties of the contained object independantly.
Hope this help.