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