diff options
-rw-r--r-- | src/library/scala/util/parsing/json/JSON.scala | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/library/scala/util/parsing/json/JSON.scala b/src/library/scala/util/parsing/json/JSON.scala index 4ac50a4f8c..734a8de008 100644 --- a/src/library/scala/util/parsing/json/JSON.scala +++ b/src/library/scala/util/parsing/json/JSON.scala @@ -11,44 +11,54 @@ package scala.util.parsing.json -import scala.collection.mutable.HashMap - -/** This object mainly shows how a JSON parser may be instantiated. +/** + * This object provides a simple interface to the JSON parser class. * * @author Derek Chen-Becker <"java"+@+"chen-becker"+"."+"org"> */ object JSON extends Parser { + /** + * Parse the given JSON string and return a List of elements. If the + * string is a JSON object it will be a list of Pairs. If it's a JSON + * array it will be be a list of individual elements. + */ def parse(input: String) = phrase(root)(new lexical.Scanner(input)) match { case Success(result, _) => Some(result) case _ => None } + /** + * Parse the given JSON string and return either a List[Any] if the JSON string + * specifies an Array, or a Map[String,Any] if the JSON string specifies an + * object. + */ def parseFull(input: String) = parse(input) match { case Some(data) => resolveType(data) case None => None } - def resolveType(input: Any): Any = + /** + * A utility method to resolve a parsed JSON list into objects or + * arrays. See the parse method for details. + */ + def resolveType(input: List[Any]): Any = input match { - case jo: List[_] => - /*println("Resolving object")*/ - val objMap = new HashMap[String, Any]() + case jo: List[Any] => + var objMap = Map[String, Any]() if(jo.forall { - case (key: String, value) => - objMap.update(key, resolveType(value)) + case (key: String, value : List[Any]) => + objMap = objMap + key -> resolveType(value) true case _ => false }) objMap else { - /*println("Resolving array"); */ - jo.toArray + jo } case _ @ elem => - /*println("Resolving element"); */ elem } } |