I'm facing a strange error where I'm trying to parse a JSON String into a generic case class. My case class looks like this:
final case class LocationAPIObject[F[_]](
countryCode: F[String],
partyId: F[String],
uid: F[String]
)
For a JSON that comes in like this:
val js: JsValue = Json.parse("""{
"countryCode": "us",
"partyId": "123456"
}""")
It results in a parse error:
diverging implicit expansion for type play.api.libs.json.Reads[T]
starting with method Tuple22R in trait GeneratedReads
Here is the sample code that I'm working on: https://scastie.scala-lang.org/aBQIUCTvTECNPgSjCjDFlw
You have
diverging implicit expansionerror because you haven't specified type parameter. If you dothen the error changes to
implicit not found: play.api.libs.json.Reads[LocationAPIObject[scala.Option]]. No Json deserializer found for type LocationAPIObject[Option]. Try to implement an implicit Reads or Format for this type.The case class
LocationAPIObjectbeing parametrized with higher-kindedF("fields which are encapsulated effects") shouldn't be a problem. The fields are of typeF[String]so in order to derive instances of the type classReadsorFormatforLocationAPIObject[F]it should be enough to know instances forF[String].But while this works for example in Circe
for some reason it doesn't in Play json:
or
or
So the thing seems to be in Play json derivation macros.
The easiest would be to define a codec manually as it was adviced in the comments.