@NotMutable @ThreadSafety(level=COMPLETELY_THREADSAFE) public final class JSONNumber extends JSONValue
BigDecimal
.
toString()
method
(or appended to the provided buffer for the toString(StringBuilder)
method). If a JSON number is created from a long
or double
value, then the Java string representation of that value (as obtained from
the String.valueOf
method) will be used as the string representation
for the number. If a JSON number is created from a BigDecimal
value,
then the Java string representation will be obtained via that value's
toPlainString
method.
Constructor and Description |
---|
JSONNumber(java.math.BigDecimal value)
Creates a new JSON number with the provided value.
|
JSONNumber(double value)
Creates a new JSON number with the provided value.
|
JSONNumber(long value)
Creates a new JSON number with the provided value.
|
JSONNumber(java.lang.String stringRepresentation)
Creates a new JSON number from the provided string representation.
|
Modifier and Type | Method and Description |
---|---|
void |
appendToJSONBuffer(JSONBuffer buffer)
Appends this value to the provided JSON buffer.
|
void |
appendToJSONBuffer(java.lang.String fieldName,
JSONBuffer buffer)
Appends a field with the given name and this value to the provided JSON
buffer.
|
boolean |
equals(JSONValue v,
boolean ignoreFieldNameCase,
boolean ignoreValueCase,
boolean ignoreArrayOrder)
Indicates whether this JSON value is considered equal to the provided JSON
value, subject to the specified constraints.
|
boolean |
equals(java.lang.Object o)
Indicates whether the provided object is equal to this JSON value.
|
java.math.BigDecimal |
getValue()
Retrieves the value of this JSON number as a
BigDecimal . |
double |
getValueAsDouble()
Retrieves the value of this JSON number as a
double . |
java.lang.Integer |
getValueAsInteger()
Retrieves the value of this JSON number as an
Integer , but only if
the value can be losslessly represented as an integer. |
java.lang.Long |
getValueAsLong()
Retrieves the value of this JSON number as a
Long , but only if
the value can be losslessly represented as a long. |
int |
hashCode()
Retrieves a hash code for this JSON value.
|
java.lang.String |
toNormalizedString()
Retrieves a normalized string representation of this number as it should
appear in a JSON object.
|
java.lang.String |
toNormalizedString(boolean ignoreFieldNameCase,
boolean ignoreValueCase,
boolean ignoreArrayOrder)
Retrieves a normalized string representation of this number as it should
appear in a JSON object.
|
void |
toNormalizedString(java.lang.StringBuilder buffer)
Appends a normalized string representation of this number as it should
appear in a JSON object to the provided buffer.
|
void |
toNormalizedString(java.lang.StringBuilder buffer,
boolean ignoreFieldNameCase,
boolean ignoreValueCase,
boolean ignoreArrayOrder)
Appends a normalized string representation of this number as it should
appear in a JSON object to the provided buffer.
|
JSONNumber |
toNormalizedValue(boolean ignoreFieldNameCase,
boolean ignoreValueCase,
boolean ignoreArrayOrder)
Retrieves a normalized representation of this value using the provided
settings.
|
java.lang.String |
toSingleLineString()
Retrieves a single-line string representation of this number as it should
appear in a JSON object.
|
void |
toSingleLineString(java.lang.StringBuilder buffer)
Appends a single-line string representation of this number as it should
appear in a JSON object to the provided buffer.
|
java.lang.String |
toString()
Retrieves a string representation of this number as it should appear in a
JSON object.
|
void |
toString(java.lang.StringBuilder buffer)
Appends a string representation of this number as it should appear in a
JSON object to the provided buffer.
|
public JSONNumber(long value)
value
- The value for this JSON number.public JSONNumber(double value)
value
- The value for this JSON number.public JSONNumber(@NotNull java.math.BigDecimal value)
value
- The value for this JSON number. It must not be
null
.public JSONNumber(@NotNull java.lang.String stringRepresentation) throws JSONException
stringRepresentation
- The string representation to parse as a JSON
number. It must not be null
.JSONException
- If the provided string cannot be parsed as a valid
JSON number.@NotNull public java.math.BigDecimal getValue()
BigDecimal
.BigDecimal
.@Nullable public java.lang.Integer getValueAsInteger()
Integer
, but only if
the value can be losslessly represented as an integer.Integer
value for this JSON number, or null
if
the value has a fractional component or is outside the range of a
Java integer.@Nullable public java.lang.Long getValueAsLong()
Long
, but only if
the value can be losslessly represented as a long.Long
value for this JSON number, or null
if
the value has a fractional component or is outside the range of a
Java long.public double getValueAsDouble()
double
. Note that if
the BigDecimal
value is outside the range that can be represented
by a double
, then the value returned may be converted to positive
or negative infinity. Further, the double
value that is returned
could potentially have less precision than the associated BigDouble
value.double
value for this JSON number.public int hashCode()
public boolean equals(@Nullable java.lang.Object o)
public boolean equals(@NotNull JSONValue v, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder)
equals
in class JSONValue
v
- The JSON value for which to make the
determination. It must not be null
.ignoreFieldNameCase
- Indicates whether to ignore differences in the
capitalization of JSON field names.ignoreValueCase
- Indicates whether to ignore differences in
the capitalization of JSON values that
represent strings.ignoreArrayOrder
- Indicates whether to ignore differences in the
order of elements in JSON arrays.true
if this JSON value is considered equal to the
provided JSON value (subject to the specified constraints), or
false
if not.@NotNull public java.lang.String toString()
public void toString(@NotNull java.lang.StringBuilder buffer)
@NotNull public java.lang.String toSingleLineString()
toSingleLineString
in class JSONValue
public void toSingleLineString(@NotNull java.lang.StringBuilder buffer)
toSingleLineString
in class JSONValue
buffer
- The buffer to which the information should be appended.@NotNull public java.lang.String toNormalizedString()
toNormalizedString
in class JSONValue
public void toNormalizedString(@NotNull java.lang.StringBuilder buffer)
toNormalizedString
in class JSONValue
buffer
- The buffer to which the information should be appended.@NotNull public java.lang.String toNormalizedString(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder)
toNormalizedString
in class JSONValue
ignoreFieldNameCase
- Indicates whether field names should be
treated in a case-sensitive (if false
)
or case-insensitive (if true
) manner.ignoreValueCase
- Indicates whether string field values should
be treated in a case-sensitive (if
false
) or case-insensitive (if
true
) manner.ignoreArrayOrder
- Indicates whether the order of elements in an
array should be considered significant (if
false
) or insignificant (if
true
).public void toNormalizedString(@NotNull java.lang.StringBuilder buffer, boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder)
toNormalizedString
in class JSONValue
buffer
- The buffer to which the information should be
appended.ignoreFieldNameCase
- Indicates whether field names should be
treated in a case-sensitive (if false
)
or case-insensitive (if true
) manner.ignoreValueCase
- Indicates whether string field values should
be treated in a case-sensitive (if
false
) or case-insensitive (if
true
) manner.ignoreArrayOrder
- Indicates whether the order of elements in an
array should be considered significant (if
false
) or insignificant (if
true
).@NotNull public JSONNumber toNormalizedValue(boolean ignoreFieldNameCase, boolean ignoreValueCase, boolean ignoreArrayOrder)
toNormalizedValue
in class JSONValue
ignoreFieldNameCase
- Indicates whether field names should be
treated in a case-sensitive (if false
)
or case-insensitive (if true
) manner.ignoreValueCase
- Indicates whether string field values should
be treated in a case-sensitive (if
false
) or case-insensitive (if
true
) manner.ignoreArrayOrder
- Indicates whether the order of elements in an
array should be considered significant (if
false
) or insignificant (if
true
).public void appendToJSONBuffer(@NotNull JSONBuffer buffer)
appendToJSONBuffer
in class JSONValue
buffer
- The JSON buffer to which this value should be appended.public void appendToJSONBuffer(@NotNull java.lang.String fieldName, @NotNull JSONBuffer buffer)
appendToJSONBuffer
in class JSONValue
fieldName
- The name to use for the field.buffer
- The JSON buffer to which this value should be appended.