Issue with Play Serialization for nested types

Version: 2.6

Issue:
I have the below case class

case class TestResponse(id: Seq[Option[String]])

object TestResponse {
implicit val testResponseFormat= Json.format[TestResponse]
}

While compiling, I keep on getting the error:

No instance of play.api.libs.json.Format is available for scala.collection.immutable.Seq[scala.Option[java.lang.String]] in the implicit scope (Hint: if declared in the same file, make sure it’s declared before)

Is this supported in play or am I missing something?

@aironm Pls check this section on JSON serialization. Has examples for complex types.

https://www.playframework.com/documentation/2.6.x/ScalaJson

HTH
Aditya

Thanks Aditya for the reply.

After some debugging the issue turned out to be how play sequences the implicits for Formats. Since I had Seq[Option[String]], it was failing as it was not able to find Option format since it is defined after the Seq format. I explicitly provided the Format[Option[String]].

object TestResponse {
implicit val testResponseFormat= {
   implicit val optionFormat: Format[Option[String]] = Format.optionWithNull
   Json.format[TestResponse]
}
}
1 Like