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,
JSONPath,
JSONPointer,
JSONPatch| 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<? extends java.lang.String,? extends java.lang.Object> |
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) |
long |
findLong(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) |
long |
getLong(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 |
void |
patch(JSArray patches) |
java.lang.Object |
put(java.lang.String name,
java.lang.Object value) |
void |
putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> 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 |
removeAll(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.util.stream.Stream |
stream()
Convenience method that calls asList().stream().
|
java.util.stream.Stream |
streamAll() |
java.lang.String |
toString() |
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 - the name value pairs to addwith(Object...)public JSNode(java.util.Map nameValuePairs)
nameValuePairs as the initial properties.nameValuePairs - the name value pairs to addputAll(Map)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 - the json string to parsejava.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 - a json string containing an as the root elementjava.lang.ClassCastException - if the result of parsing is not a JSArraypublic 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.
pathExpression - defines the properties to findqty - the maximum number of resultspublic void patch(JSArray patches)
public 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 - the case insensitive property name to retrieve.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 - the case insensitive property name to retrieve.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 - the case insensitive property name to retrieve.name if it exists else nullget(Object)public int getInt(java.lang.String name)
get(Object)name - the case insensitive property name to retrieve.name stringified and parsed as an int if it exists else -1get(Object)public long getLong(java.lang.String name)
get(Object)name - the case insensitive property name to retrieve.name stringified and parsed as long if it exists else -1get(Object)public double getDouble(java.lang.String name)
get(Object)name - the case insensitive property name to retrieve.name stringified and parsed as a double if it exists else -1get(Object)public boolean getBoolean(java.lang.String name)
get(Object)name - the case insensitive property name to retrieve.name stringified and parsed as a boolean if it exists else falseget(Object)public JSNode findNode(java.lang.String pathExpression)
find(String)pathExpression - specifies the nodes to findpathExpression 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)pathExpression - specifies the properties to findpathExpression 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 - specifies the properties to findpathExpression stringified if exists else nullfind(String)public int findInt(java.lang.String pathExpression)
find(String)pathExpression - specifies the properties to findpathExpression stringified and parsed as an int if exists else -1find(String)public long findLong(java.lang.String pathExpression)
find(String)pathExpression - specifies the properties to findpathExpression stringified and parsed as a long if exists else -1find(String)public double findDouble(java.lang.String pathExpression)
find(String)pathExpression - specifies the properties to findpathExpression stringified and parsed as a double if exists else -1find(String)public boolean findBoolean(java.lang.String pathExpression)
find(String)pathExpression - specifies the properties to findpathExpression 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 foundpathExpression - specifies the properties to findpathExpressionfindAll(String, int)public JSArray findAll(java.lang.String pathExpression)
findAll(String, int)pathExpression - specifies the properties to findpathExpressionfindAll(String, int)public java.util.List<JSNode> findAllNodes(java.lang.String pathExpression)
findAll(String, int)pathExpression - specifies the properties to findpathExpression 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<? extends java.lang.String,? extends java.lang.Object> 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 - the property namevalue - the property valuepublic 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 removeAll(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<? extends java.lang.String,? extends java.lang.Object> 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 readabilitylowercasePropertyNames - when true all property names are printed in lower case instead of their original casepublic 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 java.util.stream.Stream stream()
public java.util.stream.Stream streamAll()
Copyright © 2021 Rocket Partners, LLC. All rights reserved.