diff options
author | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-06-04 16:04:23 +0300 |
---|---|---|
committer | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-06-04 16:04:23 +0300 |
commit | 0ad757032defe9cafb2659e4015a6fee9a17a745 (patch) | |
tree | 1d2ea51e93e5c14b1f98bbf0b4bee6ce8336911a | |
parent | 4f60f866f6f7050895974918b8dc767826367444 (diff) | |
download | spray-json-0ad757032defe9cafb2659e4015a6fee9a17a745.tar.gz spray-json-0ad757032defe9cafb2659e4015a6fee9a17a745.tar.bz2 spray-json-0ad757032defe9cafb2659e4015a6fee9a17a745.zip |
using lenses section
-rw-r--r-- | README.md | 18 |
1 files changed, 16 insertions, 2 deletions
@@ -60,8 +60,8 @@ To update values use one of the defined operations. To overwrite a value use `se based on the previous value use `modify`. ```scala -// overwrite all authors' names to "ABC" -val newJson1 = json.update(allAuthors ! set[String]("ABC")) +// overwrite all authors' names to "John Doe" +val newJson1 = json.update(allAuthors ! set[String]("John Doe")) // prepend authors' names with "Ms or Mr " val newJson2 = json.update(allAuthors ! modify[String]("Ms or Mr " + _)) @@ -175,7 +175,21 @@ from the following list. #### Predefined update operations +Currently, there are these update operations defined: + * `set[T: JsonWriter](t: => T)` uses the `JsonWriter` to set the value to a constant value. + * `modify[T: JsonFormat](f: T => T)` applies an update function to an existing value. The value has to be + serializable from and to `T`. + +#### Using lenses to extract or update json data + +To extract a value from json data using a lens you can use either `lens.get[T](json)` or the equivalent +`json.extract[T](lens)` which both throw an exception in case something fails or use `lens.tryGet[T](json)` +which returns an `Either` value you can match on (or use `Either`'s functions) to check for errors. + +To update a value use either `(lens ! operation).apply(json)` or `json.update(lens ! operation)`. You can +also use the fancy `val newJson = json(lens) = "abc"` syntax if you like that. These operations throw an exception +in the error case. Use `lens.updated(operation)(json)` to get an `Either` result to process errors. #### [json-path] support |