-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
@JsonUnwrapped not supported for Map-valued properties #171
Comments
Hmmh. Unfortunately Maps and POJOs are internally handled rather differently. But I am all for unification if it is technically possible, so limitation is not philosophical, just practical. I suspect that serialization part should be easy enough to make work; deserialization might get trickier? |
It would be very cool if this worked 😄 |
:-) One quick question: I assume most users would expect it to work both ways. But I suspect serialization is much easier to make work. Would you find any value in serialization-only impl? (especially initial one) One more thing: as per this: http://www.cowtowncoder.com/blog/archives/2011/07/entry_458.html one can use @JsonAnyGetter/setter to do something possibly similar. One missing pieces is that currently one must have getter (can't use it on Map filed), but that should be easy enough to address. |
Yes, a serialization-only implementation would solve my immediate use case, although I can see the deserialization as being extremely useful as well. It does seem that this could potentially be done with serialization-only JsonUnwrapped usage and then JsonAnySetter to handle the deserialization case, although that feels more than a little bit janky to me. |
Yes my concern is really with unexpected one-way street: unwrapping on way out, but not "wrapping it back" as Map on way back in. |
Ok, I think supporting this would be very useful for CSV, f.ex see FasterXML/jackson-dataformat-csv#25 Although, with CSV there are other open questions due to name/index mapping. But still, solving this on databind side could help. |
+1 |
1 similar comment
+1 |
A related note for anyone who happens upon this issue: one alternative is use of |
Great! |
I find this behavior surprising at a conceptual level, since it seems to be contrary to how Jackson behaves in general. Would I be correct in assuming that this is one of the features that will likely not be in the next Jackson 2.9 release, per the Changes likely to be postponed section? |
@matthew-pwnieexpress You are correct in that no work is planned for this feature. |
@cowtowncoder Given that |
@henryptung Interesting thought... hmmh. There is also then the possible question of what if more than one such "any property" is declared. |
Unfortunately the @JsonAnyGetter does not work (easily) with Kotlin:
as the annotation needs to put on a method. !! UPDATE !! It does work (easily):
|
True; as of now (2.9), EDIT (2023-05-15): |
We face the challenge that we provide a wrapper type for some content object that could either be a custom object or a class SomeWrapper<T> {
T content;
@JsonUnwrapped
public getContent() {
return content;
}
} Is there something we could do using a custom serializer to not have to add the extra method or extra class? |
…ces. The usage of text/uri-list as media type was entirely broken and not even advertised in the reference docs anymore. It's now again supported for both to-one and to-many associations via Collections. Maps are rejected as they cannot be rendered as list of URIs correctly. Updated reference documentation accordingly. Added a custom MapModel implementation of RepresentationModel as apparently using Maps with EntityModel does not unwrap the content properly due to [0]. [0] FasterXML/jackson-databind#171
…ces. The usage of text/uri-list as media type was entirely broken and not even advertised in the reference docs anymore. It's now again supported for both to-one and to-many associations via Collections. Maps are rejected as they cannot be rendered as list of URIs correctly. Updated reference documentation accordingly. Added a custom MapModel implementation of RepresentationModel as apparently using Maps with EntityModel does not unwrap the content properly due to [0]. [0] FasterXML/jackson-databind#171
@odrotbohm I can't think of anything: |
I have a POC with the following steps:
That way, |
Incidentally, the Stack Overflow question for this issue has 46 upvotes, and its top answer has 101 upvotes, so this is definitely an issue users have been encountering. |
Ok, marking this as "most-wanted" as there are a few thumbs-ups here too. About the only question I have is whether there are some specific differences between just using |
It's also very surprising it doesn't work for ObjectNodes, e.g., the trivial case |
@Vroo Conceptually, |
@Vroo that said, I can see why specific cases of
|
I have the same problem as @odrotbohm: This creates other problems because I've subclassed it. Being able to use |
I had a go about this and could get Unwrapping aspect working. The @Override
public JsonSerializer<Map<?, ?>> unwrappingSerializer(NameTransformer transformer) {
return new UnwrappingMapSerializer(this, transformer);
} The sole responsibility of an this._keySerializer = this._keySerializer.unwrappingSerializer(transformer); This changes the result of
As you can see unwrapping worked but the name transformer did not have any effect as |
Hi, I'd like to add here my use-case: ugly
|
@fprochazka Please don't add things that are not relevant for the specific issues. For usage questions, mailing list: https://groups.google.com/g/jackson-user works wonders. For issues to report, file a new one UNLESS you have something specific to add into existing one. |
@cowtowncoder sorry, I thought it is relevant to this issue, since I didn't find the sample anywhere previously |
Hmmh. Well, looking at it, it's a more complicated case where |
Thinking about this more I think that as things are, it really SHOULD be possible to just use Because of this, I am leaning towards closing this as "will-not-fix". But will not do that quite as yet. Note, too, that it is perfectly possible to add features (like use of "prefix") to |
Hi! @fprochazka, you can try to solve this for your "pretty Data" with a static method annotated with @JsonCreator:
|
@agorbachenko thanks for the alternative, it's much nicer than my first example, but I don't like it either; I've already solved it, see: #3439 (comment), until jackson has native support for this, I'm content using my hack. |
Although there may be some aspects where |
According to the documentation,
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.The text was updated successfully, but these errors were encountered: