在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jrudolph/json-lenses开源软件地址:https://github.com/jrudolph/json-lenses开源编程语言:Scala 100.0%开源软件介绍:json-lenses is a library to query and update JSON data. It has the following features
UsageIf you use SBT you can include json-lenses in your project with
(json-lenses supports spray-json 1.3.x. For support of an older spray-json version use json-lenses 0.5.4. See https://github.com/jrudolph/json-lenses/tree/v0.5.4-scala-2.11 for the old documentation). ExampleGiven this example json document: val json = """
{ "store": {
"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"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}""".parseJson All authors in this document are addressed by import spray.json.lenses.JsonLenses._
import spray.json.DefaultJsonProtocol._
val allAuthors = 'store / 'book / * / 'author This is called a lens. You can use a lens to retrieve or update the addressed values. val authorNames = json.extract[String](allAuthors) To update values use one of the defined operations. To overwrite a value use // 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 " + _)) Here are other interesting queries on the example data: // The author of the first book in the store
val firstAuthor = "store" / "book" / element(0) / "author"
// The titles of books more expensive than $ 10
val expensiveBookTitles = "store" / "book" / filter("price".is[Double](_ >= 10)) / "title"
// ISBN of all books that have one
val allIsbn = 'store / 'book / * / 'isbn.? // not all books have ISBNs, so the selection must be optional DocumentationThe conceptThe concept of lenses is a powerful concept not only applicable to json objects. A lens is an updatable, composable view into a data structure. There are simple lenses which provide just the functionality to "go one level deeper" in the data structure (for json: accessing fields of a json object or elements of a json array) and there are lenses which compose other lenses to allow a deeper view into the data structure. See this answer on stack overflow and this presentation for more info on the general topic of lenses. The json lensThe json lenses in this project are specialized lenses to extract and update json data. A lens has this form (almost, in reality it's a bit more complicated): // not the real code
trait Lens {
def retr: JsValue => JsValue
def updated(f: JsValue => JsValue)(parent: JsValue): JsValue
} It has a method In contrast to a more general lens input and output types of the json lens are fixed: both have to be an
instance of Support for multiple cardinalitiesThe simple scheme introduced in the last section is actually too simple to support more than the absolute simplest lenses. One basic requirement is to extract a list of values or an optional value. Therefore, lenses are parameterized by the container type for the cardinality of the lens. // still not the real code
trait Lens[M[_]] {
def retr: JsValue => M[JsValue]
// ... Scalar lenses are of type It's interesting what happens when two lenses of different cardinality are joined: The rule is to always return a lens
of the more general container type, i.e. the one with the greater cardinality. Joining two lenses of sequences of values
results in a flattened sequence of all the results. See the source code of Error handlingTo support proper error handling and recovery from errors (in some cases) failure is always assumed as a
possible outcome. This is reflected by returning an type Validated[T] = Either[Exception, T]
type SafeJsValue = Validated[JsValue]
trait Lens[M[_]] {
def retr: JsValue => Validated[M[JsValue]]
def updated(f: SafeJsValue => SafeJsValue)(parent: JsValue): SafeJsValue
} The result of Predefined lensesWhen working with lenses you normally don't have to worry about the details but you can just choose and combine lenses from the following list. Field access
Element access
Combination
Predefined update operationsCurrently, there are these update operations defined:
Using lenses to extract or update json dataTo extract a value from json data using a lens you can use either To update a value use either json-path supportUse API DocumentationYou can find the documentation for the json-lenses API here. What's missing
Please file an issue at the issue tracker if you need something. Mailing listPlease use the spray-user mailing list to discuss json-lenses issues. Licensespray-json is licensed under APL 2.0. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论