Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.