playframework / playframework/play-json
`LowPriorityKeyReads.readableKeyReads` should not exist
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 375
- Forks
- 140
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 28
Description
Play JSON Version (2.5.x / etc)
3.0.1
API (Scala / Java / Neither / Both)
Scala
Problem
Map serialization/deserialization is broken if the key doesn't have a KeyReads instance.
The problem stems from the LowPriorityKeyReads.readableKeyReads which implies that if you have a Reads[A] then you have KeyReads[A], which does not seem like a logical conclusion. After all, the Reads can expect JsArray, JsObject or any other type, none of which are JsString.
import play.api.libs.json.*
val map = Map((1, 2) -> "foo")
val asJson = Json.prettyPrint(Json.toJson(map))
println(asJson)
// Gets serialized to: [ [ [ 1, 2 ], "foo" ] ]
val fromJson = Json.parse(asJson).validate[Map[(Int, Int), String]]
println(fromJson)
// Fails to deserialize, as a failing `KeyReads[(Int, Int)]` will be created from `LowPriorityKeyReads.readableKeyReads`
Suggested solution
Remove LowPriorityKeyReads.readableKeyReads. This breaks backwards compatibility, but I argue that this function is useless (as in it almost never produces a useful Reads instance anyway).
Reproducible Test Case
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating LowPriorityKeyReads.readableKeyReads in the Play JSON sources and reproduce the Map[(Int, Int), String] case from the linked Scastie example. Check the existing KeyReads and map serialization tests, then verify that the failing key no longer receives an inappropriate fallback and that the reported map case serializes and deserializes successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100