Types
JSONValue is an abstract class, and child classes represent actual JSON values. Call type() to get the type of the JSON value you have. The returned type tells you to which dedicated class you can cast the instance of JSONValue safely. The table below shows the correspondence between the type returned by type() and the class which you can cast to.
Type | Class |
---|---|
NULL | JSONValueNull |
BOOLEAN | JSONValueBoolean |
STRING | JSONValueString |
NUMBER | JSONValueNumber |
ARRAY | JSONValueArray |
OBJECT | JSONValueObject |
Instead of checking the value returned by type(), you can also use instanceof
operator.
The following sections explain the details of each type.
null
JSONValueNull has no value.
boolean
JSONValueBoolean holds a boolean value. Call value() to extract the boolean value.
string
JSONValueString holds an unescaped string value. Call value() to extract the string value.
number
JSONValueNumber holds a number value as String. Unfortunately, no Java class in the standard Java library can represent all possible numeric values that the JSON number notation can express without information loss.
RFC 8259 - 6. Numbers explains all possible numeric expressions in JSON. It allows an extremely large value like 1E100000, and an extremely precise value like 3.14159265358979323846264338327950288. Although the RFC 8259 allows libraries and applications to set limits on the range and precision of numbers accepted, this library does not set such limits to maximize interoperability. JSONValueNumber of this library holds each numeric value in a string as it appears in your original JSON text.
For your convenience, you can call toDouble() or toLong() to get a corresponding Java primitive value.
toDouble() internally calls Double.parseDouble(String)
. As far as the library author knows, this method does not throw an exception as long as you pass a valid JSON numeric expression. However, you loose information if you pass an extremely precise value, and you will get POSITIVE_INFINITY
or NEGATIVE_INFINITY
if you pass an extremely large value.
toLong() tries to convert the String representation of the JSON numeric value into long, but if it cannot convert without loosing information, it throws NumberFormatException
. For example, 1.52e2
appears to be a floating point value, but it is actually an integer 152
. Therefore, toLong() returns 152
. However, if there is a fractional part (e.g. 1.523e2
), or if the value is too large (e.g. 9223372036854775808
), this method throws NumberFormatException
.
array
JSONValueArray holds an ordered sequence of JSONValue. It implements List
interface, so all values can be iterated using for
statement, for example.
JSONText jsText = JSONText.fromString(" [ true, false, \"abc\", 1.52, null ] ");
JSONValue root = jsText.parse().root();
if (root.type() == JSONValueType.ARRAY) {
JSONValueArray rootArray = (JSONValueArray) root;
for (JSONValue jsonValue : rootArray) {
...
}
}
object
JSONValueObject holds a key-value map. It implements Map
interface. The class of the key is JSONValueString and that of the value is JSONValue. All key-value pairs can be iterated as shown below.
JSONText jsText = JSONText.fromString(
" { \"key1\": true, " +
" \"key2\": false," +
" \"key3\": null } ");
JSONValue root = jsText.parse();
if (root.type() == JSONValueType.OBJECT) {
JSONValueObject rootObj = (JSONValueObject) root;
for (Map.Entry<JSONValueString, JSONValue> entry: rootObj.entrySet()) {
JSONValueString key = entry.getKey();
JSONValue value = entry.getValue();
...
}
}