Skip to content
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

[Feature] Support typeclass derivation (derives) from Scala 3 #1089

Open
gaeljw opened this issue Nov 7, 2024 · 0 comments
Open

[Feature] Support typeclass derivation (derives) from Scala 3 #1089

gaeljw opened this issue Nov 7, 2024 · 0 comments

Comments

@gaeljw
Copy link
Member

gaeljw commented Nov 7, 2024

This is a feature request, not an issue.

Following the discussion on this Reddit thread, it would be nice if it was supported OOB by play-json. I'm copying the relevant part of the thread here.

From @SnooDogs8860:

In Scala 2.13, and play-json, I used the following pattern to serialize/de-serialize between case classes and Json:

import play.api.libs.json.{Format, Json}

object WelcomeResult {
  implicit val format = Json.format[WelcomeResult]
}

case class WelcomeResult(service: String)

defining implicit formatin the companion object of the case class, made it easily visible from outside, so the following code worked without > even requiring an extra import:

val welcome = WelcomeResult("test")
val json = Json.toJson(welcome)

In Scala 3 I saw the type class derivation feature and though I could take advantage of it by a. making the code more compact. b. defining such a default formatting only once in a generic way in the type class.

This is the implementation I have in mind:

case class WelcomeResult(service: String) derives Format 

From @mkatrenik and @SnooDogs8860, the implementation could like the following (using another custom trait as modifying the PlayJSON one was not possible):

trait JsonFormat[T] extends Format[T]

object JsonFormat:

  inline def derived[T]: JsonFormat[T] =
    new JsonFormat[T]:
      override def reads(json: JsValue): JsResult[T] = Json.reads[T].reads(json)

      override def writes(o: T): JsValue = Json.writes[T].writes(o)

It might only be a matter of opening a PR with the code above, I don't know how much changes would be required in play-json code to integrate it. I guess most of the "issue" is to have it only for Scala 3 and avoiding duplication of having a source file for Scala 2 and one for Scala3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant