Skip to content

Instantly share code, notes, and snippets.

@systay
Created January 15, 2012 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save systay/1615896 to your computer and use it in GitHub Desktop.
Save systay/1615896 to your computer and use it in GitHub Desktop.
import org.junit.Test
import util.parsing.combinator.JavaTokenParsers
class ParserTest extends JavaTokenParsers {
@Test def aorb() {
val json = """{ "address book": John }"""
val r = parse(json)
println(r)
}
def obj: Parser[Map[String, Any]] =
"{" ~> repsep(member, ",") <~ "}" ^^ (Map() ++ _)
def arr: Parser[List[Any]] =
"[" ~> repsep(value, ",") <~ "]"
def member: Parser[(String, Any)] =
stringLiteral ~ ":" ~ value ^^ {
case name ~ ":" ~ value => (name, value)
}
def value: Parser[Any] = obj | arr | stringLiteral | floatingPointNumber ^^ (_.toDouble) | "null" ^^ (x => null) | "true" ^^ (x => true) | "false" ^^ (x => false) | failure("illegal start of value")
def parse(txt: String) = parseAll(obj, txt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment