Package net.pwall.json.pointer
Class JSONPointer
- java.lang.Object
-
- net.pwall.json.pointer.JSONPointer
-
- Direct Known Subclasses:
JSONReference
public class JSONPointer extends Object
JSON Pointer.- Author:
- Peter Wall
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJSONPointer.EscapePipeline<R>
-
Field Summary
Fields Modifier and Type Field Description protected static String[]emptyArraystatic net.pwall.util.CharMappermapperACharMapperto assist with escaping JSON Pointers.static JSONPointerrootstatic net.pwall.util.CharUnmapperunmapperACharUnmapperto assist with unescaping JSON Pointers.
-
Constructor Summary
Constructors Modifier Constructor Description JSONPointer(String string)Main constructor - creates aJSONPointerusing the specified path.protectedJSONPointer(String[] tokens)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static <T> TcheckNotNull(T value)Check that a user-supplied parameter is notnull.JSONPointerchild(int index)Get a pointer to the numbered child of the currently-addressed element (must be an array).JSONPointerchild(String string)Get a pointer to the named child of the currently-addressed element.static StringencodeURI(String str)Encode a string for use in a URI.booleanequals(Object obj)Compare two pointers for equality.static StringescapeToken(String token)Escape a token using the JSON Pointer escaping rules.net.pwall.json.JSONValueeval(net.pwall.json.JSONValue base)Evaluate a JSON Pointer against a given JSON value.static booleanexists(String string, net.pwall.json.JSONValue base)Test whether a value exists at a given path (as aString).booleanexists(net.pwall.json.JSONValue base)Test whether a value exists at a given path.static net.pwall.json.JSONValuefind(String string, net.pwall.json.JSONValue base)Find the value corresponding to the pointer (as aString) in the given JSON value.net.pwall.json.JSONValuefind(net.pwall.json.JSONValue base)Find the value corresponding to the pointer in the given JSON value.static JSONPointerfromURIFragment(String fragment)Create aJSONPointerfrom a URI fragment.StringgetCurrent()Get the current token, i.e.JSONPointergetPointer()Get this pointer as a simpleJSONPointer.String[]getTokens()Get the array of tokens for this pointer.inthashCode()Get the hash code for the pointer.JSONPointerlocateChild(net.pwall.json.JSONValue value, net.pwall.json.JSONValue target)Attempt to locate the specified childJSONValuewithin a nested structure.JSONPointerparent()Get a pointer to the parent of the currently-addressed element.protected static String[]parse(String string)Parse a pointer string into an array of pointer elements.StringtoString()Get a string representation of the pointer.StringtoURIFragment()Convert the pointer to a URI fragment.static StringunescapeToken(String str)Unescape a token using the JSON Pointer escaping rules.
-
-
-
Field Detail
-
emptyArray
protected static final String[] emptyArray
-
root
public static final JSONPointer root
-
mapper
public static final net.pwall.util.CharMapper mapper
ACharMapperto assist with escaping JSON Pointers.
-
unmapper
public static final net.pwall.util.CharUnmapper unmapper
ACharUnmapperto assist with unescaping JSON Pointers.
-
-
Constructor Detail
-
JSONPointer
protected JSONPointer(String[] tokens)
- Parameters:
tokens- a list of tokens
-
JSONPointer
public JSONPointer(String string)
Main constructor - creates aJSONPointerusing the specified path.- Parameters:
string- the path- Throws:
JSONPointerException- if the string is not either an empty string, or starts with "/"
-
-
Method Detail
-
eval
public net.pwall.json.JSONValue eval(net.pwall.json.JSONValue base)
Evaluate a JSON Pointer against a given JSON value.- Parameters:
base- the base value (path elements will be used to navigate from this point)- Returns:
- the value at that point (may be
null) - Throws:
JSONPointerException- if there are any errors in navigation
-
find
public net.pwall.json.JSONValue find(net.pwall.json.JSONValue base)
Find the value corresponding to the pointer in the given JSON value.- Parameters:
base- the base value (path elements will be used to navigate from this point)- Returns:
- the value at that point (may be
null) - Throws:
JSONPointerException- if there are any errors in navigation
-
exists
public boolean exists(net.pwall.json.JSONValue base)
Test whether a value exists at a given path.- Parameters:
base- the base value (path elements will be used to navigate from this point)- Returns:
trueif the value exists (including if it isnull)
-
parent
public JSONPointer parent()
Get a pointer to the parent of the currently-addressed element.- Returns:
- a pointer to the parent element
- Throws:
JSONPointerException- on any attempt to get the parent of the root element
-
child
public JSONPointer child(String string)
Get a pointer to the named child of the currently-addressed element.- Parameters:
string- the name of the child- Returns:
- a pointer to the child element
-
child
public JSONPointer child(int index)
Get a pointer to the numbered child of the currently-addressed element (must be an array).- Parameters:
index- the index of the child- Returns:
- a pointer to the child element
- Throws:
JSONPointerException- if the index is negative
-
getPointer
public JSONPointer getPointer()
Get this pointer as a simpleJSONPointer. Classes that derive from this class (notablyJSONReferencemay override this function to return a simpleJSONPointer.- Returns:
- this pointer
-
getCurrent
public String getCurrent()
Get the current token, i.e. the property name or index that this pointer points to at the lowest level. Returnsnullin the case of the root pointer.- Returns:
- the property name or array item index, or
nullif this is the root pointer
-
toURIFragment
public String toURIFragment()
Convert the pointer to a URI fragment.- Returns:
- the URI fragment
-
getTokens
public String[] getTokens()
Get the array of tokens for this pointer.- Returns:
- the array of tokens
-
equals
public boolean equals(Object obj)
Compare two pointers for equality.
-
hashCode
public int hashCode()
Get the hash code for the pointer.
-
toString
public String toString()
Get a string representation of the pointer.
-
find
public static net.pwall.json.JSONValue find(String string, net.pwall.json.JSONValue base)
Find the value corresponding to the pointer (as aString) in the given JSON value.- Parameters:
string- the pointer as a stringbase- the base value (path elements will be used to navigate from this point)- Returns:
- the value at that point (may be
null) - Throws:
JSONPointerException- if there are any errors in navigation
-
exists
public static boolean exists(String string, net.pwall.json.JSONValue base)
Test whether a value exists at a given path (as aString).- Parameters:
string- the pointer as a stringbase- the base value (path elements will be used to navigate from this point)- Returns:
trueif the value exists (including if it isnull)
-
checkNotNull
protected static <T> T checkNotNull(T value)
Check that a user-supplied parameter is notnull.- Type Parameters:
T- the type of the parameter- Parameters:
value- the parameter- Returns:
- the value, if it is not
null - Throws:
NullPointerException- if the value isnull
-
parse
protected static String[] parse(String string)
Parse a pointer string into an array of pointer elements.- Parameters:
string- the original string- Returns:
- the array of elements
- Throws:
JSONPointerException- if the string is not either an empty string, or starts with "/"
-
locateChild
public JSONPointer locateChild(net.pwall.json.JSONValue value, net.pwall.json.JSONValue target)
Attempt to locate the specified childJSONValuewithin a nested structure. The function will return aJSONPointerto the target, ornullif the target can not be located. Note that this will perform a depth-first search of the entire structure, comparing on object identity, not equality.- Parameters:
value- the baseJSONValuewithin which to searchtarget- the target of the search- Returns:
- a
JSONPointerto locate the target in the base value
-
encodeURI
public static String encodeURI(String str)
Encode a string for use in a URI. This conversion uses "%20" to encode spaces rather than "+", because that is the form used in the JSON Pointer specification.- Parameters:
str- the input string- Returns:
- the encoded string
-
fromURIFragment
public static JSONPointer fromURIFragment(String fragment)
Create aJSONPointerfrom a URI fragment.- Parameters:
fragment- the URI fragment- Returns:
- the pointer
- Throws:
JSONPointerException- if the fragment is illegal (does not start with "#/")
-
escapeToken
public static String escapeToken(String token)
Escape a token using the JSON Pointer escaping rules.- Parameters:
token- the token- Returns:
- the escaped token
-
-