Created
January 15, 2012 13:42
-
-
Save systay/1615896 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
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