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.
fun box get(
input: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val))
: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val | JsonNotFound val)
Parameters¶
Returns¶
- (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val | JsonNotFound val)
set¶
Apply this lens to update a value, returning a new root. Returns JsonNotFound if the path doesn't exist.
fun box set(
input: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val),
value: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val))
: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val | JsonNotFound val)
Parameters¶
- input: (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val)
- value: (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val)
Returns¶
- (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val | JsonNotFound val)
remove¶
Apply this lens to remove a value, returning a new root. Returns JsonNotFound if the path doesn't exist.
fun box remove(
input: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val))
: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val | JsonNotFound val)
Parameters¶
Returns¶
- (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val | 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