summaryrefslogtreecommitdiffstats
path: root/Scala/example/project/RichJsValue.scala
diff options
context:
space:
mode:
Diffstat (limited to 'Scala/example/project/RichJsValue.scala')
-rw-r--r--Scala/example/project/RichJsValue.scala28
1 files changed, 28 insertions, 0 deletions
diff --git a/Scala/example/project/RichJsValue.scala b/Scala/example/project/RichJsValue.scala
new file mode 100644
index 0000000..ca9ad94
--- /dev/null
+++ b/Scala/example/project/RichJsValue.scala
@@ -0,0 +1,28 @@
+import cc.spray.json._
+
+class RichJsValue(js: JsValue) {
+ def \ (name: String): JsValue = js match {
+ case JsObject(fields) =>
+ fields(name)
+ case _ =>
+ throw new IllegalArgumentException("Cannot select field "+ name +" from non-JsObject "+ js)
+ }
+
+ def hasFieldNamed(name: String) = js match {
+ case JsObject(fields) =>
+ fields.contains(name)
+ case _ =>
+ false
+ }
+
+ def arrayValues: List[JsValue] = js match {
+ case JsArray(values) =>
+ values
+ case _ =>
+ throw new IllegalArgumentException("Trying to select values from non-JsArray"+ js)
+ }
+}
+
+object RichJsValue {
+ implicit def enrichJsValue(js: JsValue) = new RichJsValue(js)
+}