summaryrefslogtreecommitdiffstats
path: root/Scala/forcomp/project/RichJsValue.scala
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-05-11 22:30:17 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:24 +0100
commitf693c7f267e2668be5626413d4eee8600630b6be (patch)
tree10ede8ee23560a228175074a59bab72f9e52a143 /Scala/forcomp/project/RichJsValue.scala
parent5c5087a4a7f8342946ffe0ccbf1a244d7d4790dc (diff)
downloadcoursera-f693c7f267e2668be5626413d4eee8600630b6be.zip
coursera-f693c7f267e2668be5626413d4eee8600630b6be.tar.gz
Scala : Scala : add forcomp assignment
Diffstat (limited to 'Scala/forcomp/project/RichJsValue.scala')
-rw-r--r--Scala/forcomp/project/RichJsValue.scala28
1 files changed, 28 insertions, 0 deletions
diff --git a/Scala/forcomp/project/RichJsValue.scala b/Scala/forcomp/project/RichJsValue.scala
new file mode 100644
index 0000000..ca9ad94
--- /dev/null
+++ b/Scala/forcomp/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)
+}