Class JsonValue
- java.lang.Object
-
- com.restfb.json.JsonValue
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
JsonArray
,JsonObject
public abstract class JsonValue extends Object implements Serializable
Represents a JSON value. This can be a JSON object, an array, a number, a string, or one of the literals true, false, and null.The literals true, false, and null are represented by the constants
Json.TRUE
,Json.FALSE
, andJson.NULL
.JSON objects and arrays are represented by the subtypes
JsonObject
andJsonArray
. 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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
ARRAY_IS_NULL
protected static String
NAME_IS_NULL
protected static String
OBJECT_IS_NULL
protected static String
STRING_IS_NULL
protected static String
VALUE_IS_NULL
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArray
asArray()
Returns this JSON value asJsonArray
, assuming that this value represents a JSON array.boolean
asBoolean()
Returns this JSON value as aboolean
value, assuming that this value is eithertrue
orfalse
.double
asDouble()
Returns this JSON value as adouble
value, assuming that this value represents a JSON number.float
asFloat()
Returns this JSON value as afloat
value, assuming that this value represents a JSON number.int
asInt()
Returns this JSON value as anint
value, assuming that this value represents a JSON number that can be interpreted as Javaint
.long
asLong()
Returns this JSON value as along
value, assuming that this value represents a JSON number that can be interpreted as Javalong
.JsonObject
asObject()
Returns this JSON value asJsonObject
, assuming that this value represents a JSON object.String
asString()
Returns this JSON value as String, assuming that this value represents a JSON string.boolean
equals(Object object)
Indicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object)
.int
hashCode()
boolean
isArray()
Detects whether this value represents a JSON array.boolean
isBoolean()
Detects whether this value represents a boolean value.boolean
isFalse()
Detects whether this value represents the JSON literalfalse
.boolean
isNull()
Detects whether this value represents the JSON literalnull
.boolean
isNumber()
Detects whether this value represents a JSON number.boolean
isObject()
Detects whether this value represents a JSON object.boolean
isString()
Detects whether this value represents a JSON string.boolean
isTrue()
Detects whether this value represents the JSON literaltrue
.String
toString()
Returns the JSON string for this value in its minimal form, without any additional whitespace.String
toString(WriterConfig config)
Returns the JSON string for this value using the given formatting.void
writeTo(Writer writer)
Writes the JSON representation of this value to the given writer in its minimal form, without any additional whitespace.void
writeTo(Writer writer, WriterConfig config)
Writes the JSON representation of this value to the given writer using the given formatting.
-
-
-
Field Detail
-
STRING_IS_NULL
protected static final String STRING_IS_NULL
- See Also:
- Constant Field Values
-
OBJECT_IS_NULL
protected static final String OBJECT_IS_NULL
- See Also:
- Constant Field Values
-
NAME_IS_NULL
protected static final String NAME_IS_NULL
- See Also:
- Constant Field Values
-
VALUE_IS_NULL
protected static final String VALUE_IS_NULL
- See Also:
- Constant Field Values
-
ARRAY_IS_NULL
protected static final String ARRAY_IS_NULL
- See Also:
- Constant Field Values
-
-
Method Detail
-
isObject
public boolean isObject()
Detects whether this value represents a JSON object. If this is the case, this value is an instance ofJsonObject
.- Returns:
true
if 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:
true
if this value is an instance of JsonArray
-
isNumber
public boolean isNumber()
Detects whether this value represents a JSON number.- Returns:
true
if this value represents a JSON number
-
isString
public boolean isString()
Detects whether this value represents a JSON string.- Returns:
true
if this value represents a JSON string
-
isBoolean
public boolean isBoolean()
Detects whether this value represents a boolean value.- Returns:
true
if this value represents either the JSON literaltrue
orfalse
-
isTrue
public boolean isTrue()
Detects whether this value represents the JSON literaltrue
.- Returns:
true
if this value represents the JSON literaltrue
-
isFalse
public boolean isFalse()
Detects whether this value represents the JSON literalfalse
.- Returns:
true
if this value represents the JSON literalfalse
-
isNull
public boolean isNull()
Detects whether this value represents the JSON literalnull
.- Returns:
true
if this value represents the JSON literalnull
-
asObject
public JsonObject 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
public JsonArray 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 anint
value, 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 theInteger
range.- Returns:
- this value as
int
- Throws:
UnsupportedOperationException
- if this value is not a JSON numberNumberFormatException
- if this JSON number can not be interpreted asint
value
-
asLong
public long asLong()
Returns this JSON value as along
value, 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 theLong
range.- Returns:
- this value as
long
- Throws:
UnsupportedOperationException
- if this value is not a JSON numberNumberFormatException
- if this JSON number can not be interpreted aslong
value
-
asFloat
public float asFloat()
Returns this JSON value as afloat
value, 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
Float
range,Float.POSITIVE_INFINITY
orFloat.NEGATIVE_INFINITY
is 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 adouble
value, 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
Double
range,Double.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
is returned.- Returns:
- this value as
double
- Throws:
UnsupportedOperationException
- if this value is not a JSON number
-
asString
public String 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 aboolean
value, assuming that this value is eithertrue
orfalse
. If this is not the case, an exception is thrown.- Returns:
- this value as
boolean
- Throws:
UnsupportedOperationException
- if this value is neithertrue
orfalse
-
writeTo
public void writeTo(Writer writer) throws IOException
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
public void writeTo(Writer writer, WriterConfig config) throws IOException
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 ornull
for the minimal form- Throws:
IOException
- if an I/O error occurs in the writer
-
toString
public String 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
public String toString(WriterConfig config)
Returns the JSON string for this value using the given formatting.- Parameters:
config
- a configuration that controls the formatting ornull
for the minimal form- Returns:
- a JSON string that represents this value
-
equals
public boolean equals(Object object)
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.
-
-