This JSON automated mapping example from the play documentation fails. why? https://www.playframework.com/documentation/2.5.x/ScalaJsonAutomated
libraryDependencies += "com.typesafe.play" %% "play" % "2.5.0"
---
import play.api.libs.json._
case class Resident(name: String, age: Int, role: Option[String])
implicit val residentWrites = Json.writes[Resident]
println(Json.toJson(Resident("john", 33, None)))
---
Error: No unapply or unapplySeq function found
implicit val residentWrites = Json.writes[Resident]
The problematic code looked more or less like that:
The issue here was that this macro apparently doesn't work for classes defined inside methods. This is not a troubling limitation though as we rather don't do this sort of things.
To resolve issue class def can be moved somewhere else, like object level
or file level
I understand that this was only for test purposes to see minimal example, but I will still mention how we usually define classes with
Writes
.This way, whenever you import the
Resident
, its writes will be in the implicit scope.