| 63992 |      1 | /*  Title:      Pure/General/json.scala
 | 
| 63644 |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for JSON parsing.
 | 
|  |      5 | */
 | 
|  |      6 | 
 | 
|  |      7 | package isabelle
 | 
|  |      8 | 
 | 
|  |      9 | 
 | 
|  |     10 | object JSON
 | 
|  |     11 | {
 | 
|  |     12 |   /* parse */
 | 
|  |     13 | 
 | 
|  |     14 |   def parse(text: String): Any =
 | 
|  |     15 |     scala.util.parsing.json.JSON.parseFull(text) getOrElse error("Malformed JSON")
 | 
|  |     16 | 
 | 
|  |     17 | 
 | 
|  |     18 |   /* field access */
 | 
|  |     19 | 
 | 
|  |     20 |   def any(obj: Any, name: String): Option[Any] =
 | 
|  |     21 |     obj match {
 | 
|  |     22 |       case m: Map[String, Any] => m.get(name)
 | 
|  |     23 |       case _ => None
 | 
|  |     24 |     }
 | 
|  |     25 | 
 | 
|  |     26 |   def array(obj: Any, name: String): List[Any] =
 | 
|  |     27 |     any(obj, name) match {
 | 
|  |     28 |       case Some(a: List[Any]) => a
 | 
|  |     29 |       case _ => Nil
 | 
|  |     30 |     }
 | 
|  |     31 | 
 | 
|  |     32 |   def string(obj: Any, name: String): Option[String] =
 | 
|  |     33 |     any(obj, name) match {
 | 
|  |     34 |       case Some(x: String) => Some(x)
 | 
|  |     35 |       case _ => None
 | 
|  |     36 |     }
 | 
|  |     37 | 
 | 
|  |     38 |   def double(obj: Any, name: String): Option[Double] =
 | 
|  |     39 |     any(obj, name) match {
 | 
|  |     40 |       case Some(x: Double) => Some(x)
 | 
|  |     41 |       case _ => None
 | 
|  |     42 |     }
 | 
|  |     43 | 
 | 
|  |     44 |   def long(obj: Any, name: String): Option[Long] =
 | 
|  |     45 |     double(obj, name).map(_.toLong)
 | 
|  |     46 | 
 | 
|  |     47 |   def int(obj: Any, name: String): Option[Int] =
 | 
|  |     48 |     double(obj, name).map(_.toInt)
 | 
|  |     49 | 
 | 
|  |     50 |   def bool(obj: Any, name: String): Option[Boolean] =
 | 
|  |     51 |     any(obj, name) match {
 | 
|  |     52 |       case Some(x: Boolean) => Some(x)
 | 
|  |     53 |       case _ => None
 | 
|  |     54 |     }
 | 
|  |     55 | }
 |