简体   繁体   中英

Play2 Scala - Json format with circular dependency

I have two classes, and each of them contains the other:

import play.api.libs.json.Json

case class Param(name: String,
                 data: ParamData)
case class ParamData(`type`: String,
                     value: String,
                     options: Option[List[Param]])

implicit val paramDataJsonFormat = Json.format[ParamData]
implicit val paramJsonFormat = Json.format[Param]

when I define the Json format I get a compilation error:

Error:(110, 51) No implicit format for List[Param]] available.

implicit val paramJsonFormat = Json.format[Param]

If I change the order of the implicits I get the same error on the other line:

Error:(110, 51) No implicit format for List[ParamData]] available.

implicit val paramDataJsonFormat = Json.format[ParamData]

How can I solve this?

Found the solution in the Play official documentation . There is a specific section regarding recursive types I was missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM