diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-28 00:33:06 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 3e2f88773c61e2d0020b57161d0e901bec596c96 (patch) | |
tree | fe5c381e4a731600a60b4ac9862af672b5b7aa12 /Scala/patmat/project/RichJsValue.scala | |
parent | 12ef23e6da7b7186bec20d426c087d757260659d (diff) | |
download | coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.zip coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.tar.gz |
Scala : add patmat
Diffstat (limited to 'Scala/patmat/project/RichJsValue.scala')
-rw-r--r-- | Scala/patmat/project/RichJsValue.scala | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Scala/patmat/project/RichJsValue.scala b/Scala/patmat/project/RichJsValue.scala new file mode 100644 index 0000000..ca9ad94 --- /dev/null +++ b/Scala/patmat/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) +} |