Class JsonValue
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
JsonArray,JsonObject
The literals true, false, and null are
represented by the constants Json.TRUE, Json.FALSE, and Json.NULL.
JSON objects and arrays are represented by the subtypes
JsonObject and JsonArray. Instances of these types can be created using the public constructors of these classes.
Instances that represent JSON numbers, strings and
boolean values can be created using the static factory methods
Json.value(String), Json.value(long), Json.value(double), etc.
In order to find out whether an instance of this class is of a certain type, the methods
isObject(), isArray(), isString(), isNumber() etc. can be used.
If the type of a JSON value is known, the methods asObject(), asArray(), asString(),
asInt(), etc. can be used to get this value directly in the appropriate target type.
This class is not supposed to be extended by clients.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionasArray()Returns this JSON value asJsonArray, assuming that this value represents a JSON array.booleanReturns this JSON value as abooleanvalue, assuming that this value is eithertrueorfalse.doubleasDouble()Returns this JSON value as adoublevalue, assuming that this value represents a JSON number.floatasFloat()Returns this JSON value as afloatvalue, assuming that this value represents a JSON number.intasInt()Returns this JSON value as anintvalue, assuming that this value represents a JSON number that can be interpreted as Javaint.longasLong()Returns this JSON value as alongvalue, assuming that this value represents a JSON number that can be interpreted as Javalong.asObject()Returns this JSON value asJsonObject, assuming that this value represents a JSON object.asString()Returns this JSON value as String, assuming that this value represents a JSON string.booleanIndicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object).inthashCode()booleanisArray()Detects whether this value represents a JSON array.booleanDetects whether this value represents a boolean value.booleanisFalse()Detects whether this value represents the JSON literalfalse.booleanisNull()Detects whether this value represents the JSON literalnull.booleanisNumber()Detects whether this value represents a JSON number.booleanisObject()Detects whether this value represents a JSON object.booleanisString()Detects whether this value represents a JSON string.booleanisTrue()Detects whether this value represents the JSON literaltrue.toString()Returns the JSON string for this value in its minimal form, without any additional whitespace.toString(WriterConfig config) Returns the JSON string for this value using the given formatting.voidWrites the JSON representation of this value to the given writer in its minimal form, without any additional whitespace.voidwriteTo(Writer writer, WriterConfig config) Writes the JSON representation of this value to the given writer using the given formatting.
-
Field Details
-
STRING_IS_NULL
- See Also:
-
OBJECT_IS_NULL
- See Also:
-
NAME_IS_NULL
- See Also:
-
VALUE_IS_NULL
- See Also:
-
ARRAY_IS_NULL
- See Also:
-
-
Method Details
-
isObject
Detects whether this value represents a JSON object. If this is the case, this value is an instance ofJsonObject.- Returns:
trueif this value is an instance of JsonObject
-
isArray
Detects whether this value represents a JSON array. If this is the case, this value is an instance ofJsonArray.- Returns:
trueif this value is an instance of JsonArray
-
isNumber
Detects whether this value represents a JSON number.- Returns:
trueif this value represents a JSON number
-
isString
Detects whether this value represents a JSON string.- Returns:
trueif this value represents a JSON string
-
isBoolean
Detects whether this value represents a boolean value.- Returns:
trueif this value represents either the JSON literaltrueorfalse
-
isTrue
Detects whether this value represents the JSON literaltrue.- Returns:
trueif this value represents the JSON literaltrue
-
isFalse
Detects whether this value represents the JSON literalfalse.- Returns:
trueif this value represents the JSON literalfalse
-
isNull
Detects whether this value represents the JSON literalnull.- Returns:
trueif this value represents the JSON literalnull
-
asObject
Returns this JSON value asJsonObject, assuming that this value represents a JSON object. If this is not the case, an exception is thrown.- Returns:
- a JSONObject for this value
- Throws:
UnsupportedOperationException- if this value is not a JSON object
-
asArray
Returns this JSON value asJsonArray, assuming that this value represents a JSON array. If this is not the case, an exception is thrown.- Returns:
- a JSONArray for this value
- Throws:
UnsupportedOperationException- if this value is not a JSON array
-
asInt
Returns this JSON value as anintvalue, assuming that this value represents a JSON number that can be interpreted as Javaint. If this is not the case, an exception is thrown.To be interpreted as Java
int, the JSON number must neither contain an exponent nor a fraction part. Moreover, the number must be in theIntegerrange.- Returns:
- this value as
int - Throws:
UnsupportedOperationException- if this value is not a JSON numberNumberFormatException- if this JSON number can not be interpreted asintvalue
-
asLong
Returns this JSON value as alongvalue, assuming that this value represents a JSON number that can be interpreted as Javalong. If this is not the case, an exception is thrown.To be interpreted as Java
long, the JSON number must neither contain an exponent nor a fraction part. Moreover, the number must be in theLongrange.- Returns:
- this value as
long - Throws:
UnsupportedOperationException- if this value is not a JSON numberNumberFormatException- if this JSON number can not be interpreted aslongvalue
-
asFloat
Returns this JSON value as afloatvalue, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.If the JSON number is out of the
Floatrange,Float.POSITIVE_INFINITYorFloat.NEGATIVE_INFINITYis returned.- Returns:
- this value as
float - Throws:
UnsupportedOperationException- if this value is not a JSON number
-
asDouble
Returns this JSON value as adoublevalue, assuming that this value represents a JSON number. If this is not the case, an exception is thrown.If the JSON number is out of the
Doublerange,Double.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYis returned.- Returns:
- this value as
double - Throws:
UnsupportedOperationException- if this value is not a JSON number
-
asString
Returns this JSON value as String, assuming that this value represents a JSON string. If this is not the case, an exception is thrown.- Returns:
- the string represented by this value
- Throws:
UnsupportedOperationException- if this value is not a JSON string
-
asBoolean
Returns this JSON value as abooleanvalue, assuming that this value is eithertrueorfalse. If this is not the case, an exception is thrown.- Returns:
- this value as
boolean - Throws:
UnsupportedOperationException- if this value is neithertrueorfalse
-
writeTo
Writes the JSON representation of this value to the given writer in its minimal form, without any additional whitespace.Writing performance can be improved by using a
BufferedWriter.- Parameters:
writer- the writer to write this value to- Throws:
IOException- if an I/O error occurs in the writer
-
writeTo
Writes the JSON representation of this value to the given writer using the given formatting.Writing performance can be improved by using a
BufferedWriter.- Parameters:
writer- the writer to write this value toconfig- a configuration that controls the formatting ornullfor the minimal form- Throws:
IOException- if an I/O error occurs in the writer
-
toString
Returns the JSON string for this value in its minimal form, without any additional whitespace. The result is guaranteed to be a valid input for the methodJson.parse(String)and to create a value that is equal to this object. -
toString
Returns the JSON string for this value using the given formatting.- Parameters:
config- a configuration that controls the formatting ornullfor the minimal form- Returns:
- a JSON string that represents this value
-
equals
Indicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object).Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.
-
hashCode
-