Skip to content

JsonLens

[Source]

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

class val JsonLens

Constructors

create

[Source]

Create an identity lens (focuses on the root value).

new val create()
: JsonLens val^

Returns


Public Functions

apply

[Source]

Compose a navigation step onto this lens.

fun box apply(
  key_or_index: (String val | USize val))
: JsonLens val

Parameters

Returns


get

[Source]

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


set

[Source]

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

Returns


remove

[Source]

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


compose

[Source]

Sequential composition: navigate this lens, then the other.

fun box compose(
  other: JsonLens val)
: JsonLens val

Parameters

Returns


or_else

[Source]

Choice: try this lens, fall back to alt if JsonNotFound.

fun box or_else(
  alt: JsonLens val)
: JsonLens val

Parameters

Returns