public abstract class JsonValue
extends java.lang.Object
implements java.io.Serializable
The literals 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 of these classes.
Instances that represent JSON numbers, strings and
boolean values can be created using the static factory methods
valueOf(String)
, valueOf(long)
, valueOf(double)
, etc.
valueOf(float)
and valueOf(double)
throw a NumberFormatException
if the input is Inf or NaN.
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
Represents the JSON literal
false . |
static JsonValue |
NULL
Represents the JSON literal
null . |
static JsonValue |
TRUE
Represents the JSON literal
true . |
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. |
java.lang.Object |
asDsf()
Returns this JSON value as a DSF object, assuming that this value represents a DSF.
|
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. |
java.lang.String |
asString()
Returns this JSON value as String, assuming that this value represents a JSON string.
|
boolean |
equals(java.lang.Object object)
Indicates whether some other object is "equal to" this one according to the contract specified
in
Object.equals(Object) . |
static java.lang.String |
getEol()
Gets the newline charater(s).
|
abstract JsonType |
getType()
Gets the type of this JSON value.
|
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 |
readHjson(java.io.Reader reader)
Reads a Hjson value from the given reader.
|
static JsonValue |
readHjson(java.io.Reader reader,
HjsonOptions options)
Reads a Hjson value from the given reader.
|
static JsonValue |
readHjson(java.lang.String text)
Reads a Hjson value from the given string.
|
static JsonValue |
readHjson(java.lang.String text,
HjsonOptions options)
Reads a Hjson value from the given string.
|
static JsonValue |
readJSON(java.io.Reader reader)
Reads a JSON value from the given reader.
|
static JsonValue |
readJSON(java.lang.String text)
Reads a JSON value from the given string.
|
static void |
setEol(java.lang.String value)
Sets the newline charater(s).
|
java.lang.String |
toString()
Returns the JSON string for this value in its minimal form, without any additional whitespace.
|
java.lang.String |
toString(HjsonOptions options)
Returns the Hjson string for this value using the given formatting.
|
java.lang.String |
toString(Stringify format)
Returns the JSON/Hjson string for this value using the given formatting.
|
static JsonValue |
valueOf(boolean value)
Returns a JsonValue instance that represents the given
boolean value. |
static JsonValue |
valueOf(double value)
Returns a JsonValue instance that represents the given
double value. |
static JsonValue |
valueOf(float value)
Returns a JsonValue instance that represents the given
float value. |
static JsonValue |
valueOf(int value)
Returns a JsonValue instance that represents the given
int value. |
static JsonValue |
valueOf(long value)
Returns a JsonValue instance that represents the given
long value. |
static JsonValue |
valueOf(java.lang.String string)
Returns a JsonValue instance that represents the given string.
|
static JsonValue |
valueOfDsf(java.lang.Object value)
Returns a JsonValue instance that represents the given DSF value.
|
void |
writeTo(java.io.Writer writer)
Writes the JSON representation of this value to the given writer in its minimal form, without
any additional whitespace.
|
void |
writeTo(java.io.Writer writer,
HjsonOptions options)
Writes the Hjson representation of this value to the given writer.
|
void |
writeTo(java.io.Writer writer,
Stringify format)
Writes the JSON/Hjson representation of this value to the given writer using the given formatting.
|
public static final JsonValue TRUE
true
.public static final JsonValue FALSE
false
.public static final JsonValue NULL
null
.public static java.lang.String getEol()
public static void setEol(java.lang.String value)
value
- the eol valuepublic static JsonValue readHjson(java.io.Reader reader) throws java.io.IOException
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 Hjson value fromjava.io.IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid Hjsonpublic static JsonValue readHjson(java.lang.String text)
text
- the string that contains the Hjson valueParseException
- if the input is not valid Hjsonpublic static JsonValue readHjson(java.io.Reader reader, HjsonOptions options) throws java.io.IOException
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 Hjson value fromoptions
- the Hjson optionsjava.io.IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid Hjsonpublic static JsonValue readHjson(java.lang.String text, HjsonOptions options)
text
- the string that contains the Hjson valueoptions
- the Hjson optionsParseException
- if the input is not valid Hjsonpublic static JsonValue readJSON(java.io.Reader reader) throws java.io.IOException
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 fromjava.io.IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid JSONpublic static JsonValue readJSON(java.lang.String text)
text
- the string that contains the JSON valueParseException
- if the input is not valid JSONpublic static JsonValue valueOf(int value)
int
value.value
- the value to get a JSON representation forpublic static JsonValue valueOf(long value)
long
value.value
- the value to get a JSON representation forpublic static JsonValue valueOf(float value)
float
value.
Throws a NumberFormatException if the input parameter
is Inf or NaN.value
- the value to get a JSON representation forpublic static JsonValue valueOf(double value)
double
value.
Throws a NumberFormatException if the input parameter
is Inf or NaN.value
- the value to get a JSON representation forpublic static JsonValue valueOf(java.lang.String string)
string
- the string to get a JSON representation forpublic static JsonValue valueOf(boolean value)
boolean
value.value
- the value to get a JSON representation forpublic static JsonValue valueOfDsf(java.lang.Object value)
value
- the value to get a JSON representation forpublic abstract JsonType getType()
public 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.java.lang.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.java.lang.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
java.lang.UnsupportedOperationException
- if this value is not a JSON numberjava.lang.NumberFormatException
- 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
java.lang.UnsupportedOperationException
- if this value is not a JSON numberjava.lang.NumberFormatException
- 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
java.lang.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
java.lang.UnsupportedOperationException
- if this value is not a JSON numberpublic java.lang.String asString()
java.lang.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
java.lang.UnsupportedOperationException
- if this value is neither true
or false
public java.lang.Object asDsf()
java.lang.UnsupportedOperationException
- if this value is not a DSFpublic void writeTo(java.io.Writer writer) throws java.io.IOException
Writing performance can be improved by using a BufferedWriter
.
writer
- the writer to write this value tojava.io.IOException
- if an I/O error occurs in the writerpublic void writeTo(java.io.Writer writer, Stringify format) throws java.io.IOException
Writing performance can be improved by using a BufferedWriter
.
writer
- the writer to write this value toformat
- controls the formattingjava.io.IOException
- if an I/O error occurs in the writerpublic void writeTo(java.io.Writer writer, HjsonOptions options) throws java.io.IOException
Writing performance can be improved by using a BufferedWriter
.
writer
- the writer to write this value tooptions
- options for the Hjson formatjava.io.IOException
- if an I/O error occurs in the writerpublic java.lang.String toString()
readJSON(String)
and to
create a value that is equal to this object.toString
in class java.lang.Object
public java.lang.String toString(Stringify format)
format
- controls the formattingpublic java.lang.String toString(HjsonOptions options)
options
- options for the Hjson formatpublic boolean equals(java.lang.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.
equals
in class java.lang.Object
object
- the reference object with which to comparepublic int hashCode()
hashCode
in class java.lang.Object