Created
November 11, 2017 13:56
-
-
Save tjheslin1/c3ab421655e80c1a5ac30dfb8798d971 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Node { | |
def print: String = | |
this match { | |
case Obj(v) => v.toString | |
case JSONObj(v) => s"{\n${v.print}\n}" | |
case Element(k, v) => s"""\t"$k": "${v.print}",\n""" | |
case JSONEnd => "" | |
case JSONPair(hd, tl) => s"${hd.print} ${tl.print}" | |
case JSONSeq(hd, tl) => s"[ ${hd.print} ${tl.print} ]" | |
} | |
} | |
final case class Obj(val value: Any) extends Node | |
final case class Element(val key: String, val value: Node) extends Node | |
sealed trait JSONList extends Node | |
final case object JSONEnd extends JSONList | |
final case class JSONObj(val value: Node) extends Node | |
final case class JSONPair(val head: Node, val tail: JSONList) extends JSONList | |
final case class JSONSeq(val head: Node, val tail: JSONList) extends JSONList | |
val example1: JSONList = JSONSeq(Obj("a string"), JSONPair(Obj(1.0), JSONPair(Obj(true), JSONEnd))) | |
val example2 = JSONObj( | |
JSONPair( | |
Element("a", JSONPair(Obj(1), JSONPair(Obj(2), JSONPair(Obj(3), JSONEnd)))), | |
JSONPair( | |
Element("b", JSONPair(Obj("a"), JSONPair(Obj("b"), JSONPair(Obj("c"), JSONEnd)))), | |
JSONPair( | |
Element("c", JSONObj(JSONPair( | |
Element("doh", Obj(true)), | |
JSONPair( | |
Element("Ray", Obj(false)), | |
JSONPair( | |
Element("me", Obj(1)), | |
JSONEnd | |
) | |
) | |
))), | |
JSONEnd | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment