Json reads fails for Map[K, V], where K is not a String.
WesselVS opened this issue · 3 comments
Are you looking for help?
This is an issue tracker, used to manage and track the development of Play JSON. It is not a support system and so it is not a place to ask questions or get help. If you're not sure if you have found a bug, the best place to start is with either the users mailing list or Stack Overflow.
If you want to discuss a feature request, the same tools are available.
Play JSON Version (2.5.x / etc)
2.9.2
API (Scala / Java / Neither / Both)
Scala
Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)
MacOS
JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)
Paste the output from java -version
at the command line.
java version "11.0.9" 2020-10-20 LT
Expected Behavior
Please describe the expected behavior of the issue, starting from the first action.
- Given some json-convertible object o with type T,
Json.fromJson[T](Json.toJson(o))
should yield the same o. - For
Map[(Int, Int), Int]
, this should be the case as well.
Actual Behavior
- Above property fails when using a
Map[K, V]
, whenK
is not aString
. - This yield the error
JsError(List((,List(JsonValidationError(List(error.expected.jsobject),List())))))
Reproducible Test Case
println(Json.fromJson[Map[(Int, Int), Int]](Json.toJson(Map((1, 2) -> 1))))
Is this library still maintained?
Is this library still maintained?
Yes, it is.
To answer the issue more directly - the core library does not explicitly provide Reads
or Writes
for many types of K
for Map[K,V]
. In fact, the only types for K
that this library provides for automatic JSON formatting are String
and Char
.
I don't know the historical reasons, but it feels like an intentional choice. And, when you consider what JSON keys look like, it makes sense.
In addition, if you want to Read
or Write
this type to JSON, play-json
provides automatic formatters for this very purpose. You should be able to use the macros to create the formatting rules that you want, and achieve this behavior.
Finally, I would like to suggest that a Tuple
as a key might make more sense in the resulting JSON if you make it a case class. It would certainly result in richer data in the serialized form.
For these reasons, it is my opinion that this is not an issue that should warrant a fix.