-
-
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
Serializing Maps with Properties Bug #1540
Comments
Thank you for reporting this. Serialization is definitely problematic. I don't recall 100% if deserialization side is (yet) supported (ideally it should be, of course), but I can check that at same time. |
Thanks! If it matters, deserialization is the painful part because of the custom deserializer. I can get the desired serialization pretty easily with a |
Right. Ironically enough, actual deserialization "as POJO" would be rather trivially easy... the problem is more in figuring out when to use Having said that, I think that per-class annotation is easier one to handle, so challenges should not be as big for this particular issue. |
Looking at code, I do not think I will go ahead and see if I can figure out what's with serialization; but I think a separate issue is needed for support deserialization side. Code to do that should probably be quite easy, added in Note, too, that there's another related issue for per-property annotation, #1419. |
Ok: so, as per #476 support for "Maps as POJOs" is only added for 2.9, where it does actually work. But before #1554 deserialization did not; that has been added. |
In my application, I am attempting to serialize and deserialize a Map implementation with additional properties. According to @cowtowncoder adding the
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
annotation will make Jackson treat the Map implementation as a plain old object. The result being JSON that includes additional properties as well as successfully deserializing those properties. This is stated in the blog post On Jackson: Serializing Lists, Maps with properties. The example given there deals with a List implementation and the same functionality is promised for Map implementations. I have used this technique with success for Lists, but it is not working for Maps.Here is a reproduction of the issue. First, the map implementation:
And here are two unit tests I expect to pass, but currently fail:
The first test checks the serialized map against the expected JSON output. We recieve {"12":45,"6":88} when I expect {"map" : {"6" : 88, "12" : 45}, "property" : 55}. That is, the property is not included in the serialization.
The second test checks that a MapImplementation can be reproduced from the JSON string {"map" : {"6" : 88, "12" : 45}, "property" : 55}. This operation fails saying an Integer cannot be created from "map", implying that the top level object is expected to be a Map<Integer, Integer>.
I am using Jackson 2.8.6. To deal with this issue, I currently have a workaround in place using a custom serializer and deserializer. Thanks for reading.
The text was updated successfully, but these errors were encountered: