Class JsonObject

java.lang.Object
com.azure.json.models.JsonElement
com.azure.json.models.JsonObject
All Implemented Interfaces:
JsonSerializable<JsonElement>

public final class JsonObject extends JsonElement
Class representing the JSON object type.
  • Constructor Details

    • JsonObject

      public JsonObject()
      Default constructor.
  • Method Details

    • hasProperty

      public boolean hasProperty(String key)
      Checks whether a property with the specified key exists in the JSON object.
      Parameters:
      key - The key to check for.
      Returns:
      Whether a property with the specified key exists in the JSON object.
    • getProperty

      public JsonElement getProperty(String key)
      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

      public JsonObject setProperty(String key, JsonElement element)
      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 the key or element is null.
    • setProperty

      public JsonObject setProperty(String key, boolean element)
      Sets the boolean 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 boolean value to set the property to.
      Returns:
      The updated JsonObject object.
    • setProperty

      public JsonObject setProperty(String key, Number element)
      Sets the number value corresponding to the specified key. If the key already exists, the value will be overwritten.

      If element is null this will set the property to JsonNull.

      Parameters:
      key - The key of the property to set.
      element - The number value to set the property to.
      Returns:
      The updated JsonObject object.
    • setProperty

      public JsonObject setProperty(String key, String element)
      Sets the string value corresponding to the specified key. If the key already exists, the value will be overwritten.

      If element is null this will set the property to JsonNull.

      Parameters:
      key - The key of the property to set.
      element - The string value to set the property to.
      Returns:
      The updated JsonObject object.
    • removeProperty

      public JsonElement removeProperty(String key)
      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 the key is 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: JsonElement
      Indicates whether the element is an object.
      Overrides:
      isObject in class JsonElement
      Returns:
      Whether the element is an object.
    • toJson

      public JsonWriter toJson(JsonWriter jsonWriter) throws IOException
      Description copied from interface: JsonSerializable
      Writes the object to the passed JsonWriter.

      The contract for writing JSON to JsonWriter is that the object being written will handle opening and closing its own JSON object. So, for objects calling out to other JsonSerializable objects for serialization, they'll write the field name only then pass the JsonWriter to the other JsonSerializable object. 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 JsonWriter where the JSON was written.
      Throws:
      IOException - If the object fails to be written to the jsonWriter.
    • fromJson

      public static JsonObject fromJson(JsonReader jsonReader) throws IOException
      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, an IllegalStateException will 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 not JsonToken.START_OBJECT.
    • toJsonString

      public String toJsonString() throws IOException
      Description copied from interface: JsonSerializable
      Convenience method for writing the JsonSerializable to a JSON string.
      Returns:
      The JSON string representing the object.
      Throws:
      IOException - If the object fails to be written as a JSON string.