Last active
December 11, 2015 09:48
-
-
Save oker1/4582284 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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