Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View ParserTest.scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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.