Package net.pwall.json.pointer
Class JSONReference
- java.lang.Object
-
- net.pwall.json.pointer.JSONPointer
-
- net.pwall.json.pointer.JSONReference
-
public class JSONReference extends JSONPointer
A JSON Reference - a combination of a JSON Pointer and the JSON value to which it refers. This allows for a single object to be used (and passed as a parameter between functions) in the common case of a pointer being employed to navigate a tree of JSON values. The class is implemented as a derived class fromJSONPointer- this is an optimisation to reduce object allocation.- Author:
- Peter Wall
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class net.pwall.json.pointer.JSONPointer
JSONPointer.EscapePipeline<R>
-
-
Field Summary
-
Fields inherited from class net.pwall.json.pointer.JSONPointer
emptyArray, mapper, root, unmapper
-
-
Constructor Summary
Constructors Constructor Description JSONReference(net.pwall.json.JSONValue base)Create a JSON Reference using the given base JSON value and a root pointer.JSONReference(net.pwall.json.JSONValue base, String string)Create a JSON Reference using the given base JSON value and a pointer in string form.JSONReference(net.pwall.json.JSONValue base, JSONPointer pointer)Create a JSON Reference using the given base JSON value and pointer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONReferencechild(int index)Navigate to the numbered child of the currently-addressed element (must be an array).JSONReferencechild(String name)Navigate to the named child of the currently-addressed element.booleanequals(Object other)Compare two JSON references for equality.net.pwall.json.JSONValuegetBase()Get the baseJSONValue.JSONPointergetPointer()Get the pointer component of the reference.net.pwall.json.JSONValuegetValue()Get the value referred to by this reference.booleanhasChild(int index)Test whether the reference has the nominated child (by index).booleanhasChild(String name)Test whether the reference has the nominated child (by name).inthashCode()Get the hash code for the reference.booleanisValid()Test whether the reference is valid, i.e.JSONReferencelocateChild(net.pwall.json.JSONValue target)Attempt to locate the specified childJSONValuewithin a nested structure.JSONReferenceparent()Navigate to the parent of the currently-addressed element.StringtoString()Get a string representation of the reference.-
Methods inherited from class net.pwall.json.pointer.JSONPointer
checkNotNull, encodeURI, escapeToken, eval, exists, exists, find, find, fromURIFragment, getCurrent, getTokens, locateChild, parse, toURIFragment, unescapeToken
-
-
-
-
Constructor Detail
-
JSONReference
public JSONReference(net.pwall.json.JSONValue base, JSONPointer pointer)Create a JSON Reference using the given base JSON value and pointer.- Parameters:
base- the baseJSONValuepointer- aJSONPointerto a node within the base value
-
JSONReference
public JSONReference(net.pwall.json.JSONValue base, String string)Create a JSON Reference using the given base JSON value and a pointer in string form.- Parameters:
base- the baseJSONValuestring- a string representing a JSON Pointer- Throws:
JSONPointerException- if the string is not either an empty string, or starts with "/"
-
JSONReference
public JSONReference(net.pwall.json.JSONValue base)
Create a JSON Reference using the given base JSON value and a root pointer.- Parameters:
base- the baseJSONValue
-
-
Method Detail
-
getPointer
public JSONPointer getPointer()
Get the pointer component of the reference. Note that since this class derives fromJSONPointer, then provided the virtual functions (e.g.toString()) are not required, the object may simply be cast to aJSONPointer.- Overrides:
getPointerin classJSONPointer- Returns:
- the pointer
-
getBase
public net.pwall.json.JSONValue getBase()
Get the baseJSONValue.- Returns:
- the
JSONValue
-
isValid
public boolean isValid()
Test whether the reference is valid, i.e. the pointer points to a valid location within the base.- Returns:
trueiff the reference is valid
-
getValue
public net.pwall.json.JSONValue getValue()
Get the value referred to by this reference.- Returns:
- the value, or
nullif the reference is not valid
-
hasChild
public boolean hasChild(String name)
Test whether the reference has the nominated child (by name).- Parameters:
name- the name of the child- Returns:
trueiff that child exists
-
hasChild
public boolean hasChild(int index)
Test whether the reference has the nominated child (by index).- Parameters:
index- the index of the child- Returns:
trueiff that child exists
-
parent
public JSONReference parent()
Navigate to the parent of the currently-addressed element.- Overrides:
parentin classJSONPointer- Returns:
- a reference that refers to the parent element
- Throws:
JSONPointerException- on any attempt to get the parent of the root element
-
child
public JSONReference child(String name)
Navigate to the named child of the currently-addressed element.- Overrides:
childin classJSONPointer- Parameters:
name- the name of the child- Returns:
- a reference that refers to the child element (possibly not valid)
-
child
public JSONReference child(int index)
Navigate to the numbered child of the currently-addressed element (must be an array).- Overrides:
childin classJSONPointer- Parameters:
index- the index of the child- Returns:
- a reference that refers to the child element (possibly not valid)
- Throws:
JSONPointerException- if the index is negative
-
locateChild
public JSONReference locateChild(net.pwall.json.JSONValue target)
Attempt to locate the specified childJSONValuewithin a nested structure. The function will return aJSONReferenceto 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:
target- the target of the search- Returns:
- a
JSONReferencereferring to the base value and the target
-
equals
public boolean equals(Object other)
Compare two JSON references for equality. The references are equal only if the pointers are equal and the base values refer to the same object.- Overrides:
equalsin classJSONPointer- Parameters:
other- the other object for comparison- Returns:
trueiff the objects are equal
-
hashCode
public int hashCode()
Get the hash code for the reference.- Overrides:
hashCodein classJSONPointer- Returns:
- the hash code
-
toString
public String toString()
Get a string representation of the reference.- Overrides:
toStringin classJSONPointer- Returns:
- a string representation of the value (or "invalid" if the reference is not valid)
-
-