Share via


JsonObject Class

public final class JsonObject
extends JsonElement

Class representing the JSON object type.

Constructor Summary

Constructor Description
JsonObject()

Default constructor.

Method Summary

Modifier and Type Method and Description
static JsonObject fromJson(JsonReader jsonReader)

Deserializes a JSON object from a JsonReader.

JsonElement getProperty(String key)

Gets the JsonElement value corresponding to the specified key.

boolean hasProperty(String key)

Checks whether a property with the specified key exists in the JSON object.

boolean isObject()

Indicates whether the element is an object.

JsonElement removeProperty(String key)

Removes the JsonElement value corresponding to the specified key.

JsonObject setProperty(String key, boolean element)

Sets the boolean value corresponding to the specified key.

JsonObject setProperty(String key, JsonElement element)

Sets the JsonElement value corresponding to the specified key.

JsonObject setProperty(String key, Number element)

Sets the number value corresponding to the specified key.

JsonObject setProperty(String key, String element)

Sets the string value corresponding to the specified key.

int size()

The number of properties in the JSON object.

JsonWriter toJson(JsonWriter jsonWriter)
String toJsonString()

Methods inherited from JsonElement

Methods inherited from java.lang.Object

Constructor Details

JsonObject

public JsonObject()

Default constructor.

Method Details

fromJson

public static JsonObject fromJson(JsonReader jsonReader)

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 nextToken() will be called to begin reading.

After ensuring the JsonReader has begun reading, if the current token is not 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.

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.

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.

isObject

public boolean isObject()

Indicates whether the element is an object.

Overrides:

JsonObject.isObject()

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.

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, 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.

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.

size

public int size()

The number of properties in the JSON object.

Returns:

The number of properties in the JSON object.

toJson

public JsonWriter toJson(JsonWriter jsonWriter)

Parameters:

jsonWriter

Throws:

toJsonString

public String toJsonString()

Throws:

Applies to