Class JsonValue
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
JsonArray,JsonObject
The literal names true, false, and null are
represented by the constants TRUE, FALSE, and NULL.
JSON objects and arrays are represented by the subtypes
JsonObject and JsonArray. Instances of these types can be created using the
public constructors.
Instances for JSON numbers and strings can be created using the
static factory methods valueOf(String), valueOf(long), valueOf(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 there is no doubt about the type of a JSON value, one of 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.
- Since:
- 2.1
- 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.static JsonValueReads a JSON value from the given reader.static JsonValueReads a JSON value from the given string.toString()Returns the JSON string for this value in its minimal form, without any additional whitespace.static JsonValuevalueOf(boolean value) Returns a JsonValue instance that represents the givenbooleanvalue.static JsonValuevalueOf(double value) Returns a JsonValue instance that represents the givendoublevalue.static JsonValuevalueOf(float value) Returns a JsonValue instance that represents the givenfloatvalue.static JsonValuevalueOf(int value) Returns a JsonValue instance that represents the givenintvalue.static JsonValuevalueOf(long value) Returns a JsonValue instance that represents the givenlongvalue.static JsonValueReturns a JsonValue instance that represents the given string.protected abstract voidwrite(org.eclipse.rap.json.JsonWriter writer) voidWrites the JSON representation for this object to the given writer.
-
Field Details
-
TRUE
Represents the JSON literaltrue. -
FALSE
Represents the JSON literalfalse. -
NULL
The JSON literalnull.
-
-
Method Details
-
readFrom
Reads a JSON value from the given reader.Characters are read in chunks and buffered internally, therefore wrapping an existing reader in an additional
BufferedReaderdoes not improve reading performance.- Parameters:
reader- the reader to read the JSON value from- Returns:
- the JSON value that has been read
- Throws:
IOException- if an I/O error occurs in the readerParseException- if the input is not valid JSON
-
readFrom
Reads a JSON value from the given string.- Parameters:
text- the string that contains the JSON value- Returns:
- the JSON value that has been read
- Throws:
ParseException- if the input is not valid JSON
-
valueOf
Returns a JsonValue instance that represents the givenintvalue.- Parameters:
value- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
- Since:
- 2.2
-
valueOf
Returns a JsonValue instance that represents the givenlongvalue.- Parameters:
value- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
valueOf
Returns a JsonValue instance that represents the givenfloatvalue.- Parameters:
value- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
valueOf
Returns a JsonValue instance that represents the givendoublevalue.- Parameters:
value- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
valueOf
Returns a JsonValue instance that represents the given string.- Parameters:
string- the string to get a JSON representation for- Returns:
- a JSON value that represents the given string
-
valueOf
Returns a JsonValue instance that represents the givenbooleanvalue.- Parameters:
value- the value to get a JSON representation for- Returns:
- a JSON value that represents the given value
-
isObject
public boolean 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
public boolean 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
public boolean isNumber()Detects whether this value represents a JSON number.- Returns:
trueif this value represents a JSON number
-
isString
public boolean isString()Detects whether this value represents a JSON string.- Returns:
trueif this value represents a JSON string
-
isBoolean
public boolean isBoolean()Detects whether this value represents a boolean value.- Returns:
trueif this value represents either the JSON literaltrueorfalse
-
isTrue
public boolean isTrue()Detects whether this value represents the JSON literaltrue.- Returns:
trueif this value represents the JSON literaltrue
-
isFalse
public boolean isFalse()Detects whether this value represents the JSON literalfalse.- Returns:
trueif this value represents the JSON literalfalse
-
isNull
public boolean 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
public int 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
public long 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
public float 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
public double 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
public boolean 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 for this object to the given writer.Single elements are passed directly to the given writer. Therefore, if the writer is not buffered, wrapping it in a
BufferedWritercan drastically improve writing performance.- Parameters:
writer- the writer to write this value to- 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 methodreadFrom(String)and to create a value that is equal to this object. -
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
public int hashCode() -
write
- Throws:
IOException
-