jackson-databind: @JsonUnwrapped not supported for Map-valued properties
According to the documentation,
Annotation used to indicate that a property should be serialized “unwrapped”; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.
Unfortunately, this seems to work only with bean types, and does not work with Map<String, Object>
. Given that bean types are generally completely interchangeable with Maps, it would be very nice if this worked correctly.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Reactions: 25
- Comments: 34 (16 by maintainers)
Commits related to this issue
- Refactoring tests for #171, as they are failing, and may not be fixed immediately — committed to FasterXML/jackson-databind by cowtowncoder 11 years ago
- resolves #171 by adding append method. — committed to christophercurrie/jackson-databind by deleted user 10 years ago
- DATAREST-1458 - Fixed rendering of compact view to association resources. The usage of text/uri-list as media type was entirely broken and not even advertised in the reference docs anymore. It's now ... — committed to spring-projects/spring-data-rest by odrotbohm 5 years ago
- DATAREST-1458 - Fixed rendering of compact view to association resources. The usage of text/uri-list as media type was entirely broken and not even advertised in the reference docs anymore. It's now ... — committed to spring-projects/spring-data-rest by odrotbohm 5 years ago
Unfortunately the @JsonAnyGetter does not work (easily) with Kotlin:
as the annotation needs to put on a method.
!! UPDATE !!
It does work (easily):
I have the same problem as @odrotbohm:
@JsonUnwrapped
sits on top of a generic. To get it to work, I’ve needed to create subclass that overrides the getter so I can add@JsonAnyGetter
. The any-getter doesn’t work because it’s of typeT
, not aMap
and it throws an error during runtime otherwise.This creates other problems because I’ve subclassed it. Being able to use
@JsonUnwrapped
would be much easier, cleaner and safer.A related note for anyone who happens upon this issue: one alternative is use of
@JsonAnyGetter
, which does allow functionality for a singleMap
.Hi! @fprochazka, you can try to solve this for your “pretty Data” with a static method annotated with @JsonCreator:
@cowtowncoder sorry, I thought it is relevant to this issue, since I didn’t find the sample anywhere previously
I had a go about this and could get Unwrapping aspect working. The
JsonSerializer<T>
has aunwrappingSerializer
method that is called withinUnwrappingBeanPropertyWriter
that sort of lifts the serializer into a unwrapping serializer. When we try to serialize a Map instance aMapSerializer
is responsible for the serialization step. OverridingunwrappingSerializer
forMapSerializer
to create aUnwrappingMapSerializer
should give us what we want.The sole responsibility of an
UnwrappingMapSerializer
is making MapSerializer’skeySerializer
an unwrapping serializer.This changes the result of
TestUnwrappedMap171.testMapUnwrapSerialize
As you can see unwrapping worked but the name transformer did not have any effect as
StdKeySerializers
do not have arename
concept asBeanPropertyWriter
s do.We face the challenge that we provide a wrapper type for some content object that could either be a custom object or a
Map
:Is there something we could do using a custom serializer to not have to add the extra method or extra class?