Options
All
  • Public
  • Public/Protected
  • All
Menu

Represents a JSON Pointer, capable of getting and setting the value on target objects at the pointer's location.

While there are static variants for most operations, our recommendation is to use the instance level methods, which enables you avoid repeated compiling/emitting transient accessors. Take a look at the speed comparisons for our justification.

In most cases, you should create and reuse instances of JsonPointer within scope that makes sense for your app. We often create constants for frequently used pointers, but your use case may vary.

The following is a contrived example showing a function that uses pointers to deal with changes in the structure of data (a version independent function):

import { JsonPointer } from 'json-ptr';

export type SupportedVersion = '1.0' | '1.1';

interface PrimaryGuestNamePointers {
name: JsonPointer;
surname: JsonPointer;
honorific: JsonPointer;
}
const versions: Record<SupportedVersion, PrimaryGuestNamePointers> = {
'1.0': {
name: JsonPointer.create('/guests/0/name'),
surname: JsonPointer.create('/guests/0/surname'),
honorific: JsonPointer.create('/guests/0/honorific'),
},
'1.1': {
name: JsonPointer.create('/primary/primaryGuest/name'),
surname: JsonPointer.create('/primary/primaryGuest/surname'),
honorific: JsonPointer.create('/primary/primaryGuest/honorific'),
}
};

interface Reservation extends Record<string, unknown> {
version?: SupportedVersion;
}

function primaryGuestName(reservation: Reservation): string {
const pointers = versions[reservation.version || '1.0'];
const name = pointers.name.get(reservation) as string;
const surname = pointers.surname.get(reservation) as string;
const honorific = pointers.honorific.get(reservation) as string;
const names: string[] = [];
if (honorific) names.push(honorific);
if (name) names.push(name);
if (surname) names.push(surname);
return names.join(' ');
}

// The original layout of a reservation (only the parts relevant to our example)
const reservationV1: Reservation = {
guests: [{
name: 'Wilbur',
surname: 'Finkle',
honorific: 'Mr.'
}, {
name: 'Wanda',
surname: 'Finkle',
honorific: 'Mrs.'
}, {
name: 'Wilma',
surname: 'Finkle',
honorific: 'Miss',
child: true,
age: 12
}]
// ...
};

// The new layout of a reservation (only the parts relevant to our example)
const reservationV1_1: Reservation = {
version: '1.1',
primary: {
primaryGuest: {
name: 'Wilbur',
surname: 'Finkle',
honorific: 'Mr.'
},
additionalGuests: [{
name: 'Wanda',
surname: 'Finkle',
honorific: 'Mrs.'
}, {
name: 'Wilma',
surname: 'Finkle',
honorific: 'Miss',
child: true,
age: 12
}]
// ...
}
// ...
};

console.log(primaryGuestName(reservationV1));
console.log(primaryGuestName(reservationV1_1));

There are many uses for pointers.

Hierarchy

  • JsonPointer

Index

Constructors

  • Creates a new instance.

    Parameters

    • ptr: string | PathSegments

      a string representation of a JSON Pointer, or a decoded array of path segments.

    Returns JsonPointer

Properties

The pointer's decoded path segments.

Accessors

  • get pointer(): string
  • This pointer's JSON Pointer encoded string representation.

    Returns string

  • get uriFragmentIdentifier(): string
  • This pointer's URI fragment identifier encoded string representation.

    Returns string

Methods

  • Creates a new instance by concatenating the specified pointer's path onto this pointer's path.

    Parameters

    • ptr: string | PathSegments | JsonPointer

      the string representation of a pointer, it's decoded path, or an instance of JsonPointer indicating the additional path to concatenate onto the pointer.

    Returns JsonPointer

  • get(target: unknown): unknown
  • Gets the target object's value at the pointer's location.

    Parameters

    • target: unknown

      the target of the operation

    Returns unknown

  • has(target: unknown): boolean
  • Determines if the specified target's object graph has a value at the pointer's location.

    Parameters

    • target: unknown

      the target of the operation

    Returns boolean

  • parent(target: unknown): unknown
  • Gets the value in the object graph that is the parent of the pointer location.

    Parameters

    • target: unknown

      the target of the operation

    Returns unknown

  • rel(target: unknown, ptr: string): unknown
  • Resolves the specified relative pointer path against the specified target object, and gets the target object's value at the relative pointer's location.

    Parameters

    • target: unknown

      the target of the operation

    • ptr: string

      the relative pointer (relative to this)

    Returns unknown

    the value at the relative pointer's resolved path; otherwise undefined.

  • Creates a new JsonPointer instance, pointing to the specified relative location in the object graph.

    Parameters

    • ptr: string

      the relative pointer (relative to this)

    Returns JsonPointer

    A new instance that points to the relative location.

  • set(target: unknown, value: unknown, force?: boolean): unknown
  • Sets the target object's value, as specified, at the pointer's location.

    If any part of the pointer's path does not exist, the operation aborts without modification, unless the caller indicates that pointer's location should be created.

    Parameters

    • target: unknown

      the target of the operation

    • value: unknown

      the value to set

    • force: boolean = false

      indicates whether the pointer's location should be created if it doesn't already exist.

    Returns unknown

  • toString(): string
  • Emits the JSON Pointer encoded string representation.

    Returns string

  • unset(target: unknown): unknown
  • Removes the target object's value at the pointer's location.

    Parameters

    • target: unknown

      the target of the operation

    Returns unknown

    the value that was removed from the object graph.

  • Factory function that creates a JsonPointer instance.

    const ptr = JsonPointer.create('/deeply/nested/data/0/here');
    

    or

    const ptr = JsonPointer.create(['deeply', 'nested', 'data', 0, 'here']);
    

    Parameters

    Returns JsonPointer

  • Decodes the specified pointer into path segments.

    Parameters

    • pointer: string

      a string representation of a JSON Pointer

    Returns PathSegments

  • flatten(target: unknown, fragmentId?: boolean): Record<string, unknown>
  • Evaluates the target's object graph, returning a Record<Pointer, unknown> populated with pointers and the corresponding values from the graph.

    Parameters

    • target: unknown

      the target of the operation

    • fragmentId: boolean = false

      indicates whether the results are populated with fragment identifiers rather than regular pointers

    Returns Record<string, unknown>

  • Gets the target object's value at the pointer's location.

    const target = {
    first: 'second',
    third: ['fourth', 'fifth', { sixth: 'seventh' }],
    eighth: 'ninth'
    };

    console.log(JsonPointer.get(target, '/third/2/sixth'));
    // seventh
    console.log(JsonPointer.get(target, '/tenth'));
    // undefined

    Parameters

    Returns unknown

  • Determines if the specified target's object graph has a value at the pointer's location.

    const target = {
    first: 'second',
    third: ['fourth', 'fifth', { sixth: 'seventh' }],
    eighth: 'ninth'
    };

    console.log(JsonPointer.has(target, '/third/0'));
    // true
    console.log(JsonPointer.has(target, '/tenth'));
    // false

    Parameters

    Returns boolean

  • map(target: unknown, fragmentId?: boolean): Map<string, unknown>
  • Evaluates the target's object graph, returning a Map<Pointer,unknown> populated with pointers and the corresponding values form the graph.

    Parameters

    • target: unknown

      the target of the operation

    • fragmentId: boolean = false

      indicates whether the results are populated with fragment identifiers rather than regular pointers

    Returns Map<string, unknown>

  • Sets the target object's value, as specified, at the pointer's location.

    const target = {
    first: 'second',
    third: ['fourth', 'fifth', { sixth: 'seventh' }],
    eighth: 'ninth'
    };

    console.log(JsonPointer.set(target, '/third/2/sixth', 'tenth'));
    // seventh
    console.log(JsonPointer.set(target, '/tenth', 'eleventh', true));
    // undefined
    console.log(JSON.stringify(target, null, ' '));
    // {
    // "first": "second",
    // "third": [
    // "fourth",
    // "fifth",
    // {
    // "sixth": "tenth"
    // }
    // ],
    // "eighth": "ninth",
    // "tenth": "eleventh"
    // }

    Parameters

    • target: unknown

      the target of the operation

    • pointer: string | PathSegments | JsonPointer

      the pointer or path

    • val: unknown

      a value to write into the object graph at the specified pointer location

    • force: boolean = false

      indications whether the operation should force the pointer's location into existence in the object graph.

    Returns unknown

    the prior value at the pointer's location in the object graph.

  • Removes the target object's value at the pointer's location.

    const target = {
    first: 'second',
    third: ['fourth', 'fifth', { sixth: 'seventh' }],
    eighth: 'ninth'
    };

    console.log(JsonPointer.unset(target, '/third/2/sixth'));
    // seventh
    console.log(JsonPointer.unset(target, '/tenth'));
    // undefined
    console.log(JSON.stringify(target, null, ' '));
    // {
    // "first": "second",
    // "third": [
    // "fourth",
    // "fifth",
    // {}
    // ],
    // "eighth": "ninth",
    // }

    Parameters

    Returns unknown

    the value that was removed from the object graph.

  • visit(target: unknown, visitor: Visitor, fragmentId?: boolean): void
  • Evaluates the target's object graph, calling the specified visitor for every unique pointer location discovered while walking the graph.

    Parameters

    • target: unknown

      the target of the operation

    • visitor: Visitor

      a callback function invoked for each unique pointer location in the object graph

    • fragmentId: boolean = false

      indicates whether the visitor should receive fragment identifiers or regular pointers

    Returns void

Generated using TypeDoc