summaryrefslogtreecommitdiff
path: root/src/main/scala/io/trygvis/cj/Json4sHelpers.scala
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-05-19 23:09:39 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-05-19 23:09:39 +0200
commitc7b619402f290fdf77312af5f426eb6bd0d5e5f7 (patch)
treec161afb788fbfb0610b6b70a7470b7d63a58cdc0 /src/main/scala/io/trygvis/cj/Json4sHelpers.scala
parentc569a4c58cf29778df28392b48e20ffebdbc518e (diff)
downloadcollection-json-explorer-scala-c7b619402f290fdf77312af5f426eb6bd0d5e5f7.tar.gz
collection-json-explorer-scala-c7b619402f290fdf77312af5f426eb6bd0d5e5f7.tar.bz2
collection-json-explorer-scala-c7b619402f290fdf77312af5f426eb6bd0d5e5f7.tar.xz
collection-json-explorer-scala-c7b619402f290fdf77312af5f426eb6bd0d5e5f7.zip
o Implementing my own model of collection+json. My browser want to be more relaxed than what normally makes sense.
o Implementing the server side of the queries.
Diffstat (limited to 'src/main/scala/io/trygvis/cj/Json4sHelpers.scala')
-rw-r--r--src/main/scala/io/trygvis/cj/Json4sHelpers.scala47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/scala/io/trygvis/cj/Json4sHelpers.scala b/src/main/scala/io/trygvis/cj/Json4sHelpers.scala
new file mode 100644
index 0000000..0241e59
--- /dev/null
+++ b/src/main/scala/io/trygvis/cj/Json4sHelpers.scala
@@ -0,0 +1,47 @@
+package io.trygvis.cj
+
+import org.json4s._
+
+object Json4sHelpers {
+ def getAsObjectList(obj: JObject, name: String): List[JObject] = {
+ (obj \ name) match {
+ case JArray(values) => values.collect{case o@JObject(_) => o}
+ case o@JObject(_) => List(o)
+ case _ => Nil
+ }
+ }
+
+ def getAsValueList(obj: JObject, name: String): List[JValue] = {
+ (obj \ name) match {
+ case JNothing => Nil
+ case JArray(values) => values
+ case j => List(j)
+ }
+ }
+
+ def getAsObject(obj: JObject, name: String): Option[JObject] = {
+ (obj \ name) match {
+ case o@JObject(_) => Some(o)
+ case _ => None
+ }
+ }
+
+ def getAsString(obj: JObject, name: String): Option[String] = {
+ (obj \ name) match {
+ case JString(s) => Some(s)
+ case _ => None
+ }
+ }
+
+ def replace(obj: JObject, name: String, value: JValue): JObject = {
+ val map = obj.obj.toMap - name
+ JObject(map.updated(name, value).toList)
+ }
+
+ def filtered(obj: JObject): JObject = {
+ JObject(obj.obj.filter {
+ case JField(_, JNothing) => false
+ case _ => true
+ })
+ }
+}