Class Json
put(java.lang.Object, java.lang.Object)
on this object. The various isNNN
methods can be used to determine the current type,
or you can just call stringValue()
, intValue()
etc to attempt to retrieve a typed value.
Keys and Paths
put
, get
, has
and remove
all accept a key,
which is an index into this object (a normal "map" or "list" key).
For lists it must be an Integer, and for maps it can theoretically be any type of object, but is currently
limited in to String
, Number
or Boolean
.
Non-String keys are only preserved when serialising to CBOR or Msgpack, and will be converted
to Strings when the object is serialised to JSON;
putPath
, getPath
, hasPath
and removePath
all accept a path, which is a String identifying a descendent.
These may be compound paths, eg a.b
, a.b[2]
. If a part of the path contains a dot or
square bracket it can be quoted and referenced inside square brackets, eg a.b["dotted.key"]
.
For speed, values supplied between two quotes simply have their quotes removed; they are not unescaped.
So json.put("\"\"\", true)
will create the structure {"\\"":true}
.
Paths that traverse lists are always converted to integers, and paths that traverse maps are converted to strings.
Note: prior to version 5,
put
, get
, has
and remove
accepted a path, as
non-String keys could not be stored.
Serialization
Object are read from the read(java.lang.CharSequence)
methods and written with the write(java.lang.Appendable, com.bfo.json.JsonWriteOptions)
method. The process
can be controlled by specifying JsonReadOptions
or JsonWriteOptions
as appropriate, although
the default will read/write as defined in RFC8259 or
RFC7049 as appropriate.
In all cases the Stream is not closed at the end of the read or write.
Since version 2, objects can also be serialized as CBOR (and since version 3, Msgpack), as defined in RFC7049 and the Msgpack spec. There are some differences between the JSON and CBOR/Msgpack object models which are significant, and to combine the two into one interface, some minor limitations to the datamodels are in place.
-
Unlike JSON, CBOR/Msgpack supports binary data, which we read as a
ByteBuffer
- these can be identified with theisBuffer()
method. When serializing a ByteBuffer to JSON, it will be Base-64 encoded with no padding, as recommended in RFC7049. By default the "URL- and filename-safe" variation of BAse64 will be used when writing (seeJsonWriteOptions.setBase64Standard(boolean)
). When reading, all Base64 variations can be parsed. The ByteBuffer position is ignored; it will be reset to zero before every read, write, or when it is returned frombufferValue()
. - JSON does not support NaN or infinite floating point values. When serializing these values to JSON, they will be serialized as null - this matches the behavior of all web-browsrs.
-
CBOR supports tags on JSON value, which can be accessed with the
setTag()
andgetTag()
methods. Tags can be set on any object, but will be ignored when serializing to JSON. CBOR supports positive value tags of any value, but these are limited to 63 bits by this API. If values higher than that are encountered when reading,readCbor()
method will throw an IOException. -
Msgpack supports "ext" types, which are treated as ByteBuffer objects with the extension type set
retrievable by calling
getTag()
- the tag is a number from 0..255. Msgpack does not support integer values greater than 64-bit; attempting to write these will throw an IOException. -
CBOR/Msgpack support complex key types in maps, such as lists or other maps. If these are encountered
with this API when reading, they will be converted to strings by default, or
(if
JsonReadOptions.setFailOnComplexKeys(boolean)
is set) throw anIOException
. CBOR/Msgpack also supports duplicate keys in maps - this abomination is not allowed in this API, and thereadCbor()
andreadMsgpack()
methods will throw anIOException
if found. -
CBOR supports the "undefined" value which is distinct from null. This can be created using the
UNDEFINED
constant, and tested for withisUndefined(java.lang.Object)
(since version 5) - CBOR allows for a number of undefined special types to be used without error. These will be loaded as undefined values, with a Tag set that identifies the type. There is no such conversion when writing; these values will be written as a tagged undefined, not the original special type.
Events
Listeners can be attached to a JSON object, and will receivedJsonEvents
when changes are
made to this item or its descendants. See the JsonEvent
API docs for an example of how to
use this to record changes to an object.
Thread Safety
This object is not synchronized, and if it is being modified in one thread while being read in another, external locking should be put in place.Examples
Json json = Json.read("{}"); json.putPath("a.b[0]", 0); assert json.get("a").get("b") == json.getPath("a.b"); assert json.getPath("a.b[0]").type().equals("number"); assert json.getPath("a.b[0]").isNumber(); assert json.getPath("a.b[0]").intValue() == 0; assert json.get("a").type().equals("map"); json.putPath("a.b[2]", 1); assert json.toString().equals("{\"a\":{\"b\":[0,null,2]}}"); json.putPath("a.b", true); assert json.toString().equals("{\"a\":{\"b\":true}}"); json.write(System.out, null); Json json2 = Json.read("[]"); json2.put(0, 0); json2.put(2, 2); assert json2.toString().equals("[0,null,2]"); json2.put("a", "a"); assert json2.toString().equals("{\"0\":0,\"2\":2,\"a\":\"a"}");
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a new Json object that represents the specified object.Json
(Object object, JsonFactory factory) Create a new Json object that represents the specified object. -
Method Summary
Modifier and TypeMethodDescriptionaddListener
(JsonListener listener) Add aJsonListener
to this class, if it has not already been added.boolean
Return the value of this node as a boolean.boolean
booleanValue
(Object key) If the specified child of this object exists call thebooleanValue()
method on it, otherwise return falseReturn the value of this node as a ByteBuffer.bufferValue
(Object key) If the specified child of this object exists call thebufferValue()
method on it, otherwise return nulldouble
Return the value of this node as a double.double
doubleValue
(Object key) If the specified child of this object exists call thedoubleValue()
method on it, otherwise return 0Create and return a deep copy of this Json tree.boolean
Get the path from this node to the specified object, which should be a descendant of this node.float
Return the value of this node as an float.float
floatValue
(Object key) If the specified child of this object exists call thefloatValue()
method on it, otherwise return 0Return the specified child of this object, or null if no value exists at the specified keyReturn the default JsonFactory, as set bysetFactory(com.bfo.json.JsonFactory)
Return the read-only set of listeners on this class.Return the specified descendant of this object, or null if no value exists at the specified path.long
getTag()
Return the tag for this item.boolean
If object is a Json object, return true if this object is a list or map, and contains that value.int
hashCode()
Return a hashCode based on thevalue()
boolean
Return true if this object has a non-null/non-undefined descendant at the specified path.int
intValue()
Return the value of this node as an integer.int
If the specified child of this object exists call theintValue()
method on it, otherwise return 0boolean
Return true if this node is a "boolean"boolean
Return true if the specified child of this object is of type "boolean".boolean
isBuffer()
Return true if this node is a "buffer"boolean
Return true if the specified child of this object is of type "buffer".boolean
isEmpty()
Return true if this node is a number, string, buffer, boolean, null or an empty map or list.boolean
isList()
Return true if this node is a "list"boolean
Return true if the specified child of this object is of type "list".boolean
isMap()
Return true if this node is a "map"boolean
Return true if the specified wchild of this object is of type "map".boolean
isNull()
Return true if this node is nullboolean
Return true if the specified child of this object is of type "null".boolean
isNumber()
Return true if this node is a "number"boolean
Return true if the specified child of this object is of type "number".boolean
isString()
Return true if this node is a "string"boolean
Return true if the specified child of this object is of type "string".boolean
Return true if this node is undefined.boolean
isUndefined
(Object key) Return true if the specified child of this object is of type "undefined".Return an Iterator that will descend through every leaf node under this object in a depth-first traveral.Return the value of this node as a list.If the specified wchild of this object exists call thelistValue()
method on it, otherwise return nulllong
Return the value of this node as a long.long
If the specified child of this object exists call thelongValue()
method on it, otherwise return 0mapValue()
Return the value of this node as a map.If the specified descendant of this object exists call themapValue()
method on it, otherwise return nullReturn the value of this node as a Number.numberValue
(Object key) If the specified child of this object exists call theintValue()
method on it, otherwise return nullReturn this Json value as a plain object.parent()
Get the parent of this node, if knownPut the specified value into this object with the specified key.Put the specified value into this object or one of its descendants by parsing the specified path.static Json
read
(InputStream in, JsonReadOptions options) Read a Json object from the specified InputStream.static Json
read
(Reader in, JsonReadOptions options) Read a Json object from the specified Reader.static Json
read
(CharSequence in) Read a Json object from the specified String.static Json
readCbor
(InputStream in, JsonReadOptions options) Read a CBOR formatted object from the specified InputStream.static Json
readCbor
(ByteBuffer in, JsonReadOptions options) Read a CBOR formatted object from the specified ByteBuffer.static Json
readMsgpack
(InputStream in, JsonReadOptions options) Read a Msgpack formatted object from the specified InputStream.static Json
readMsgpack
(ByteBuffer in, JsonReadOptions options) Read a Msgpack formatted object from the specified ByteBuffer.Remove the item at the specified path from this object or one of its descendants.removeListener
(JsonListener listener) Remove aJsonListener
from this classremovePath
(String path) Remove the item at the specified path from this object or one of its descendants.setFactory
(JsonFactory factory) Set the default JsonFactory for this object and its descendants.setTag
(long tag) Set the tag for this item.Copy the internal value from the specified Json object to this object.int
size()
Return the size of this object, or zero if this object is a number, string, buffer, boolean or null.sort()
For Json objects that are maps, sort the map keys.Return the value of this node as a String.stringValue
(Object key) If the specified child of this object exists call thestringValue()
method on it, otherwise return nulltoCbor()
Return a Cbor representation of this Json object.toCbor
(JsonWriteOptions options) Return a Cbor representation of this Json object with the specified optionstoString()
Return a String representation of this Json object.toString
(JsonWriteOptions options) Return a String representation of this Json object with the specified serialization options.type()
Return the type of this node, which may be "number", "string", "boolean", "list", "map", "buffer", "null" or (since v5) "undefined"value()
write
(Appendable out, JsonWriteOptions options) Write the Json object to the specified outputwriteCbor
(OutputStream out, JsonWriteOptions options) Write the Json object in the CBOR format to the specified outputwriteMsgpack
(OutputStream out, JsonWriteOptions options) Write the Json object in the Msgpack format to the specified output
-
Field Details
-
UNDEFINED
A constant object that can be passed into the Json constructor to create a Cbor "undefined" value- Since:
- 5
-
-
Constructor Details
-
Json
Create a new Json object that represents the specified object. The object should be a
CharSequence
,Boolean
,Number
,ByteBuffer
,byte[]
,Map
orCollection
; if a Map or Collection, the collection is copied rather than referenced, and the values must also meet this criteria. A ByteBuffer (or byte[]) is not a native Json type, but is used for CBOR. The buffer is not copied, it is stored by reference.An fast alternative method for creating a Json object representing an empty map or list is to call
Json.read("{}")
orJson.read("[]")
- Parameters:
object
- the object- Throws:
ClassCastException
- if these conditions are not met
-
Json
Create a new Json object that represents the specified object. As forJson(Object)
, but first attempts to convert the object to a Json object by callingJsonFactory.toJson(java.lang.Object)
.- Parameters:
object
- the objectfactory
- the factory for conversion, which may be null. Will be passed tosetFactory(com.bfo.json.JsonFactory)
- Throws:
ClassCastException
- if the object cannot be converted to Json
-
-
Method Details
-
setFactory
Set the default JsonFactory for this object and its descendants. Any objects passed intoput()
will be converted using this factory. The default is null- Parameters:
factory
- the factory- Returns:
- this
- Since:
- 2
-
getFactory
Return the default JsonFactory, as set bysetFactory(com.bfo.json.JsonFactory)
- Returns:
- the Factory set by
setFactory(com.bfo.json.JsonFactory)
- Since:
- 2
-
read
Read a Json object from the specified String. The object may be a structured type or a primitive value (boolean, number or string). The values "{}" and "[]" may be supplied to create a new Json map or array.- Parameters:
in
- the String, which must not be null or empty.- Returns:
- the Json object
- Throws:
IllegalArgumentException
- if the JSON is invalid
-
read
Read a Json object from the specified InputStream. If the stream begins with a valid byte-order mark, that will be used determine the encoding (which must be UTF-8 or UTF-16), otherwise the stream will be sniffed for UTF-16, and otherwise parsed as UTF-8.
If you are sure the InputStream is in UTF-8 and has no byte-order mark, as recommended in RFC8259, then you're better off calling
read(new InputStreamReader(in, "UTF-8"), options)
as this will remove the possibility of guessing an incorrect encoding- Parameters:
in
- the InputStreamoptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an IO exception was encountered during readingIllegalArgumentException
- if the JSON is invalid
-
read
Read a Json object from the specified Reader.- Parameters:
in
- the Readeroptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an IO exception was encountered during readingIllegalArgumentException
- if the JSON is invalid
-
readCbor
Read a CBOR formatted object from the specified InputStream.- Parameters:
in
- the InputStreamoptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an I/O exception was encountered during reading or the stream does not meet the CBOR format- Since:
- 2
-
readCbor
Read a CBOR formatted object from the specified ByteBuffer.- Parameters:
in
- the ByteBufferoptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an I/O exception was encountered during reading or the stream does not meet the CBOR format- Since:
- 2
-
readMsgpack
Read a Msgpack formatted object from the specified InputStream.- Parameters:
in
- the InputStreamoptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an I/O exception was encountered during reading or the stream does not meet the MsgPack format- Since:
- 3
-
readMsgpack
Read a Msgpack formatted object from the specified ByteBuffer.- Parameters:
in
- the ByteBufferoptions
- the options to use for reading, or null to use the default- Returns:
- the Json object
- Throws:
IOException
- if an I/O exception was encountered during reading or the stream does not meet the CBOR format- Since:
- 3
-
writeCbor
Write the Json object in the CBOR format to the specified output- Parameters:
out
- the outputoptions
- the JsonWriteOptions to use when writing, or null to use the default- Returns:
- the "out" parameter
- Throws:
IOException
- if an IOException is thrown while writing- Since:
- 2
-
writeMsgpack
Write the Json object in the Msgpack format to the specified output- Parameters:
out
- the outputoptions
- the JsonWriteOptions to use when writing, or null to use the default- Returns:
- the "out" parameter
- Throws:
IOException
- if an IOException is thrown while writing- Since:
- 3
-
addListener
Add aJsonListener
to this class, if it has not already been added. The listener will received events for any changes to this object or its descendants - theJsonEvent
will reference which object the event relates to, and thefind(com.bfo.json.Json)
method can be used to construct the path to that object if of interest.- Parameters:
listener
- listener to add, which may not be null- Returns:
- this object
-
removeListener
Remove aJsonListener
from this class- Parameters:
listener
- listener to remove- Returns:
- this object
-
getListeners
Return the read-only set of listeners on this class.- Returns:
- a read-only collection of listeners, which will never be null
-
write
Write the Json object to the specified output- Parameters:
out
- the outputoptions
- the JsonWriteOptions to use when writing, or null to use the default- Returns:
- the "out" parameter
- Throws:
IOException
- if an IOException is thrown while writing
-
duplicate
Create and return a deep copy of this Json tree. Note that ByteBuffers values will not be cloned, and the returned item will have no listeners- Returns:
- a deep copy of this item
-
parent
Get the parent of this node, if known- Returns:
- the items parent, or null if it has not been added to another Json object
-
find
Get the path from this node to the specified object, which should be a descendant of this node. If the parameter is null or not a descendant of this object, this method returns null.
Specifically, if this method returns not null then it is the case that
this.getPath(this.find(node)) == node
.Implementation note: this method is implemented by traversing up from the descendant to this object; possible because a Json object can only have one parent. So the operation is O(n).
- Parameters:
descendant
- the presumed descendant of this object to find in the tree- Returns:
- the path from this node to the descendant object
- Since:
- 5 (was called "find" prior to that)
-
put
Put the specified value into this object with the specified key. Although the key can be any value, it will collapse to a String when serialised as JSON. If the object is not a Json, it will be converted with the factory set bysetFactory()
, if any.- Parameters:
key
- the key - if this object is a list and the key is a non-negative integer, it will be used as the list index. Otherwise this object will be converted to a map. Must not be null.value
- the value to insert, which must not be this or an ancestor of this- Returns:
- the object that was previously found at that path, which may be null
-
get
Return the specified child of this object, or null if no value exists at the specified key- Parameters:
key
- the key, which should be an integer (for lists) or any value for maps- Returns:
- the Json object at that path or null if none exists
-
has
If object is a Json object, return true if this object is a list or map, and contains that value. Otherwise return true if this object is a list or map and contains a non-null/non-undefined object at that entry.- Parameters:
object
- the object- Returns:
- true if this is a list or map and the object is either a Json and present in this object as a value, or is used as a key for a non-null/non-undefined value
- Since:
- 5
-
remove
Remove the item at the specified path from this object or one of its descendants. Or, if object is aJson
, remove that value from this list or map. If called on an object that is not a list or map, this method has no effect.- Parameters:
object
- if a Json object, the value to remove, otherwise the key to remove from this object.- Returns:
- the object that was removed, or null if nothing was removed
- Since:
- 5
-
putPath
Put the specified value into this object or one of its descendants by parsing the specified path. If the path specifies a compound key than any intermediate descendants are created as required. If the path specifies an existing object then the old object (which may be a subtree) is removed and returned. The object will be converted with the factory set bysetFactory()
, if any.- Parameters:
path
- the key, which may be a compound key (e.g "a.b" or "a.b[2]") and must not be nullvalue
- the value to insert, which must not be this or an ancestor of this- Returns:
- the object that was previously found at that path, which may be null
- Since:
- 5 - prior that that revision was called "put"
-
getPath
Return the specified descendant of this object, or null if no value exists at the specified path.- Parameters:
path
- the path, which must not be null- Returns:
- the Json object at that path or null if none exists
-
hasPath
Return true if this object has a non-null/non-undefined descendant at the specified path.- Parameters:
path
- the path- Returns:
- true if this object is a list or map, it has the specified descendant and the descendant is not null
-
removePath
Remove the item at the specified path from this object or one of its descendants.- Parameters:
path
- the key, which may be a compound key (e.g "a.b" or "a.b[2]") and must not be null- Returns:
- the object that was removed, or null if nothing was removed
- Since:
- 5 was called remove() prior to version 5
-
size
public int size()Return the size of this object, or zero if this object is a number, string, buffer, boolean or null.- Returns:
- the size of the object
-
getTag
public long getTag()Return the tag for this item. Tags are only used with the CBOR serialization. Although CBOR alows positive tag values of any size, this implementation limits them to 63 bits. If no tag is set (the default) this method returns -1.- Returns:
- the tag, or -1 if none is set.
- Since:
- 2
-
setTag
Set the tag for this item. Tags are only used with the CBOR serialization, so they will be ignored when writing to Json. Although CBOR allows positive value tags of any size, this implementation limits them to 63 bits.- Parameters:
tag
- the tag, or a negative number to remove the tag.- Returns:
- this
- Since:
- 2
-
isEmpty
public boolean isEmpty()Return true if this node is a number, string, buffer, boolean, null or an empty map or list.- Returns:
- true if the node is a leaf node.
-
isNull
Return true if the specified child of this object is of type "null". Equivalent toget(key) != null && get(key).isNull()
. It is not the same as the key being missing.- Parameters:
key
- the key- Returns:
- true if the child exists and is null
-
isUndefined
Return true if the specified child of this object is of type "undefined". Equivalent toget(key) != null && get(key).isUndefined()
. It is not the same as the key being missing.- Parameters:
key
- the key- Returns:
- true if the child exists and is undefined
-
isBuffer
Return true if the specified child of this object is of type "buffer". Equivalent toget(key) != null && get(key).isBuffer()
.- Parameters:
key
- the key- Returns:
- true if the child exists and is a buffer
-
isString
Return true if the specified child of this object is of type "string". Equivalent toget(key) != null && get(key).isString()
.- Parameters:
key
- the key- Returns:
- true if the child exists and is a string
-
isNumber
Return true if the specified child of this object is of type "number". Equivalent toget(key) != null && get(key).isNumber()
- Parameters:
key
- the key- Returns:
- true if the child exists and is a number
-
isBoolean
Return true if the specified child of this object is of type "boolean". Equivalent toget(key) != null && get(key).isBoolean()
- Parameters:
key
- the key- Returns:
- true if the child exists and is a boolean
-
isList
Return true if the specified child of this object is of type "list". Equivalent toget(key) != null && get(key).isList()
- Parameters:
key
- the key- Returns:
- true if the child exists and is a list
-
isMap
Return true if the specified wchild of this object is of type "map". Equivalent toget(key) != null && get(key).isMap()
- Parameters:
key
- the key- Returns:
- true if the child exists and is a map
-
leafIterator
Return an Iterator that will descend through every leaf node under this object in a depth-first traveral. The returned keys are converter to Strings; they are relative to this node's path and will always return non-null if passed intogetPath(java.lang.StringBuilder, com.bfo.json.Json)
. If this is called on a leaf nodes, it returns an empty iterator- Returns:
- an Iterator as described
-
sort
For Json objects that are maps, sort the map keys. For other types this is a no-op- Returns:
- this
- Since:
- 5
-
type
Return the type of this node, which may be "number", "string", "boolean", "list", "map", "buffer", "null" or (since v5) "undefined"- Returns:
- the object type
-
isNull
public boolean isNull()Return true if this node is null- Returns:
- true if the object is null
-
isUndefined
public boolean isUndefined()Return true if this node is undefined. Undefined is a CBOR concept; in JSON serialization, this will collapse to null- Returns:
- true if the object is null
- Since:
- 5
-
isNumber
public boolean isNumber()Return true if this node is a "number"- Returns:
- true if the object is a number
-
isBoolean
public boolean isBoolean()Return true if this node is a "boolean"- Returns:
- true if the object is a boolean
-
isString
public boolean isString()Return true if this node is a "string"- Returns:
- true if the object is a string
-
isBuffer
public boolean isBuffer()Return true if this node is a "buffer"- Returns:
- true if the object is a buffer
-
isMap
public boolean isMap()Return true if this node is a "map"- Returns:
- true if the object is a map
-
isList
public boolean isList()Return true if this node is a "list"- Returns:
- true if the object is a list
-
value
Return the value of this Json as a plain Java object, which may be aString
,Boolean
,Number
,List
,Map
,ByteBuffer
or null.- Returns:
- the object value
-
setValue
Copy the internal value from the specified Json object to this object. This can be useful to update the value of a Json object in a structure, without having to modify the structure. For example:Json a = new Json(999); Json b = new Json("string"); a.setValue(b); System.out.println(a); // "string"
- Parameters:
json
- the json object that is the source of the intended value of this object- Returns:
- this
-
stringValue
Return the value of this node as a String. This method will always succeed. It will return null if this object represents null, but it otherwise identical totoString()
- Returns:
- the string value of this object
-
stringValue
If the specified child of this object exists call thestringValue()
method on it, otherwise return null- Parameters:
key
- the key- Returns:
- the string value of that object
- Since:
- 4
-
bufferValue
Return the value of this node as a ByteBuffer. ByteBuffers only exist natively in the CBOR and Msgpack serialization. Note that every ByteBuffer created by the API will be backed by an array of the same size; it is guaranteed that
buffer.array().length == buffer.limit() && buffer.arrayOffset() == 0
.- A string will be decoded using Base64 (no padding) and, if valid returned as a ByteBuffer. If invalid, a ClassCastException will be thrown
- null will return null
- Returns:
- the buffer value of this object, which will always have position=0
- Throws:
ClassCastException
- if none of these conditions are met- Since:
- 2
- See Also:
-
bufferValue
If the specified child of this object exists call thebufferValue()
method on it, otherwise return null- Parameters:
key
- the key- Returns:
- the buffer value of that object
- Since:
- 4
-
numberValue
Return the value of this node as a Number.- A number will be returned as an Integer, Long, Double, BigInteger or BigDecimal as appropriate
- A string in the format specified in RFC8259 will be parsed and returned as an Integer, Long or Double (if
"strictTypes"
was not specified) - An empty string will return 0 (if
"looseEmptyStrings"
was specified)) - A boolean will return an Integer that is 1 or 0 (if
"strictTypes"
was not specified) - null will return null
- Returns:
- the number value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
numberValue
If the specified child of this object exists call theintValue()
method on it, otherwise return null- Parameters:
key
- the key- Returns:
- the number value of that object
- Since:
- 4
-
intValue
public int intValue()Return the value of this node as an integer.- A number will be converted to int
- A string that can be parsed with Integer.parseInt will converted (if
"strictTypes"
was not specified) - An empty string will return 0 (if
"looseEmptyStrings"
was specified)) - A boolean will return 1 or 0 (if
"strictTypes"
was not specified)
- Returns:
- the int value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
intValue
If the specified child of this object exists call theintValue()
method on it, otherwise return 0- Parameters:
key
- the key- Returns:
- the int value of that object
- Since:
- 4
-
longValue
public long longValue()Return the value of this node as a long.- A number will be converted to long
- A string that can be parsed with Long.parseLong will converted (if
"strictTypes"
was not specified) - An empty string will return 0 (if
"looseEmptyStrings"
was specified)) - A boolean will return 1 or 0 (if
"strictTypes"
was not specified)
- Returns:
- the long value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
longValue
If the specified child of this object exists call thelongValue()
method on it, otherwise return 0- Parameters:
key
- the key- Returns:
- the long value of that object
- Since:
- 4
-
floatValue
public float floatValue()Return the value of this node as an float.- A number will be converted to float
- A string in the format specified in RFC8259 will be parsed and returned (if
"strictTypes"
was not specified) - An empty string will return 0 (if
"looseEmptyStrings"
was specified) - A boolean will return 1 or 0 (if
"strictTypes"
was not specified)
- Returns:
- the float value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
floatValue
If the specified child of this object exists call thefloatValue()
method on it, otherwise return 0- Parameters:
key
- the key- Returns:
- the float value of that object
- Since:
- 4
-
doubleValue
public double doubleValue()Return the value of this node as a double.- A number will be converted to double
- A string in the format specified in RFC8259 will be parsed and returned (if
"strictTypes"
was not specified) - An empty string will return 0 (if
"looseEmptyStrings"
was specified) - A boolean will return 1 or 0 (if
"strictTypes"
was not specified)
- Returns:
- the double value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
doubleValue
If the specified child of this object exists call thedoubleValue()
method on it, otherwise return 0- Parameters:
key
- the key- Returns:
- the double value of that object
- Since:
- 4
-
booleanValue
public boolean booleanValue()Return the value of this node as a boolean.- A boolean will return its value
- A number evaluating to 0 will return false, otherwise it will return true (if
"strictTypes"
was not specified) - The string "false", or one that represents a number evaluating to 0 will return false, otherwise it will return true (if
"strictTypes"
was not specified) - An empty string will return false (if
"looseEmptyStrings"
was specified)
- Returns:
- the boolean value of this object
- Throws:
ClassCastException
- if none of these conditions are met- See Also:
-
booleanValue
If the specified child of this object exists call thebooleanValue()
method on it, otherwise return false- Parameters:
key
- the key- Returns:
- the boolean value of that object
- Since:
- 4
-
mapValue
Return the value of this node as a map. The returned Map is read-only- Returns:
- the read-only map value of this object
- Throws:
ClassCastException
- if the node is not a map.
-
mapValue
If the specified descendant of this object exists call themapValue()
method on it, otherwise return null- Parameters:
key
- the key- Returns:
- the read-only map value of that object
- Since:
- 4
-
listValue
Return the value of this node as a list. The returned List is read-only- Returns:
- the read-only list value of this object
- Throws:
ClassCastException
- if the node is not a list
-
listValue
If the specified wchild of this object exists call thelistValue()
method on it, otherwise return null- Parameters:
key
- the key- Returns:
- the read-only list value of that object
- Since:
- 4
-
objectValue
Return this Json value as a plain object. Unlikevalue()
this method will traverse through its descendants, converting Json objects to plain objects so that the returned object is guaranteed to have no reference to this package. The returned value is as follows- If this object
is null
, return null - If a
factory
is set andJsonFactory.fromJson()
returns a non-null value, return that value - If this value is a string, buffer, number or boolean, return the value from
value()
- If this value is a list or map, populate the map values with the output of this method and return as a Map<String,Object> or List<Object>
- Returns:
- a String, Number, Boolean, Map<String,Object>, List<Object> or null as described
- If this object
-
hashCode
public int hashCode()Return a hashCode based on thevalue()
-
equals
-
toString
Return a String representation of this Json object. Roughly equivalent to callingreturn
write
(new StringBuilder(), null).toString() -
toString
Return a String representation of this Json object with the specified serialization options. Equivalent to callingreturn
write
(new StringBuilder(), options).toString()- Parameters:
options
- theJsonWriteOptions
to use for serializing, or null to use the default- Returns:
- the serialized object
- Since:
- 5
-
toCbor
Return a Cbor representation of this Json object.- Returns:
- the Cbor representation as an array-backed ByteBuffer
- Since:
- 5
-
toCbor
Return a Cbor representation of this Json object with the specified options- Parameters:
options
- theJsonWriteOptions
to use for serializing, or null to use the default- Returns:
- the Cbor representation as an array-backed ByteBuffer
- Since:
- 5
-