I want to make marshaller for case class which has fields referred to the same class.
case class TreeNode (name: String, parentNode: Option[TreeNode])
if i make serializer
implicit val nodeJson = jsonFormat2(TreeNode)
i get an error that no imlicits found for parameter Option[TreeNode] is there are any way to solve such problem except writing serialization from scratch?
PS Some more attempts i made with encoder
private def toNode (node: TreeNode): Map[String, Any] = {
val parent = node.parentNode.map(toNode).orNull
Map[String, Any] ("name" -> node.name, "parentNode" -> parent)
}
implicit val treeNodeEncoder: Encoder[TreeNode] =
Encoder.forProduct2("name", "parentNode")(n =>
(n.name, n.parentNode.map(toNode).orNull)
)
it does not work as well, because Map[String, Any] has no implicit either
circe supports this out of the box, you just need to use the semiautomatic / automatic derivation mechanism provided by the library.
You can also modify the printer you want to use to control the identation and if you want to include or not
nulls
Take a look to this code:
You can see it running here