summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Chen-Beker <dchenbecker@gmail.com>2008-03-12 13:47:58 +0000
committerDerek Chen-Beker <dchenbecker@gmail.com>2008-03-12 13:47:58 +0000
commitda97d90a01a2c76e30a2308f531ed5aecaa36e3b (patch)
treeb5725a2bc56371154b014899ae866053f0a62668
parentbb1cc87c92a35468ac4322276caca7a601bf2352 (diff)
downloadscala-da97d90a01a2c76e30a2308f531ed5aecaa36e3b.tar.gz
scala-da97d90a01a2c76e30a2308f531ed5aecaa36e3b.tar.bz2
scala-da97d90a01a2c76e30a2308f531ed5aecaa36e3b.zip
Changed the return type for parseFull to List f...
Changed the return type for parseFull to List for JSON arrays and Map for JSON objects. Also added scaladoc comments and cleaned up some old/unneeded code.
-rw-r--r--src/library/scala/util/parsing/json/JSON.scala34
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
}
}