-
-
Notifications
You must be signed in to change notification settings - Fork 142
Cannot deserialize a Map whose key is a case class? #251
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
Comments
Unfortunately, json does not support objects as keys. What is happening is that Foo(1) is being stringified as "Foo(1)" (the same as calling .toString on the object) before writing it as the json key. It is not possible to deserialize "Foo(1)" as an instance of Foo of 1. I can imagine that a value class (i.e. a case class that extends AnyVal) of an integer or a string would be a desirable key, but currently the module does not support this behavior. Does that make sense? |
Ah, that does make sense @nbauernfeind, thank you. In this case I guess the best approach is to have an intermediate data structure then, for (de-)serialization, which uses the unwrapped values for keys? |
It really depends on what you are trying to do, but I find myself doing something like this instead: val keyMap = Map[String, KeyType]()
val valMap = Map[String, ValType]() Or if you really are using value classes, then write your own accessors/modifiers that strip out the id from the value class. |
We are using value classes. But there's enough limitations with Jackson and Scala that I found it easier just to have transformations to/from simplified representations for Jackson. |
If I'm remembering correctly, it is not possible to tell the difference between value classes and primitives without using scala reflection. However scala 2.10's reflection is not thread safe which makes it a non-starter. I don't think we will be able to support them without carving a 2.10 branch, which won't support the new scala things, or waiting until we stop supporting 2.10. |
Not sure if this helps, but it is possible to add custom key serializers, deserializers, either for types, or for Map-valued properties. Other custom serializers, deserializers are only used for values; for deserialization there is separate Default set of handlers is quite small (most primitives, enums, Dates), and more extensive for one direction (serialization I think). |
Yeah I get it. Once 2.10 is not supported, a lot more stuff can be done in this module. As it stands, I contend that transforming to a simplified representation is a lot easier than getting the various custom |
Are Maps with keys that are simple case classes (of one primitive field) supported?
The text was updated successfully, but these errors were encountered: