public abstract class JsonValue extends Object implements Serializable
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.
Modifier and Type | Field and Description |
---|---|
static JsonValue |
FALSE
Deprecated.
Use
Json.FALSE instead |
static JsonValue |
NULL
Deprecated.
Use
Json.NULL instead |
static JsonValue |
TRUE
Deprecated.
Use
Json.TRUE instead |
Modifier and Type | Method and Description |
---|---|
JsonArray |
asArray()
Returns this JSON value as
JsonArray , assuming that this value represents a JSON array. |
boolean |
asBoolean()
Returns this JSON value as a
boolean value, assuming that this value is either
true or false . |
double |
asDouble()
Returns this JSON value as a
double value, assuming that this value represents a
JSON number. |
float |
asFloat()
Returns this JSON value as a
float value, assuming that this value represents a
JSON number. |
int |
asInt()
Returns this JSON value as an
int value, assuming that this value represents a
JSON number that can be interpreted as Java int . |
long |
asLong()
Returns this JSON value as a
long value, assuming that this value represents a
JSON number that can be interpreted as Java long . |
JsonObject |
asObject()
Returns this JSON value as
JsonObject , 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
in
Object.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 literal
false . |
boolean |
isNull()
Detects whether this value represents the JSON literal
null . |
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 literal
true . |
static JsonValue |
readFrom(Reader reader)
Deprecated.
Use
Json.parse(Reader) instead |
static JsonValue |
readFrom(String text)
Deprecated.
Use
Json.parse(String) instead |
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.
|
static JsonValue |
valueOf(boolean value)
Deprecated.
Use
Json.value() instead |
static JsonValue |
valueOf(double value)
Deprecated.
Use
Json.value() instead |
static JsonValue |
valueOf(float value)
Deprecated.
Use
Json.value() instead |
static JsonValue |
valueOf(int value)
Deprecated.
Use
Json.value() instead |
static JsonValue |
valueOf(long value)
Deprecated.
Use
Json.value() instead |
static JsonValue |
valueOf(String string)
Deprecated.
Use
Json.value() instead |
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.
|
@Deprecated public static final JsonValue TRUE
Json.TRUE
insteadtrue
.@Deprecated public static final JsonValue FALSE
Json.FALSE
insteadfalse
.@Deprecated public static final JsonValue NULL
Json.NULL
insteadnull
.@Deprecated public static JsonValue readFrom(Reader reader) throws IOException
Json.parse(Reader)
instead
Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
an additional BufferedReader
does not improve reading
performance.
reader
- the reader to read the JSON value fromIOException
- if an I/O error occurs in the readerParseException
- if the input is not valid JSON@Deprecated public static JsonValue readFrom(String text)
Json.parse(String)
insteadtext
- the string that contains the JSON valueParseException
- if the input is not valid JSON@Deprecated public static JsonValue valueOf(int value)
Json.value()
insteadint
value.value
- the value to get a JSON representation for@Deprecated public static JsonValue valueOf(long value)
Json.value()
insteadlong
value.value
- the value to get a JSON representation for@Deprecated public static JsonValue valueOf(float value)
Json.value()
insteadfloat
value.value
- the value to get a JSON representation for@Deprecated public static JsonValue valueOf(double value)
Json.value()
insteaddouble
value.value
- the value to get a JSON representation for@Deprecated public static JsonValue valueOf(String string)
Json.value()
insteadstring
- the string to get a JSON representation for@Deprecated public static JsonValue valueOf(boolean value)
Json.value()
insteadboolean
value.value
- the value to get a JSON representation forpublic boolean isObject()
JsonObject
.true
if this value is an instance of JsonObjectpublic boolean isArray()
JsonArray
.true
if this value is an instance of JsonArraypublic boolean isNumber()
true
if this value represents a JSON numberpublic boolean isString()
true
if this value represents a JSON stringpublic boolean isBoolean()
true
if this value represents either the JSON literal true
or
false
public boolean isTrue()
true
.true
if this value represents the JSON literal true
public boolean isFalse()
false
.true
if this value represents the JSON literal false
public boolean isNull()
null
.true
if this value represents the JSON literal null
public JsonObject asObject()
JsonObject
, assuming that this value represents a JSON
object. If this is not the case, an exception is thrown.UnsupportedOperationException
- if this value is not a JSON objectpublic JsonArray asArray()
JsonArray
, assuming that this value represents a JSON array.
If this is not the case, an exception is thrown.UnsupportedOperationException
- if this value is not a JSON arraypublic int asInt()
int
value, assuming that this value represents a
JSON number that can be interpreted as Java int
. 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 the Integer
range.
int
UnsupportedOperationException
- if this value is not a JSON numberNumberFormatException
- if this JSON number can not be interpreted as int
valuepublic long asLong()
long
value, assuming that this value represents a
JSON number that can be interpreted as Java long
. 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 the Long
range.
long
UnsupportedOperationException
- if this value is not a JSON numberNumberFormatException
- if this JSON number can not be interpreted as long
valuepublic float asFloat()
float
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
or
Float.NEGATIVE_INFINITY
is returned.
float
UnsupportedOperationException
- if this value is not a JSON numberpublic double asDouble()
double
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
or
Double.NEGATIVE_INFINITY
is returned.
double
UnsupportedOperationException
- if this value is not a JSON numberpublic String asString()
UnsupportedOperationException
- if this value is not a JSON stringpublic boolean asBoolean()
boolean
value, assuming that this value is either
true
or false
. If this is not the case, an exception is thrown.boolean
UnsupportedOperationException
- if this value is neither true
or false
public void writeTo(Writer writer) throws IOException
Writing performance can be improved by using a BufferedWriter
.
writer
- the writer to write this value toIOException
- if an I/O error occurs in the writerpublic void writeTo(Writer writer, WriterConfig config) throws IOException
Writing performance can be improved by using a BufferedWriter
.
writer
- the writer to write this value toconfig
- a configuration that controls the formatting or null
for the minimal formIOException
- if an I/O error occurs in the writerpublic String toString()
Json.parse(String)
and to
create a value that is equal to this object.public String toString(WriterConfig config)
config
- a configuration that controls the formatting or null
for the minimal formpublic boolean equals(Object object)
Object.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.
Copyright © 2018 Hazelcast, Inc.. All Rights Reserved.