summaryrefslogtreecommitdiffstats
path: root/Scala/patmat/project/RichJsValue.scala
blob: ca9ad94174b0d1abc8ef780ce688c6c87a3a2e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
}