Play Json serialize/deserialize mutual recursive

2020-01-19 07:53发布

问题:

How to serialize and deserialize these structures using Play Json combinators?

final case class WriteGroupEntity(label: String, items: Map[String, WriteEntity])
final case class WriteEntity(label: String,
                             propertyType: String,
                             groups: Option[Map[String, WriteGroupEntity]])

回答1:

Two days are gone and I've finally found a solution.

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

final case class GroupEntity(label: String, items: Map[String, Entity])
final case class Entity(label: String,
                        propertyType: String,
                        groups: Option[Map[String, GroupEntity]])

implicit lazy val ge: OWrites[GroupEntity] = (
  (JsPath \ "label").write[String] and
    (JsPath \ "items").lazyWrite(Writes.map[Entity](e))
)(unlift(GroupEntity.unapply))

implicit val e: OWrites[Entity] = Json.writes[Entity]

implicit lazy val ger: Reads[GroupEntity] = (
  (JsPath \ "label").read[String] and
    (JsPath \ "items").lazyRead(Reads.map[Entity](er))
)(GroupEntity)

implicit val er = Json.reads[Entity]