summaryrefslogtreecommitdiff
path: root/src/test/scala
diff options
context:
space:
mode:
authorJohannes Rudolph <johannes.rudolph@gmail.com>2013-05-29 14:08:33 +0200
committerJohannes Rudolph <johannes.rudolph@gmail.com>2013-05-29 14:08:33 +0200
commitc80dfc25c25a63da02ad4819988a19a4b5656652 (patch)
tree21d89b77c65676bfc16cd3b2739afb8bc19b442a /src/test/scala
parent6737d9b134aa07f54cba4171e8d8633898787333 (diff)
downloadspray-json-c80dfc25c25a63da02ad4819988a19a4b5656652.tar.gz
spray-json-c80dfc25c25a63da02ad4819988a19a4b5656652.tar.bz2
spray-json-c80dfc25c25a63da02ad4819988a19a4b5656652.zip
add `optionalField` lens, fixes #3, #4
This allows access to fields that maybe are missing. Subfeatures: * `'field.?`-syntax as shortcut to optionalField * optional fields can be `modify`d * an optional field can be `set` in which case it gets created if it is missing * json-path field syntax automatically uses `optionalField` instead of `field`, which implements the spec better than before
Diffstat (limited to 'src/test/scala')
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala11
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala11
2 files changed, 22 insertions, 0 deletions
diff --git a/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala b/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala
index 17ebbed..d8ba767 100644
--- a/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala
+++ b/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala
@@ -25,6 +25,9 @@ class JsonLensesSpec extends Specification with SpecHelpers {
"""{"n": 2}""".extract[String]('n) must throwA[DeserializationException]("Expected String as JsString, but got 2")
}
}
+ "optional field" in {
+ """[{"b": 4}, {"c": 5}]""".extract[Int](* / 'b.?) must be_==(Seq(4))
+ }
"field of member" in {
"""{"n": {"b": 4}}""".extract[Int]("n" / "b") must be_==(4)
}
@@ -114,6 +117,14 @@ class JsonLensesSpec extends Specification with SpecHelpers {
"""{"n": 12}""" update (field("z") ! modify[Int](_ + 1)) must throwAn[Exception]( """Expected field 'z' in '{"n":12}'""")
}
}
+ "optional field" in {
+ "modify" in {
+ """[{"b": 4}, {"c": 5}]""".update((* / 'b.?) ! modify[Int](_ + 12)) must be_json("""[{"b": 16}, {"c": 5}]""")
+ }
+ "create" in {
+ """[{"b": 4}, {"c": 5}]""".update((* / 'b.?) ! set(38)) must be_json("""[{"b": 38}, {"c": 5, "b": 38}]""")
+ }
+ }
"set field of member" in {
"""{"n": {"b": 4}}""" update ("n" / "b" ! set(23)) must be_json( """{"n": {"b": 23}}""")
}
diff --git a/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala b/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala
index d1cff83..707671b 100644
--- a/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala
+++ b/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala
@@ -55,6 +55,9 @@ class JsonPathExamplesSpec extends Specification with SpecHelpers {
"All books that have isbn" in {
json.extract[String](("store" / "book" / filter("isbn".is[JsValue](_ => true)) / "title")) must be_==(Seq("Sword of Honour"))
}
+ "Isbn of books that have isbn" in {
+ json.extract[String](("store" / "book" / * / "isbn".?)) must be_==(Seq("0-553-21311-3"))
+ }
"All prices" in todo
}
"With Json-Path syntax" in {
@@ -75,6 +78,14 @@ class JsonPathExamplesSpec extends Specification with SpecHelpers {
"All books that have isbn" in {
json.extract[String](fromPath("$.store.book[?(@.isbn)].title")) must be_==(Seq("Sword of Honour"))
}
+ "Isbn of books that have isbn" in {
+ val lens = fromPath("$.store.book[*].isbn")
+ json.extract[String](lens) must be_==(Seq("0-553-21311-3"))
+
+ val expected =
+ JsonParser("""{"store":{"bicycle":{"color":"red","price":19.95},"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99,"isbn":"0-553-21311-3?"}]}}""")
+ json.update(lens ! modify[String](_ + "?")) must be_==(expected)
+ }
"All prices" in todo
}
}