Top Description Fields Constructors Methods
com.google.gson

public final Class JsonObject

extends JsonElement
Class Inheritance
Imports
com.google.errorprone.annotations.CanIgnoreReturnValue, com.google.gson.internal.LinkedTreeMap, java.util.Map, .Set

A class representing an object type in JSON. An object consists of name-value pairs where names are strings, and values are any other type of JsonElement. This allows for a creating a tree of JsonElements. The member elements of this object are maintained in order they were added. This class does not support null values. If null is provided as value argument to any of the methods, it is converted to a JsonNull.

JsonObject does not implement the Map interface, but a Map view of it can be obtained with asMap().

See the JsonElement documentation for details on how to convert JsonObject and generally any JsonElement from and to JSON.

Authors
Inderjeet Singh, Joel Leitch

Field Summary

Modifier and TypeField and Description
private final LinkedTreeMap<String, JsonElement>

Constructor Summary

AccessConstructor and Description
public
JsonObject()

Creates an empty JsonObject.

Method Summary

Modifier and TypeMethod and Description
public void
add(String
name of the member.
property
,
JsonElement
the member object.
value
)

Adds a member, which is a name-value pair, to self.

public void
addProperty(String
name of the member.
property
,
String
the string value associated with the member.
value
)

Convenience method to add a string member.

public void
addProperty(String
name of the member.
property
,
Number
the number value associated with the member.
value
)

Convenience method to add a number member.

public void
addProperty(String
name of the member.
property
,
Boolean
the boolean value associated with the member.
value
)

Convenience method to add a boolean member.

public void
addProperty(String
name of the member.
property
,
Character
the char value associated with the member.
value
)

Convenience method to add a char member.

public Map<String, JsonElement>

Returns:

mutable Map view
asMap
()

Returns a mutable Map view of this JsonObject.

public JsonObject
deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Creates a deep copy of this element and all its children.

public Set<Map.Entry<String, JsonElement>>

Returns:

a set of members of this object.
entrySet
()

Returns a set of members of this object.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this.

public JsonElement

Returns:

the member matching the name, or null if no such member exists.
get
(String
name of the member that is being requested.
memberName
)

Returns the member with the specified name.

public JsonArray

Returns:

the JsonArray corresponding to the specified member, or null if no member with this name exists.
getAsJsonArray
(String
name of the member being requested.
memberName
)

Convenience method to get the specified member as a JsonArray.

public JsonObject

Returns:

the JsonObject corresponding to the specified member, or null if no member with this name exists.
getAsJsonObject
(String
name of the member being requested.
memberName
)

Convenience method to get the specified member as a JsonObject.

public JsonPrimitive

Returns:

the JsonPrimitive corresponding to the specified member, or null if no member with this name exists.
getAsJsonPrimitive
(String
name of the member being requested.
memberName
)

Convenience method to get the specified member as a JsonPrimitive.

public boolean

Returns:

true if there is a member with the specified name, false otherwise.
has
(String
name of the member that is being checked for presence.
memberName
)

Convenience method to check if a member with the specified name is present in this object.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this object.

public boolean

Returns:

true if the number of key/value pairs in the object is zero.
isEmpty
()

Returns true if the number of key/value pairs in the object is zero.

public Set<String>

Returns:

a set of member keys as Strings
keySet
()

Returns a set of members key values.

public JsonElement

Returns:

the JsonElement object that is being removed, or null if no member with this name exists.
remove
(String
name of the member that should be removed.
property
)

Removes the property from this object.

public int

Returns:

the number of key/value pairs in the object.
size
()

Returns the number of key/value pairs in the object.

Inherited from com.google.gson.JsonElement:
getAsBigDecimalgetAsBigIntegergetAsBooleangetAsBytegetAsCharactergetAsDoublegetAsFloatgetAsIntgetAsJsonArraygetAsJsonNullgetAsJsonObjectgetAsJsonPrimitivegetAsLonggetAsNumbergetAsShortgetAsStringisJsonArrayisJsonNullisJsonObjectisJsonPrimitivetoString

Field Detail

membersback to summary
private final LinkedTreeMap<String, JsonElement> members

Constructor Detail

JsonObjectback to summary
public JsonObject()

Creates an empty JsonObject.

Annotations
@SuppressWarnings:deprecation

Method Detail

addback to summary
public void add(String property, JsonElement value)

Adds a member, which is a name-value pair, to self. The name must be a String, but the value can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements rooted at this node.

Parameters
property:String

name of the member.

value:JsonElement

the member object.

addPropertyback to summary
public void addProperty(String property, String value)

Convenience method to add a string member. The specified value is converted to a JsonPrimitive of String.

Parameters
property:String

name of the member.

value:String

the string value associated with the member.

addPropertyback to summary
public void addProperty(String property, Number value)

Convenience method to add a number member. The specified value is converted to a JsonPrimitive of Number.

Parameters
property:String

name of the member.

value:Number

the number value associated with the member.

addPropertyback to summary
public void addProperty(String property, Boolean value)

Convenience method to add a boolean member. The specified value is converted to a JsonPrimitive of Boolean.

Parameters
property:String

name of the member.

value:Boolean

the boolean value associated with the member.

addPropertyback to summary
public void addProperty(String property, Character value)

Convenience method to add a char member. The specified value is converted to a JsonPrimitive of Character.

Parameters
property:String

name of the member.

value:Character

the char value associated with the member.

asMapback to summary
public Map<String, JsonElement> asMap()

Returns a mutable Map view of this JsonObject. Changes to the Map are visible in this JsonObject and the other way around.

The Map does not permit null keys or values. Unlike JsonObject's null handling, a NullPointerException is thrown when trying to add null. Use JsonNull for JSON null values.

Returns:Map<String, JsonElement>

mutable Map view

Since
2.10
deepCopyback to summary
public JsonObject deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Creates a deep copy of this element and all its children.

Annotations
@Override
Since
2.8.2
entrySetback to summary
public Set<Map.Entry<String, JsonElement>> entrySet()

Returns a set of members of this object. The set is ordered, and the order is in which the elements were added.

Returns:Set<Map.Entry<String, JsonElement>>

a set of members of this object.

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this. This method only considers the other object to be equal if it is an instance of JsonObject and has equal members, ignoring order.

Annotations
@Override
getback to summary
public JsonElement get(String memberName)

Returns the member with the specified name.

Parameters
memberName:String

name of the member that is being requested.

Returns:JsonElement

the member matching the name, or null if no such member exists.

getAsJsonArrayback to summary
public JsonArray getAsJsonArray(String memberName)

Convenience method to get the specified member as a JsonArray.

Parameters
memberName:String

name of the member being requested.

Returns:JsonArray

the JsonArray corresponding to the specified member, or null if no member with this name exists.

Exceptions
ClassCastException:
if the member is not of type JsonArray.
getAsJsonObjectback to summary
public JsonObject getAsJsonObject(String memberName)

Convenience method to get the specified member as a JsonObject.

Parameters
memberName:String

name of the member being requested.

Returns:JsonObject

the JsonObject corresponding to the specified member, or null if no member with this name exists.

Exceptions
ClassCastException:
if the member is not of type JsonObject.
getAsJsonPrimitiveback to summary
public JsonPrimitive getAsJsonPrimitive(String memberName)

Convenience method to get the specified member as a JsonPrimitive.

Parameters
memberName:String

name of the member being requested.

Returns:JsonPrimitive

the JsonPrimitive corresponding to the specified member, or null if no member with this name exists.

Exceptions
ClassCastException:
if the member is not of type JsonPrimitive.
hasback to summary
public boolean has(String memberName)

Convenience method to check if a member with the specified name is present in this object.

Parameters
memberName:String

name of the member that is being checked for presence.

Returns:boolean

true if there is a member with the specified name, false otherwise.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this object. This method calculates the hash code based on the members of this object, ignoring order.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Returns true if the number of key/value pairs in the object is zero.

Returns:boolean

true if the number of key/value pairs in the object is zero.

Since
2.10.1
keySetback to summary
public Set<String> keySet()

Returns a set of members key values.

Returns:Set<String>

a set of member keys as Strings

Since
2.8.1
removeback to summary
public JsonElement remove(String property)

Removes the property from this object.

Parameters
property:String

name of the member that should be removed.

Returns:JsonElement

the JsonElement object that is being removed, or null if no member with this name exists.

Annotations
@CanIgnoreReturnValue
Since
1.3
sizeback to summary
public int size()

Returns the number of key/value pairs in the object.

Returns:int

the number of key/value pairs in the object.

Since
2.7