std/data/json/schema/relative_pointer

Standard Library documentation

Relative JSON Pointer parser.

Module

Name
std/data/json/schema/relative_pointer
Area
Standard Library
Source
modules/std/data/json/schema/relative_pointer.zzm

NAME

std/data/json/schema/relative_pointer - Relative JSON Pointer parser.

SYNOPSIS

  from std/data/json/schema/relative_pointer import RelativeJSONPointer;

  let doc := {
    people: [
      { name: "Ada" },
      { name: "Grace" },
    ],
  };

  let pointer := new RelativeJSONPointer( path: "1/name" );
  say( pointer.first( doc, "/people/0/age", "(missing)" ) );

IMPLEMENTATION SUPPORT

This Pure Zuzu module is supported by all implementations of ZuzuScript.

DESCRIPTION

Relative JSON Pointer describes a target by starting from a context location, climbing zero or more levels, optionally shifting an array index, and then optionally applying a normal JSON Pointer suffix. JSON Schema uses the syntax for the relative-json-pointer format.

This module evaluates relative pointers against a root document and a context pointer. The context pointer must itself be a JSON Pointer string.

EXPORTS

Classes

  • RelativeJSONPointer

    Construct with new RelativeJSONPointer( path: "0/foo" ). Invalid syntax throws during construction.

    Methods:

    • expression()

      Returns the original pointer expression.

    • up()

      Returns the number of levels the pointer climbs from the context location.

    • index_change()

      Returns the array-index offset, or null when none was supplied.

    • pointer()

      Returns the JSON Pointer suffix, or null for a # key request.

    • key_request()

      Returns true for # pointers that request the key or array index at the target location.

    • target_pointer( context_pointer := "" )

      Returns the absolute JSON Pointer reached by climbing and applying any index offset, before the suffix is applied.

    • evaluate( root, context_pointer := "" )

      Evaluates the relative pointer. Normal pointers return the same array of matches as std/path/jsonpointer's query. # key requests return the key or index string directly, or null when the request names the document root.

    • get( root, context_pointer := "" )

      Alias for evaluate.

    • first( root, context_pointer := "", fallback := null )

      Returns the first matched value, or fallback when no value is found. For # key requests, fallback is returned only when the key request returns null.

    • exists( root, context_pointer := "" )

      Returns true when evaluation finds a value. For # key requests, the document root key is treated as absent.

Functions

  • valid_relative_json_pointer( text )

    Returns true when text parses as a Relative JSON Pointer.

COPYRIGHT AND LICENCE

std/data/json/schema/relative_pointer 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.