playframework / play-json

The Play JSON library
Apache License 2.0
361 stars 134 forks source link

Case Class AnyVal Not Working with Json.reads/Json.writes #1011

Open cheeseng opened 5 months ago

cheeseng commented 5 months ago

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 Play Discussion Forum or Stack Overflow.

If you want to discuss a feature request, the same tools are available.

Play JSON Version (2.5.x / etc)

3.0.2

API (Scala / Java / Neither / Both)

Scala

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

22.04.1-Ubuntu

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

openjdk version "17.0.9" 2023-10-17 OpenJDK Runtime Environment GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22) OpenJDK 64-Bit Server VM GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22, mixed mode, sharing)

Library Dependencies

None.

Expected Behavior

Please describe the expected behavior of the issue, starting from the first action.

The following should compile (works under Scala 2.13):

case class UserId(id:Long) extends AnyVal

object UserId {
  implicit val reads: Reads[UserId] = Json.reads[UserId]
  implicit val writes: Writes[UserId] = Json.writes[UserId]
}

Actual Behavior

It fails to compile under Scala 3.3.3:

Instance not found: 'ProductOf[UserId]'

Reproducible Test Case

Example included in the ticket.

xuwei-k commented 4 months ago
cchantep commented 2 months ago

Class with AnyVal is a rather a Value class than a Case class (and in fact I would not recommended make it a case class). There are dedicated macros for Value classes.