score:3

Accepted answer

If you really want Id to be serialized as a JSON string, here you go:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Id(value: String)

implicit val idFormats: Format[Id] =
  implicitly[Format[String]].inmap(Id, unlift(Id.unapply))

Json.toJson(Id("asd")) == JsString("asd")
Json.toJson(Id("asd")).toString == "\"asd\""
Json.parse(Json.toJson(Id("asd")).toString).as[Id] == Id("asd")

I wrote it this way to clearly illustrate that you use nothing else that a basic String formatter, which is defined in play-json.


Related Query

More Query from same tag