JSONLens¶
Composable, reusable JSON path for reading and modifying nested values.
Define a lens by chaining key/index steps, then apply it to any document:
let host_lens = JSONLens("config")("database")("host")
// Read
match host_lens.get(doc)
| let host: String => env.out.print(host)
| JSONNotFound => env.out.print("no host configured")
end
// Modify (returns new document with the change applied)
match host_lens.set(doc, "newhost.example.com")
| let updated: JSONValue => // updated doc
| JSONNotFound => // path didn't exist
end
Constructors¶
create¶
Create an identity lens (focuses on the root value).
Returns¶
- JSONLens val^
Public Functions¶
apply¶
Compose a navigation step onto this lens.
Parameters¶
Returns¶
- JSONLens val
get¶
Apply this lens to read a value.
Parameters¶
- input: JSONValue
Returns¶
- (JSONValue | JSONNotFound val)
set¶
Apply this lens to update a value, returning a new root. Returns JSONNotFound if the path doesn't exist.
Parameters¶
Returns¶
- (JSONValue | JSONNotFound val)
remove¶
Apply this lens to remove a value, returning a new root. Returns JSONNotFound if the path doesn't exist.
Parameters¶
- input: JSONValue
Returns¶
- (JSONValue | JSONNotFound val)
compose¶
Sequential composition: navigate this lens, then the other.
Parameters¶
- other: JSONLens val
Returns¶
- JSONLens val
or_else¶
Choice: try this lens, fall back to alt if JSONNotFound.
Parameters¶
- alt: JSONLens val
Returns¶
- JSONLens val