diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-03-27 23:25:43 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:20 +0100 |
commit | dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a (patch) | |
tree | f5ab0b7e1ab3e2186dd864230419c5dc20404c1f /Scala/example/project/RichJsValue.scala | |
parent | ab61b59311fc1428a8edbe99b103ab77627c7dc8 (diff) | |
download | coursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.zip coursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.tar.gz |
Scala : add scala/example
Diffstat (limited to 'Scala/example/project/RichJsValue.scala')
-rw-r--r-- | Scala/example/project/RichJsValue.scala | 28 |
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) +} |