std/path/jsonpointer

Standard Library documentation

RFC 6901 JSON Pointer selectors.

Module

Name
std/path/jsonpointer
Area
Standard Library
Source
modules/std/path/jsonpointer.zzm

NAME

std/path/jsonpointer - RFC 6901 JSON Pointer selectors.

SYNOPSIS

  from std/path/jsonpointer import JSONPointer;

  let data := {
    users: [
      { name: "Ada" },
      { name: "Bob" },
    ],
  };

  let pointer := new JSONPointer( path: "/users/0/name" );
  say( pointer.first(data) );

IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

DESCRIPTION

JSONPointer implements JSON Pointer as defined by RFC 6901. It provides the same path API shape as std/path/simple and std/path/z: get, select, query, first, exists, assignment methods, reference methods, and lexical path-operator registration via use.

Syntactically valid pointers that do not resolve return no matches for read APIs.

EXPORTS

Classes

  • JSONPointer({ path: String })

    Constructs a JSON Pointer selector. Returns: JSONPointer.

    • JSONPointer.use()

      Parameters: none. Returns: null. Makes this path class the lexical implementation for @, @@, and @?.

    • pointer.expression()

      Parameters: none. Returns: String. Returns the original pointer expression.

    • pointer.get(value), pointer.select(value),

      pointer.query(value)

      Parameters: value is the query root. Returns: Array. Evaluates the pointer and returns selected values.

    • pointer.first(value, fallback?)

      Parameters: value is the query root and fallback is optional. Returns: value. Returns the selected value, or fallback/null when there is no match.

    • pointer.exists(value)

      Parameters: value is the query root. Returns: Boolean. Returns true when the pointer resolves to a concrete value.

    • pointer.assign_first(target, value, op := ":=", weak := false)

      Parameters: target is the query root, value is the assignment value, op is an assignment operator, and weak is accepted for path API compatibility. Returns: value. Updates the selected location.

    • pointer.assign_all(target, value, op := ":=", weak := false)

      Parameters: same as assign_first. Returns: value. Updates the selected location when it exists.

    • pointer.assign_maybe(target, value, op := ":=", weak := false)

      Parameters: same as assign_first. Returns: Boolean. Updates the selected location when it exists.

    • pointer.ref_first(target)

      Parameters: target is the query root. Returns: Function. Returns a reference-like getter/setter for the selected location.

    • pointer.ref_all(target)

      Parameters: target is the query root. Returns: Array. Returns a single reference-like getter/setter when the pointer resolves, otherwise an empty array.

    • pointer.ref_maybe(target)

      Parameters: target is the query root. Returns: Function or null. Returns a reference-like getter/setter when the pointer resolves.

Functions

  • extract_pointer_from_url(url)

    Parameters: url is a String. Returns: Dict. Splits a URL into baseurl and pointer. If the URL contains #, baseurl is the text before # and pointer is the URI-fragment percent-decoded JSON Pointer. If the URL has no #, baseurl is the full URL and pointer is null.

SEE ALSO

Specification: https://www.rfc-editor.org/rfc/rfc6901.

COPYRIGHT AND LICENCE

std/path/jsonpointer is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.