public
Created

  • Download Gist
ParserTest.scala
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)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.