Class JsonPointer

java.lang.Object
org.forgerock.json.JsonPointer
All Implemented Interfaces:
Iterable<String>

public class JsonPointer extends Object implements Iterable<String>
Identifies a specific value within a JSON structure. Conforms with draft-pbryan-zip-json-pointer-02.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a JSON pointer, identifying the root value of a JSON structure.
    Constructs a JSON pointer from an iterable collection of reference tokens.
    Constructs a JSON pointer, identifying the specified pointer value.
    JsonPointer(String... tokens)
    Constructs a JSON pointer from an array of reference tokens.
  • Method Summary

    Modifier and Type
    Method
    Description
    child(int child)
    Returns a new JSON pointer, which identifies a specified child element of the array identified by this pointer.
    child(String child)
    Returns a new JSON pointer, which identifies a specified child member of the object identified by this pointer.
    boolean
    Compares the specified object with this pointer for equality.
    get(int index)
    Returns the reference token at the specified position.
    int
    Returns the hash code value for this pointer.
    boolean
    Returns true if this pointer identifies the root value of a JSON structure.
    Returns an iterator over the pointer's reference tokens.
    Returns the last (leaf) reference token of the JSON pointer, or null if the pointer contains no reference tokens (i.e. references document root).
    Returns a pointer to the parent of the JSON value identified by this JSON pointer, or null if the pointer has no parent JSON value (i.e. references document root).
    ptr(Iterable<String> iterable)
    Constructs a JSON pointer from an iterable collection of reference tokens.
    ptr(String pointer)
    Constructs a JSON pointer, identifying the specified pointer value.
    ptr(String... tokens)
    Constructs a JSON pointer from an array of reference tokens.
    Returns a pointer containing all but the first reference token contained in this pointer, or / if this pointer contains less than 2 reference tokens.
    Returns a pointer containing the last sz reference tokens contained in this pointer.
    int
    Returns the number of reference tokens in the pointer.
    Returns a newly allocated array of strings, containing the pointer's reference tokens.
    Returns the JSON pointer string value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • JsonPointer

      public JsonPointer()
      Constructs a JSON pointer, identifying the root value of a JSON structure.
    • JsonPointer

      public JsonPointer(String pointer)
      Constructs a JSON pointer, identifying the specified pointer value.
      Parameters:
      pointer - a string containing the JSON pointer of the value to identify.
      Throws:
      JsonException - if the pointer is malformed.
    • JsonPointer

      public JsonPointer(String... tokens)
      Constructs a JSON pointer from an array of reference tokens.
      Parameters:
      tokens - an array of string reference tokens.
    • JsonPointer

      public JsonPointer(Iterable<String> iterable)
      Constructs a JSON pointer from an iterable collection of reference tokens.
      Parameters:
      iterable - an iterable collection of reference tokens.
  • Method Details

    • ptr

      public static JsonPointer ptr(String pointer)
      Constructs a JSON pointer, identifying the specified pointer value.
      Parameters:
      pointer - a string containing the JSON pointer of the value to identify.
      Returns:
      The new JSON pointer
      Throws:
      JsonException - if the pointer is malformed.
    • ptr

      public static JsonPointer ptr(String... tokens)
      Constructs a JSON pointer from an array of reference tokens.
      Parameters:
      tokens - an array of string reference tokens.
      Returns:
      The new json pointer
    • ptr

      public static JsonPointer ptr(Iterable<String> iterable)
      Constructs a JSON pointer from an iterable collection of reference tokens.
      Parameters:
      iterable - an iterable collection of reference tokens.
      Returns:
      The new json pointer
    • size

      public int size()
      Returns the number of reference tokens in the pointer.
      Returns:
      the number of reference tokens in the pointer.
    • get

      public String get(int index)
      Returns the reference token at the specified position.
      Parameters:
      index - the index of the reference token to return.
      Returns:
      the reference token at the specified position.
      Throws:
      IndexOutOfBoundsException - if the index is out of range.
    • toArray

      public String[] toArray()
      Returns a newly allocated array of strings, containing the pointer's reference tokens. No references to the array are maintained by the pointer. Hence, the caller is free to modify it.
      Returns:
      a newly allocated array of strings, containing the pointer's reference tokens.
    • parent

      public JsonPointer parent()
      Returns a pointer to the parent of the JSON value identified by this JSON pointer, or null if the pointer has no parent JSON value (i.e. references document root).
      Returns:
      a pointer to the parent of of this JSON pointer. Can be null.
    • relativePointer

      Returns a pointer containing all but the first reference token contained in this pointer, or / if this pointer contains less than 2 reference tokens.

      This method yields the following results:

      Input Output
      / /
      /a /
      /a/b /b
      /a/b/c /b/c
      Returns:
      A pointer containing all but the first reference token contained in this pointer.
    • relativePointer

      public JsonPointer relativePointer(int sz)
      Returns a pointer containing the last sz reference tokens contained in this pointer.

      This method yields the following results:

      Input sz Output
      /a/b/c 0 /
      /a/b/c 1 /c
      /a/b/c 2 /b/c
      /a/b/c 3 /a/b/c
      Parameters:
      sz - The number of trailing reference tokens to retain.
      Returns:
      A pointer containing the last sz reference tokens contained in this pointer.
      Throws:
      IndexOutOfBoundsException - If sz is negative or greater than size().
    • leaf

      public String leaf()
      Returns the last (leaf) reference token of the JSON pointer, or null if the pointer contains no reference tokens (i.e. references document root).
      Returns:
      the last (leaf) reference token of the JSON pointer if it exists, null otherwise
    • child

      public JsonPointer child(String child)
      Returns a new JSON pointer, which identifies a specified child member of the object identified by this pointer.
      Parameters:
      child - the name of the child member to identify.
      Returns:
      the child JSON pointer.
      Throws:
      NullPointerException - if child is null.
    • child

      public JsonPointer child(int child)
      Returns a new JSON pointer, which identifies a specified child element of the array identified by this pointer.
      Parameters:
      child - the index of the child element to identify.
      Returns:
      the child JSON pointer.
      Throws:
      IndexOutOfBoundsException - if child is less than zero.
    • isEmpty

      public boolean isEmpty()
      Returns true if this pointer identifies the root value of a JSON structure. More specifically, it returns true if this pointer does not contain any reference tokens (i.e. size() == 0).
      Returns:
      true if this pointer identifies the root value of a JSON structure.
    • iterator

      Returns an iterator over the pointer's reference tokens.
      Specified by:
      iterator in interface Iterable<String>
      Returns:
      an iterator over the pointer's reference tokens.
    • toString

      public String toString()
      Returns the JSON pointer string value.
      Overrides:
      toString in class Object
      Returns:
      the JSON pointer string value.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this pointer for equality. Returns true if and only if the specified object is also a JSON pointer, both pointers have the same size, and all corresponding pairs of reference tokens in the two pointers are equal.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to be compared for equality with this pointer.
      Returns:
      true if the specified object is equal to this pointer.
    • hashCode

      public int hashCode()
      Returns the hash code value for this pointer.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this pointer.