Skip to content

Instantly share code, notes, and snippets.

@oker1
Last active December 11, 2015 09:48
Show Gist options
  • Save oker1/4582284 to your computer and use it in GitHub Desktop.
Save oker1/4582284 to your computer and use it in GitHub Desktop.
sealed abstract class JValue {
override def toString: String = {
this match {
case JString(value) => String.format("\"%s\"", value)
case JBool(true) => "true"
case JBool(false) => "false"
case JNumber(value) => value.toString
case JNull() => "null"
case JList(values) => "[" ++ values.map(_.toString).mkString(", ") ++ "]"
case JObject(values) => "{" ++ values.foldLeft("") { case (prefix, (key, value)) => prefix ++ String.format("\"%s\": %s", key, value) } ++ "}"
}
}
}
object JList {
def apply(list: JValue*) = {
new JList(list.toList)
}
}
case class JString(value: String) extends JValue
case class JBool(value: Boolean) extends JValue
case class JNumber(value: Double) extends JValue
case class JNull() extends JValue
case class JList(value: List[JValue]) extends JValue
case class JObject(value: Map[String, JValue]) extends JValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment