Interface JsonNumber
- All Superinterfaces:
JsonValue
The interface that represents JSON number, an arbitrary-precision
number represented in base 10 using decimal digits.
A JsonNumber can be created by parsing JSON text using either
Json.parse(String) or Json.parse(char[]).
When a JSON number is parsed, a JsonNumber object is created
as long as the input number text adheres to the JSON number
syntax.
Alternatively, of(int), of(long), of(double),
or of(String) can be used to obtain a JsonNumber.
The value of the JsonNumber can be retrieved as an int with
asInt(), as a long with asLong(), or as a
double with asDouble(). toString() can be used to
return the string representation of the JsonNumber.
- API Note:
- To avoid precision loss when converting
JsonNumbers to Java types, or when convertingJsonNumbers outside the range oflongordouble, usetoString()to create arbitrary-precision Java objects, for example,new BigDecimal(jsonNumber.toString()) // or if an integral number is preferred new BigInteger(jsonNumber.toString()) // for cases with an exponent or zero fractional part new BigDecimal(jsonNumber.toString()).toBigIntegerExact() - Since:
- 28
- External Specifications
-
Method Summary
Modifier and TypeMethodDescriptiondoubleasDouble()Returns adoubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException.intasInt()Returns anintif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException.longasLong()Returns alongif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException.static JsonNumberof(double num) Creates aJsonNumberfrom the givendoublevalue.static JsonNumberof(int num) Creates aJsonNumberfrom the givenintvalue.static JsonNumberof(long num) Creates aJsonNumberfrom the givenlongvalue.static JsonNumberCreates aJsonNumberfrom the givenStringvalue.toString()Returns the string representation of thisJsonNumber.Methods declared in interface JsonValue
asBoolean, asList, asMap, asString, get, get, tryGet, tryValueModifier and TypeMethodDescriptiondefault booleanReturns thebooleanvalue represented by thisJsonValueif it is an instance ofJsonBoolean; otherwise, throws aJsonValueException.asList()Returns an unmodifiable list of theJsonValues if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.asMap()Returns an unmodifiable map ofStringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.default StringasString()Returns theStringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException.default JsonValueget(int index) Returns theJsonValueassociated with the given index if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.default JsonValueReturns theJsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.Returns anOptionalcontaining the value of a given member of thisJsonObject, or an emptyOptionalif the member is absent; throwsJsonValueExceptionif thisJsonValueis not aJsonObject.tryValue()Returns anOptionalcontaining thisJsonValueif it is not an instance ofJsonNull, otherwise an emptyOptional.
-
Method Details
-
asInt
int asInt()Returns anintif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException. ThisJsonValuemust be a JSON number that represents a whole number and that is within the rangeInteger.MIN_VALUEtoInteger.MAX_VALUE, inclusive. This is true even if the JSON number contains an exponent or a fractional part consisting of all zeroes. For example, the JSON numbers "123.0" and "1.23e2" both produce anintvalue of123. AJsonValueExceptionis thrown when the numeric value cannot be represented as anint; for example, the JSON number "5.5".- Specified by:
asIntin interfaceJsonValue- Returns:
- an
intif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonNumberis not representable as anint.
-
asLong
long asLong()Returns alongif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException. ThisJsonValuemust be a JSON number that represents a whole number and that is within the rangeLong.MIN_VALUEtoLong.MAX_VALUE, inclusive. This is true even if the JSON number contains an exponent or a fractional part consisting of all zeroes. For example, the JSON numbers "123.0" and "1.23e2" both produce alongvalue of123. AJsonValueExceptionis thrown when the numeric value cannot be represented as along; for example, the JSON number "5.5".- Specified by:
asLongin interfaceJsonValue- Returns:
- a
longif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonNumberis not representable as along.
-
asDouble
double asDouble()Returns adoubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException.- Specified by:
asDoublein interfaceJsonValue- API Note:
- Callers of this method should be aware of the potential loss in precision or
magnitude when a
JsonNumberis converted to adouble. A JSON number may be rounded to the nearest representabledoublevalue, and a JSON number with more than about 15 decimal digits may lose precision. A JSON number with a magnitude larger than about 1.8 × 10308 cannot be represented as a finitedouble, and attempting to convert such a number will result inJsonValueException. (This differs fromDouble.parseDouble, which will returnDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYfor such cases.) This method will never returnDouble.NaN. However, this method will properly convert and return negative zero (-0.0). To handle numbers of almost arbitrary precision and magnitude, consider converting toBigDecimalusingnew BigDecimal(jsonNumber.toString()). Note thatBigDecimalcannot represent negative zero. - Returns:
- a
doubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonNumberis not representable as a finitedouble.
-
of
Creates aJsonNumberfrom the givendoublevalue. The string representation of theJsonNumbercreated is produced by applyingDouble.toString(double)onnum.- Parameters:
num- the givendoublevalue.- Returns:
- a
JsonNumbercreated from thedoublevalue - Throws:
IllegalArgumentException- if the givendoublevalue is not a finite floating-point value (NaN,positive infinity, ornegative infinity).
-
of
Creates aJsonNumberfrom the givenintvalue. The string representation of theJsonNumbercreated is produced by applyingInteger.toString(int)onnum.- Parameters:
num- the givenintvalue.- Returns:
- a
JsonNumbercreated from theintvalue
-
of
Creates aJsonNumberfrom the givenlongvalue. The string representation of theJsonNumbercreated is produced by applyingLong.toString(long)onnum.- Parameters:
num- the givenlongvalue.- Returns:
- a
JsonNumbercreated from thelongvalue
-
of
Creates aJsonNumberfrom the givenStringvalue. The string representation of theJsonNumbercreated is equivalent tonumwith any leading or trailing JSON insignificant whitespaces removed.- Parameters:
num- the givenStringvalue.- Returns:
- a
JsonNumbercreated from theStringvalue - Throws:
IllegalArgumentException- ifnumis not a valid string representation of aJsonNumber.NullPointerException- ifnumisnull
-
toString
String toString()Returns the string representation of thisJsonNumber. If thisJsonNumberis created by parsing a JSON number in a JSON text, it preserves the string representation in the JSON text, regardless of its precision or range. For example, a JSON number like "3.141592653589793238462643383279" in the JSON text will be returned exactly as it appears. If thisJsonNumberis created via one of the factory methods, such asof(double), then the string representation is specified by the factory method.
-