Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created January 8, 2014 18:29
Show Gist options
  • Save tpolecat/8321821 to your computer and use it in GitHub Desktop.
Save tpolecat/8321821 to your computer and use it in GitHub Desktop.
implicit def treeEncode[A: EncodeJson]: EncodeJson[Tree[A]] =
EncodeJson(t => (t.rootLabel, t.subForest).asJson)
implicit def treeDecode[A: DecodeJson]: DecodeJson[Tree[A]] =
DecodeJson(c =>
for {
rootLabel <- (c =\ 0).as[A]
subForest <- (c =\ 1).as[Stream[Tree[A]]]
} yield Tree.node(rootLabel, subForest)
)
@LeifW
Copy link

LeifW commented Jan 9, 2014

I was wondering how this would work; I guess treeEncode can be applied to t.subForest again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment