Open ms-ati opened 8 years ago
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 KeyDeserializer
, for serialization JsonSerializer
is used, but it has to use different write method. So this is why separate ones for keys are needed.
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 KeyDeserializer
and JsonSerializers
all correct...
Are Maps with keys that are simple case classes (of one primitive field) supported?