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: JSONValue)
: (JSONValue | 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: JSONValue,
  value: JSONValue)
: (JSONValue | 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: JSONValue)
: (JSONValue | 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