-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Failure to deserialise scala Map when default typing is activated #643
Comments
@alexwei jackson-module-scala is an ugly set of hacks around the Java-centric library 'jackson-databind' With the endless variety of configuration that jackson allows but with Java-centric implementations in jackson-databind, jackson-module-scala simply can't keep up. Fixing this could take days of fiddly debugging and require changes in both jackson-databind and jackson-module-scala. My main aim is to get jackson-module-scala to handle the vanilla configuration of ObjectMapper. Once people start tweaking advanced Java-centric configs of jackson-databind - I lose interest. The cost-benefit is heavily skewed against spending time on this. |
The issue seems to be a serialization issue: vanilla ObjectMapper writes:
|
I, personally, would never use a Map when dealing when serializing/deserializing data. I would try to use collections that contain strongly typed data - Putting class names in JSON output doesn't feel great. If I had to get something working with JacksonTestData, I would change the Or just use a vanilla mapper instance (ie without the DefaultTyping set). |
Noting that this used to work in Scala 2.12 through a bit of good fortune. AFAICT
A workaround would be to replace the |
The big problem really is that Jackson's databind has pretty decent support for Now doing 2-phase processing using Java Map/Collection intermediates works ok for most other aspects but NOT for polymorphic typing (esp. via Default Typing) because it by definition is very tightly coupled to implementation types -- and in this case information for Wrapper is used. I guess I am just repeating much of what @retronym just said. :) |
I spent some time trying to hack a solution but I just kept hitting issues. I have many higher priority things to work on so I'm not planning to spend much time on this.
|
Thanks for the notes. I'm doing maintenance work across a large codebase and luckily there only seems to be one small spot using Default Typing within fairly extensive usage of jackson-scala. It looks like this was done to serialise a Scala sealed hierarchy. I'm going to use an alternative mechanism for that instead. I believe @ JsonTypeInfo + @ JsonSubtypes should work as per https://stackoverflow.com/questions/18027233/jackson-scala-json-deserialization-to-case-classes . |
Thanks @retronym - JsonTypeInfo/JsonSubtypes annotations are the best way to do this. DefaultTyping should be ok if you avoid Scala Maps. |
@retronym I'm not sure if it's a great idea but I have reflection based libs that extend jackson-module-scala and that auto-discover subtypes for sealed traits. You still need the JsonTypeInfo annotation (but don't need to list the sub types). I was hoping to create a jackson-databind enhancement that would allow you to avoid the JsonTypeInfo annotation too but I haven't gotten around to it. https://github.com/FasterXML/jackson-module-scala#related-projects (separate projects for Scala 2 and Scala 3 - because the code differs quite a bit depending on which Scala version you are using). |
…g is enabled. This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
…g is enabled. This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
…g is enabled. This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in FasterXML#643 / FasterXML#470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes FasterXML#643
@pjfanning I finally got back to trying to work around this bug. It proved awkward so I took another look at finding a workaround in the library:. Here's my PR: #658 |
This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in #643 / #470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes #643
This Map serializer is implemented in terms of the Java version and is somewhat fragile, as discussed in #643 / #470. This commit ensures that the wrapper class is a member class in both Scala and Java. That results in this JSON as the serializer code chooses not to put the inner class name in the type annotation. ``` {"m":["scala.collection.immutable.Map",{"one":"one","two":"two"}]} ``` Fixes #643
Scala version: 2.13.10
Jason version: 2.15.2
Jackson-module-scala version: 2.13-2.15.2
Test code:
When running the test:
Probably related to #470
The text was updated successfully, but these errors were encountered: