Interface JsonValue
- All Known Subinterfaces:
JsonArray, JsonBoolean, JsonNull, JsonNumber, JsonObject, JsonString
public sealed interface JsonValue
permits JsonString, JsonNumber, JsonObject, JsonArray, JsonBoolean, JsonNull
The interface that represents a JSON value. A
JsonValue represents
a syntactic element within a JSON text. The JsonValue subtypes
correspond to the JSON types, while JsonValue itself provides a uniform
interface for navigation, conversion, and generation.
JsonValue does not define any identity or value semantics.
Code that requires equality, hashing, or comparisons should use a
conversion
method to obtain a Java value upon which such operations are performed.
Instances of JsonValue are immutable and thread safe. See the
package specification
for an overview of parsing, accessing, converting, and generating JSON text.
- Since:
- 28
-
Method Summary
Modifier and TypeMethodDescriptiondefault booleanReturns thebooleanvalue represented by thisJsonValueif it is an instance ofJsonBoolean; otherwise, throws aJsonValueException.default doubleasDouble()Returns adoubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException.default intasInt()Returns anintif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException.asList()Returns an unmodifiable list of theJsonValues if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.default longasLong()Returns alongif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; 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.toString()Returns a String representation of thisJsonValuethat conforms to JSON syntax.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
-
toString
String toString()Returns a String representation of thisJsonValuethat conforms to JSON syntax. The returned string represents the same JSON value as this object and does not contain insignificant whitespace or line separators. The returned String may or may not be a canonical representation of the JSON value. If thisJsonValuewas obtained via one of the parsing methods on theJsonclass, the returned String is not necessarily an exact lexical match of the JSON text that was parsed. Subinterfaces may specify stronger preservation behavior for their corresponding JSON type.For a String representation suitable for display, use
Json.toDisplayString(JsonValue, String). -
asBoolean
default boolean asBoolean()Returns thebooleanvalue represented by thisJsonValueif it is an instance ofJsonBoolean; otherwise, throws aJsonValueException.- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonBooleanare expected to provide an implementation of this method. - Returns:
- the
booleanvalue represented by thisJsonValueif it is an instance ofJsonBoolean; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonBoolean.
-
asInt
default 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".- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonNumberare expected to provide an implementation of this method. - Returns:
- an
intif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonNumberor is not representable as anint.
-
asLong
default 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".- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonNumberare expected to provide an implementation of this method. - Returns:
- a
longif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonNumberor is not representable as along.
-
asDouble
default double asDouble()Returns adoubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException.- 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. - Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonNumberare expected to provide an implementation of this method. - Returns:
- a
doubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonNumberor is not representable as a finitedouble.
-
asString
Returns theStringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException. If thisJsonStringwas created by parsing a JSON text, any escaped characters in the original JSON text are converted to their unescaped form.- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonStringare expected to provide an implementation of this method. - Returns:
- the
Stringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonString.
-
asList
Returns an unmodifiable list of theJsonValues if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonArrayare expected to provide an implementation of this method. - Returns:
- an unmodifiable list of the
JsonValues if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonArray.
-
asMap
Returns an unmodifiable map ofStringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.- Implementation Requirements:
- The default implementation provided by
JsonValuethrowsJsonValueException. As such, implementors ofJsonObjectare expected to provide an implementation of this method. - Implementation Note:
- The JDK platform implementation of
JsonObjectpreserves the encounter order of members. When aJsonObjectis created by parsing, this corresponds to the order of members in the source JSON text. When created via theJsonObject.of(Map)factory method, the order follows the encounter order of the provided map. - Returns:
- an unmodifiable map of
StringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance ofJsonObject.
-
get
Returns theJsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.- Implementation Requirements:
- The default implementation obtains a
JsonValuewhich is the result of invokingasMap().get(name). Ifnameis absent,JsonValueExceptionis thrown. - Parameters:
name- the member name- Returns:
- the
JsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException - Throws:
NullPointerException- if the member name isnullJsonValueException- if thisJsonValueis not an instance of aJsonObjector there is no association with the member name
-
tryGet
Returns anOptionalcontaining the value of a given member of thisJsonObject, or an emptyOptionalif the member is absent; throwsJsonValueExceptionif thisJsonValueis not aJsonObject.- Implementation Requirements:
- The default implementation obtains an
Optional<JsonValue>by invokingasMap().get(name), which is then passed toOptional.ofNullable(T). - Parameters:
name- the member name- Returns:
- an
Optionalcontaining the value of a given member of thisJsonObject, or an emptyOptionalif the member is absent; throwsJsonValueExceptionif thisJsonValueis not aJsonObject - Throws:
NullPointerException- if the member name isnullJsonValueException- if thisJsonValueis not an instance of aJsonObject
-
get
Returns theJsonValueassociated with the given index if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.- Implementation Requirements:
- The default implementation obtains a
JsonValuewhich is the result of invokingasList().get(index). Ifindexis out of bounds,JsonValueExceptionis thrown. - Parameters:
index- the index of the array- Returns:
- the
JsonValueassociated with the given index if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException - Throws:
JsonValueException- if thisJsonValueis not an instance of aJsonArrayor the given index is out of bounds
-
tryValue
Returns anOptionalcontaining thisJsonValueif it is not an instance ofJsonNull, otherwise an emptyOptional.- Implementation Requirements:
- The default implementation returns
Optional.empty()if thisJsonValueis an instance ofJsonNull; otherwiseOptional.of(this). - Returns:
- an
Optionalcontaining thisJsonValueif it is not an instance ofJsonNull, otherwise an emptyOptional
-