Interface JsonObject
- All Superinterfaces:
JsonValue
The interface that represents JSON object.
A JsonObject can be created by parsing JSON text using either
Json.parse(String) or Json.parse(char[]).
Alternatively, of(Map) can be used to obtain a JsonObject.
Implementations of JsonObject cannot be created from sources that
contain duplicate member names. If duplicate names appear while parsing with
Json.parse(String), a JsonParseException is thrown. If duplicate
member names are detected while creating a JsonObject with of(Map),
an IllegalArgumentException is thrown.
- Since:
- 28
- External Specifications
-
Method Summary
Modifier and TypeMethodDescriptionasMap()Returns an unmodifiable map ofStringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.default JsonValueReturns theJsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.static JsonObjectReturns theJsonObjectcreated from the given map ofStringtoJsonValues.Returns anOptionalcontaining the value of a given member of thisJsonObject, or an emptyOptionalif the member is absent; throwsJsonValueExceptionif thisJsonValueis not aJsonObject.Methods declared in interface JsonValue
asBoolean, asDouble, asInt, asList, asLong, asString, get, toString, tryValueModifier 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.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.toString()Returns a String representation of thisJsonValuethat conforms to JSON syntax.tryValue()Returns anOptionalcontaining thisJsonValueif it is not an instance ofJsonNull, otherwise an emptyOptional.
-
Method Details
-
asMap
Returns an unmodifiable map ofStringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.- Specified by:
asMapin interfaceJsonValue- 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 theof(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
-
get
Returns theJsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.- Specified by:
getin interfaceJsonValue- 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 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.- Specified by:
tryGetin interfaceJsonValue- 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 isnull
-
of
Returns theJsonObjectcreated from the given map ofStringtoJsonValues.- Parameters:
map- the map ofJsonValues. Non-null.- Returns:
- the
JsonObjectcreated from the given map ofStringtoJsonValues - Throws:
IllegalArgumentException- if duplicate member names are given inmap, including when they are encountered while iterating over the mappings of anIdentityHashMap.NullPointerException- ifmapisnull, contains any keys that arenull, or contains any values that arenull.
-