Class JsonObject
- All Implemented Interfaces:
JsonSerializable<JsonElement>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonObjectfromJson(JsonReader jsonReader) Deserializes a JSON object from a JsonReader.getProperty(String key) Gets the JsonElement value corresponding to the specified key.booleanisObject()Indicates whether the element is an object.removeProperty(String key) Removes the JsonElement value corresponding to the specified key.setProperty(String key, JsonElement element) Sets the JsonElement value corresponding to the specified key.intsize()The number of properties in the JSON object.toJson(JsonWriter jsonWriter) Writes the object to the passedJsonWriter.Convenience method for writing theJsonSerializableto a JSON string.Methods inherited from class com.azure.json.models.JsonElement
isArray, isBoolean, isNull, isNumber, isStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.azure.json.JsonSerializable
toJson, toJson, toJsonBytes
-
Constructor Details
-
JsonObject
public JsonObject()Default constructor.
-
-
Method Details
-
getProperty
Gets the JsonElement value corresponding to the specified key. If the key doesn't exist, null will be returned.- Parameters:
key- The key of the property to get.- Returns:
- The JsonElement value corresponding to the specified key, or null if the property doesn't exist.
-
setProperty
Sets the JsonElement value corresponding to the specified key. If the key already exists, the value will be overwritten.- Parameters:
key- The key of the property to set.element- The JsonElement value to set the property to.- Returns:
- The updated JsonObject object.
- Throws:
NullPointerException- If thekeyorelementis null.
-
removeProperty
Removes the JsonElement value corresponding to the specified key. If the key doesn't exist, null will be returned.- Parameters:
key- The key of the property to remove.- Returns:
- The JsonElement value corresponding to the specified key, or null if the property doesn't exist.
- Throws:
NullPointerException- If thekeyis null.
-
size
public int size()The number of properties in the JSON object.- Returns:
- The number of properties in the JSON object.
-
isObject
public boolean isObject()Description copied from class:JsonElementIndicates whether the element is an object.- Overrides:
isObjectin classJsonElement- Returns:
- Whether the element is an object.
-
toJson
Description copied from interface:JsonSerializableWrites the object to the passedJsonWriter.The contract for writing JSON to
JsonWriteris that the object being written will handle opening and closing its own JSON object. So, for objects calling out to otherJsonSerializableobjects for serialization, they'll write the field name only then pass theJsonWriterto the otherJsonSerializableobject. This way objects writing JSON will be self-encapsulated for writing properly formatted JSON.- Parameters:
jsonWriter- Where the object's JSON will be written.- Returns:
- The
JsonWriterwhere the JSON was written. - Throws:
IOException- If the object fails to be written to thejsonWriter.
-
fromJson
Deserializes a JSON object from a JsonReader.If the JsonReader's current token is null, it is assumed the JsonReader hasn't begun reading and
JsonReader.nextToken()will be called to begin reading.After ensuring the JsonReader has begun reading, if the current token is not
JsonToken.START_OBJECT, anIllegalStateExceptionwill be thrown. Otherwise, a JSON object representing the object will be created and returned.- Parameters:
jsonReader- The JsonReader to deserialize from.- Returns:
- The deserialized JSON object.
- Throws:
IOException- If an error occurs while deserializing the JSON object.IllegalStateException- If the current token is notJsonToken.START_OBJECT.
-
toJsonString
Description copied from interface:JsonSerializableConvenience method for writing theJsonSerializableto a JSON string.- Returns:
- The JSON string representing the object.
- Throws:
IOException- If the object fails to be written as a JSON string.
-