public class JSNode
extends java.lang.Object
implements java.util.Map<java.lang.String,java.lang.Object>
Inversion encourages working with JSON data structures abstractly instead of transcoding them into a concrete Java object model. So JSNode and JSArray were designed to make it as easy as possible to work with JSON in its "native form."
JSNode and JSArray are a one stop shop for:
Property name case is preserved but considered case insensitive when accessing a property by name.
Property iteration or is preserved based on insertion order.
It is possible to create a document with a child JSNode appearing multiple times in the document including circular reference loops. When printing a document, if a JSNode has previously been printed AND it has an 'href' property, instead of erroring, the printer will write an '@link' property pointing to the previously printed href. If the JSNode does not have an 'href' an error will be thrown.
Under the covers this Jackson is used as the json parser.
JSArray,
- https://github.com/json-path/JsonPath,
- https://tools.ietf.org/html/rfc6901,
- https://github.com/flipkart-incubator/zjsonpatch| Constructor and Description |
|---|
JSNode()
Creates an empty JSNode.
|
JSNode(java.util.Map nameValuePairs)
Creates a JSNode with
nameValuePairs as the initial properties. |
JSNode(java.lang.Object... nameValuePairs)
Creates a JSNode with
nameValuePairs as the initial properties. |
| Modifier and Type | Method and Description |
|---|---|
JSArray |
asArray()
Similar to #asList() but instead of returning a List, it returns a this JSNode
as the only item in a JSArray.
|
java.util.List |
asList()
Returns this object as the only element in a list.
|
java.util.Map |
asMap() |
java.util.List<JSNode> |
asNodeList()
Returns this object as the only element in a List
|
void |
clear()
Removes all properties
|
boolean |
containsKey(java.lang.Object name) |
boolean |
containsValue(java.lang.Object value)
Checks all property values for equality to
value |
JSNode |
copy()
Makes a deep copy of this JSNode by stringifying/parsing
|
JSArray |
diff(JSNode source) |
java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> |
entrySet() |
java.lang.Object |
find(java.lang.String pathExpression)
Convenience overloading of
findAll(String, int) that returns the first item found |
JSArray |
findAll(java.lang.String pathExpression)
Convenience overloading of
findAll(String, int) |
JSArray |
findAll(java.lang.String pathExpression,
int qty)
A heroically permissive finder supporting JSON Pointer, JSONPath and
a simple 'dot and wildcard' type of system like so:
'propName.childPropName.*.skippedGenerationPropsName.4.fifthArrayNodeChildPropsName.**.recursivelyFoundPropsName'.
|
java.util.List<JSNode> |
findAllNodes(java.lang.String pathExpression)
Convenience overloading of
findAll(String, int) |
JSArray |
findArray(java.lang.String pathExpression)
Convenience overloading of
find(String) |
boolean |
findBoolean(java.lang.String pathExpression)
Convenience overloading of
find(String) |
double |
findDouble(java.lang.String pathExpression)
Convenience overloading of
find(String) |
int |
findInt(java.lang.String pathExpression)
Convenience overloading of
find(String) |
JSNode |
findNode(java.lang.String pathExpression)
Convenience overloading of
find(String) |
java.lang.String |
findString(java.lang.String pathExpression)
Convenience overloading of
find(String) |
java.lang.Object |
get(java.lang.Object name) |
JSArray |
getArray(java.lang.String name)
Convenience overloading of
get(Object) |
boolean |
getBoolean(java.lang.String name)
Convenience overloading of
get(Object) |
double |
getDouble(java.lang.String name)
Convenience overloading of
get(Object) |
int |
getInt(java.lang.String name)
Convenience overloading of
get(Object) |
JSNode |
getNode(java.lang.String name)
Convenience overloading of
get(Object) |
java.lang.String |
getString(java.lang.String name)
Convenience overloading of
get(Object) |
boolean |
hasProperty(java.lang.String name) |
boolean |
isArray()
Easy alternative to 'instanceof' to differentiate JSNode from JSArray (which subclasses JSNode).
|
boolean |
isEmpty() |
java.util.Set<java.lang.String> |
keySet() |
static java.lang.Object |
parseJson(java.lang.String json)
Turns a JSON string in to JSNode (maps), JSArray (lists), String numbers and booleans.
|
static JSArray |
parseJsonArray(java.lang.String json)
Utility overloading of
parseJson(String) to cast the return as a JSArray |
static JSNode |
parseJsonNode(java.lang.String json)
Utility overloading of
parseJson(String) to cast the return as a JSNode |
JSArray |
patch(JSArray patches) |
java.lang.Object |
put(java.lang.String name,
java.lang.Object value) |
void |
putAll(java.util.Map map) |
java.lang.Object |
putFirst(java.lang.String name,
java.lang.Object value)
Vanity method to make sure the attributes prints out first
|
java.lang.Object |
remove(java.lang.Object name) |
java.lang.Object |
remove(java.lang.String... names)
Removes all properties with
names. |
int |
size() |
void |
sortKeys()
Changes the property name iteration order from insertion order to alphabetic order.
|
java.lang.String |
toString()
Pretty prints the JSNode with properties written out in their original case.
|
java.lang.String |
toString(boolean pretty)
Prints the JSNode with properties written out in their original case.
|
java.lang.String |
toString(boolean pretty,
boolean lowercasePropertyNames)
Prints the JSNode
|
java.util.Collection |
values() |
JSNode |
with(java.lang.Object... nvPairs) |
public JSNode()
public JSNode(java.lang.Object... nameValuePairs)
nameValuePairs as the initial properties.
The first and every other element in nameValuePairs should be a string.
nameValuePairs - with(Object...)public JSNode(java.util.Map nameValuePairs)
nameValuePairs as the initial properties.map - putAll(Map)public JSArray findAll(java.lang.String pathExpression, int qty)
All forms are internally converted into a 'master' form before processing. This master simply uses '.' to separate property names and array indexes and uses uses '*' to represent a single level wildcard and '**' to represent a recursive wildcard. For example:
Arrays indexes are treated just like property names but with integer names. For example "myObject.4.nextProperty" finds "nextProperty" on the 5th element in the "myObject" array.
JSON Pointer is the least expressive supported form and uses '/' characters to separate properties. To support JSON Pointer, we simply replace all '/' characters for "." characters before processing.
JSON Path is more like XML XPath but uses '.' instead of '/' to separate properties. Technically JSON Path statements are supposed to start with '$.' but that is optional here. The best part about JSON Path is the query filters that let you conditionally select elements.
Below is the implementation status of various JSON Path features:
The JSON Path following boolean comparison operators are supported:
JsonPath bracket-notation such as "$['store']['book'][0]['title']" is currently not supported.
Pointer - https://tools.ietf.org/html/rfc6901,
Path - https://goessner.net/articles/JsonPath/,
Path - https://github.com/json-path/JsonPathpublic java.lang.Object get(java.lang.Object name)
get in interface java.util.Map<java.lang.String,java.lang.Object>public JSNode getNode(java.lang.String name) throws java.lang.ClassCastException
get(Object)name - name cast to a JSNode if exists else nulljava.lang.ClassCastException - if the object found is not a JSNodeget(Object)public JSArray getArray(java.lang.String name)
get(Object)name - name cast to a JSArray if exists else nulljava.lang.ClassCastException - if the object found is not a JSArrayget(Object)public java.lang.String getString(java.lang.String name)
get(Object)name - name if it exists else nullget(Object)public int getInt(java.lang.String name)
get(Object)name - name stringified and parsed as an int if it exists else -1get(Object)public double getDouble(java.lang.String name)
get(Object)name - name stringified and parsed as a double if it exists else -1get(Object)public boolean getBoolean(java.lang.String name)
get(Object)name - name stringified and parsed as a boolean if it exists else falseget(Object)public JSNode findNode(java.lang.String pathExpression)
find(String)name - pathExpression cast as a JSNode if exists else nulljava.lang.ClassCastException - if the object found is not a JSNodefind(String)public JSArray findArray(java.lang.String pathExpression)
find(String)name - pathExpression cast as a JSArray if exists else nulljava.lang.ClassCastException - if the object found is not a JSArrayfind(String)public java.lang.String findString(java.lang.String pathExpression)
find(String)pathExpression - pathExpression stringified if exists else nullfind(String)public int findInt(java.lang.String pathExpression)
find(String)pathExpression - pathExpression stringified and parsed as an int if exists else -1find(String)public double findDouble(java.lang.String pathExpression)
find(String)pathExpression - pathExpression stringified and parsed as a double if exists else -1find(String)public boolean findBoolean(java.lang.String pathExpression)
find(String)pathExpression - pathExpression stringified and parsed as a boolean if exists else falsefind(String)public java.lang.Object find(java.lang.String pathExpression)
findAll(String, int) that returns the first item foundpathExpressionfindAll(String, int)public JSArray findAll(java.lang.String pathExpression)
findAll(String, int)pathExpression - pathExpressionfindAll(String, int)public java.util.List<JSNode> findAllNodes(java.lang.String pathExpression)
findAll(String, int)pathExpression - pathExpression cast as a ListfindAll(String, int)public java.lang.Object put(java.lang.String name,
java.lang.Object value)
put in interface java.util.Map<java.lang.String,java.lang.Object>public void putAll(java.util.Map map)
putAll in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object putFirst(java.lang.String name,
java.lang.Object value)
name - value - public JSNode with(java.lang.Object... nvPairs)
public boolean containsKey(java.lang.Object name)
containsKey in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object remove(java.lang.Object name)
remove in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object remove(java.lang.String... names)
names.names - the keys to removenamespublic java.util.Set<java.lang.String> keySet()
keySet in interface java.util.Map<java.lang.String,java.lang.Object>public boolean hasProperty(java.lang.String name)
public java.util.Map asMap()
public JSNode copy()
public void sortKeys()
public boolean isArray()
JSArray.isArray()public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toString(boolean pretty)
pretty - should spaces and carriage returns be added to the doc for readabilitypublic java.lang.String toString(boolean pretty,
boolean lowercasePropertyNames)
pretty - should spaces and carriage returns be added to the doc for readabilitypublic int size()
size in interface java.util.Map<java.lang.String,java.lang.Object>public boolean isEmpty()
isEmpty in interface java.util.Map<java.lang.String,java.lang.Object>size() == 0public boolean containsValue(java.lang.Object value)
valuecontainsValue in interface java.util.Map<java.lang.String,java.lang.Object>valuepublic void clear()
clear in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.Collection values()
values in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
entrySet in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.List asList()
JSArray overrides this method to return all of its elements in a list.
This method is designed to make it super easy to iterate over all property values or array elements without having to cast or consider differences between JSNode and JSArray.
For example:
JSNode node = response.getJson();//don't know if this is a JSNode or JSArray
for(Object value : node.asList())
{
//do something;
}
JSArray.asList(),
asNodeList()public java.util.List<JSNode> asNodeList()
JSArray overrides this method to return all of its elements in a list.
This method is designed to make it super easy to iterate over all property values or array elements without having to cast or consider differences between JSNode and JSArray.
For example:
JSNode node = response.getJson();//don't know if this is a JSNode or JSArray
for(JSNode child : node.asList())
{
System.out.println("found items with price: " + child.find("**.item.price"));
}
asList()public JSArray asArray()
JSArray overrides this method to simply return 'this'.
asList(),
asNodeList(),
JSArray.asArray()public static java.lang.Object parseJson(java.lang.String json)
Jackson is the underlying parser
json - the json string to parsepublic static JSNode parseJsonNode(java.lang.String json) throws java.lang.ClassCastException
parseJson(String) to cast the return as a JSNodejson - java.lang.ClassCastException - if the result of parsing is not a JSNodepublic static JSArray parseJsonArray(java.lang.String json)
parseJson(String) to cast the return as a JSArrayjson - java.lang.ClassCastException - if the result of parsing is not a JSArrayCopyright © 2024 Rocket Partners, LLC. All rights reserved.