public class JSONDocument extends java.lang.Object implements java.lang.Iterable<JSONDocument>
Modifier and Type | Class and Description |
---|---|
(package private) static class |
JSONDocument.IteratorWrapper |
static class |
JSONDocument.Type
The type of a JSON node.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.lang.Object> |
array |
static JSONDocument |
EMPTY_ARRAY
An empty array.
|
static JSONDocument |
EMPTY_OBJECT
An empty object.
|
static java.lang.String |
JSON_XPATH_URI
URI for use in
javax.xml.xpath.XPathFactory to specify
a subset of javax.xml.xpath.XPath over JSON. |
private java.util.Map<java.lang.String,java.lang.Object> |
object |
private JSONDocument.Type |
type |
Modifier | Constructor and Description |
---|---|
|
JSONDocument(JSONDocument.Type type)
Constructs an empty node of the specified type.
|
private |
JSONDocument(JSONDocument.Type type,
int length) |
Modifier and Type | Method and Description |
---|---|
java.util.List<java.lang.Object> |
array()
Returns the array representation of the node.
|
static JSONDocument |
createArray() |
static JSONDocument |
createArray(int length) |
static JSONDocument |
createObject() |
boolean |
equals(java.lang.Object obj) |
JSONDocument |
get(int index)
Get child of this array node.
|
private java.lang.Object |
get(int index,
java.lang.Class<?> type) |
JSONDocument |
get(java.lang.String key)
Get child of this object node.
|
private java.lang.Object |
get(java.lang.String key,
java.lang.Class<?> type) |
java.lang.Boolean |
getBoolean(int index)
Get the array node's value at specified index as a Boolean.
|
java.lang.Boolean |
getBoolean(java.lang.String key)
Get the object node's named value as a Boolean.
|
java.util.List<java.lang.Object> |
getList(int index)
Get child array of this array node as a List.
|
private java.util.List<java.lang.Object> |
getList(JSONDocument doc) |
java.util.List<java.lang.Object> |
getList(java.lang.String key)
Get child array of this object node as a List.
|
java.util.Map<java.lang.String,java.lang.Object> |
getMap(int index)
Get child object of this array node as a Map.
|
private java.util.Map<java.lang.String,java.lang.Object> |
getMap(JSONDocument doc) |
java.util.Map<java.lang.String,java.lang.Object> |
getMap(java.lang.String key)
Get child object of this object node as a Map.
|
java.lang.Number |
getNumber(int index)
Get the array node's value at specified index as a Number.
|
java.lang.Number |
getNumber(java.lang.String key)
Get the object node's named value as a Number.
|
java.lang.String |
getString(int index)
Get the array node's value at specified index as a String.
|
java.lang.String |
getString(java.lang.String key)
Get the object node's named value as a String.
|
int |
hashCode() |
boolean |
isArray()
Test if the node is an ARRAY.
|
boolean |
isNull(int index)
Get if the array node's value at specified index is null.
|
boolean |
isNull(java.lang.String key)
Get if the object node's named value is null.
|
boolean |
isObject()
Test if the node is an OBJECT.
|
JSONDocument.IteratorWrapper |
iterator() |
java.util.Map<java.lang.String,java.lang.Object> |
object()
Returns the map representation of the node.
|
private void |
prettyPrint(java.io.Writer sb,
int depth,
boolean pretty) |
private void |
printArray(java.io.Writer sb,
java.util.List<java.lang.Object> obj,
int depth,
boolean pretty) |
(package private) static void |
printEscapedString(java.io.Writer sb,
java.lang.String s) |
private void |
printJSON(java.io.Writer sb,
java.lang.Object obj,
int depth,
boolean pretty) |
private void |
printObject(java.io.Writer sb,
java.util.Map<java.lang.String,java.lang.Object> obj,
int depth,
boolean pretty) |
JSONDocument |
set(int index,
JSONDocument value)
Set child object of this node.
|
private java.lang.Object |
set(int index,
java.lang.Object value,
java.lang.Class<?> type) |
JSONDocument |
set(java.lang.String key,
JSONDocument child)
Set child object of this node.
|
private java.lang.Object |
set(java.lang.String key,
java.lang.Object value,
java.lang.Class<?> type) |
java.lang.Boolean |
setBoolean(int index,
java.lang.Boolean value)
Set value of this node as Boolean.
|
java.lang.Boolean |
setBoolean(java.lang.String key,
java.lang.Boolean value)
Set value of this node as Boolean.
|
java.lang.Object |
setNull(int index)
Set value of this node to null.
|
java.lang.Object |
setNull(java.lang.String key)
Set value of this node to null.
|
java.lang.Number |
setNumber(int index,
java.lang.Number value)
Set value of this node as Number.
|
java.lang.Number |
setNumber(java.lang.String key,
java.lang.Number value)
Set value of this node as Number.
|
java.lang.String |
setString(int index,
java.lang.String value)
Set value of this node as String.
|
java.lang.String |
setString(java.lang.String key,
java.lang.String value)
Set value of this node as String.
|
java.lang.String |
toJSON()
Returns a string representation of the node and its children,
without newlines or whitespace.
|
void |
toJSON(java.io.Writer writer)
Writes a string representation of the node and its children,
without newlines or whitespace, to the supplied Writer.
|
java.lang.String |
toString() |
JSONDocument.Type |
type()
Returns the type of the node
|
public static final java.lang.String JSON_XPATH_URI
javax.xml.xpath.XPathFactory
to specify
a subset of javax.xml.xpath.XPath
over JSON.public static final JSONDocument EMPTY_ARRAY
public static final JSONDocument EMPTY_OBJECT
private final JSONDocument.Type type
private final java.util.List<java.lang.Object> array
private final java.util.Map<java.lang.String,java.lang.Object> object
public JSONDocument(JSONDocument.Type type)
type
- the type of the node, an ARRAY or an OBJECT.private JSONDocument(JSONDocument.Type type, int length)
public static JSONDocument createObject()
public static JSONDocument createArray()
public static JSONDocument createArray(int length)
public JSONDocument.Type type()
public java.util.List<java.lang.Object> array()
public java.util.Map<java.lang.String,java.lang.Object> object()
public boolean isArray()
public boolean isObject()
public void toJSON(java.io.Writer writer) throws java.io.IOException
java.io.IOException
- if there are errors writing to the Writerfor a variant that returns a String
,
for a human-readable representation
public java.lang.String toJSON()
for a human-readable representation
private void printJSON(java.io.Writer sb, java.lang.Object obj, int depth, boolean pretty) throws java.io.IOException
java.io.IOException
private void printArray(java.io.Writer sb, java.util.List<java.lang.Object> obj, int depth, boolean pretty) throws java.io.IOException
java.io.IOException
private void printObject(java.io.Writer sb, java.util.Map<java.lang.String,java.lang.Object> obj, int depth, boolean pretty) throws java.io.IOException
java.io.IOException
static void printEscapedString(java.io.Writer sb, java.lang.String s) throws java.io.IOException
java.io.IOException
private void prettyPrint(java.io.Writer sb, int depth, boolean pretty) throws java.io.IOException
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public JSONDocument get(java.lang.String key)
key
- the key for the current object node whose value
is to be retrievedpublic JSONDocument get(int index)
index
- the index for the current array node at which the node
is to be retrievedpublic java.util.Map<java.lang.String,java.lang.Object> getMap(java.lang.String key)
key
- the key for the current object node whose value
is to be retrievedpublic java.util.Map<java.lang.String,java.lang.Object> getMap(int index)
index
- the index for the current array node at which the node
is to be retrievedpublic java.util.List<java.lang.Object> getList(java.lang.String key)
key
- the key for the current object node whose value
is to be retrievedpublic java.util.List<java.lang.Object> getList(int index)
index
- the index for the current array node at which the node
is to be retrievedpublic java.lang.String getString(java.lang.String key)
key
- the name of the valuepublic java.lang.String getString(int index)
index
- the array index whose value is to be returnedpublic java.lang.Boolean getBoolean(java.lang.String key)
key
- the name of the valuepublic java.lang.Boolean getBoolean(int index)
index
- the array index whose value is to be returnedpublic java.lang.Number getNumber(java.lang.String key)
key
- the name of the valuepublic java.lang.Number getNumber(int index)
index
- the array index whose value is to be returnedpublic boolean isNull(java.lang.String key)
key
- the name of the valuepublic boolean isNull(int index)
index
- the array index whose value is to be checkedprivate java.util.Map<java.lang.String,java.lang.Object> getMap(JSONDocument doc)
private java.util.List<java.lang.Object> getList(JSONDocument doc)
private java.lang.Object get(java.lang.String key, java.lang.Class<?> type)
private java.lang.Object get(int index, java.lang.Class<?> type)
public JSONDocument set(java.lang.String key, JSONDocument child)
key
- the name of the childvalue
- the node containing the new valuespublic JSONDocument set(int index, JSONDocument value)
index
- the index at which the child is to be setvalue
- the node containing the new valuespublic java.lang.String setString(java.lang.String key, java.lang.String value)
key
- the name of the valuevalue
- the new valuepublic java.lang.String setString(int index, java.lang.String value)
index
- the index at which the value is to be setvalue
- the new valuepublic java.lang.Boolean setBoolean(java.lang.String key, java.lang.Boolean value)
key
- the name of the valuevalue
- the new valuepublic java.lang.Boolean setBoolean(int index, java.lang.Boolean value)
index
- the index at which the value is to be setvalue
- the new valuepublic java.lang.Number setNumber(java.lang.String key, java.lang.Number value)
key
- the name of the valuevalue
- the new valuepublic java.lang.Number setNumber(int index, java.lang.Number value)
index
- the index at which the value is to be setvalue
- the new valuepublic java.lang.Object setNull(java.lang.String key)
key
- the name of the valuepublic java.lang.Object setNull(int index)
index
- the index at which the value is to be setvalue
- the new valueprivate java.lang.Object set(java.lang.String key, java.lang.Object value, java.lang.Class<?> type)
private java.lang.Object set(int index, java.lang.Object value, java.lang.Class<?> type)
public JSONDocument.IteratorWrapper iterator()
iterator
in interface java.lang.Iterable<JSONDocument>