You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to serialize and deserialize an abstract class that has a type parameter, but the deserialization isn't working. Instead of deserializing the JSON into an object of the type parameter's class, Jackson deserializes it into a Map, so I'm unable to read the object's properties.
The first part when we serialize and deserialize the User object works just fine. The code breaks on (7) when it tries to access success.result.name because success.result is somehow a Map instead of a User.
Here is the output:
(1) serializedUser: {"name":"John Smith","age":39}
(2) deserializedUser: User(JohnSmith,Some(39))
(3) deserializedUser.name:JohnSmith
(4) serializedSuccess: {"@type":"GithubExample$ResultWrapperSuccess","result":{"name":"John Smith","age":39}}
(5) success: ResultWrapperSuccess(Map(name ->JohnSmith, age ->39))
(6) success.result:Map(name ->JohnSmith, age ->39)
Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.Map$Map2 cannot be cast to GithubExample$User
at GithubExample$.main(GithubExample.scala:55)
at GithubExample.main(GithubExample.scala)
As evidenced by the logs, the serialization seems to be working just fine. Is there something I need to change to get the deserialization working?
The text was updated successfully, but these errors were encountered:
Combination of generic type and polymorphic type handling is not supported by jackson-databind (both are supported separately, just not in this combination), so you would need to refactor things to achieve polymorphism separately for wrapper (to get success/fail subtypes), and then for enclosed value type (using @JsonTypeInfo on result / failure property).
I'm trying to serialize and deserialize an abstract class that has a type parameter, but the deserialization isn't working. Instead of deserializing the JSON into an object of the type parameter's class, Jackson deserializes it into a
Map
, so I'm unable to read the object's properties.Here is the code:
The first part when we serialize and deserialize the
User
object works just fine. The code breaks on (7) when it tries to accesssuccess.result.name
becausesuccess.result
is somehow aMap
instead of aUser
.Here is the output:
As evidenced by the logs, the serialization seems to be working just fine. Is there something I need to change to get the deserialization working?
The text was updated successfully, but these errors were encountered: