java.lang.Object
com.google.gson.JsonElement
com.google.gson.JsonObject
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()
.
- Author:
- Inderjeet Singh, Joel Leitch
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(String property, JsonElement value) Adds a member, which is a name-value pair, to self.void
addProperty
(String property, Boolean value) Convenience method to add a boolean member.void
addProperty
(String property, Character value) Convenience method to add a char member.void
addProperty
(String property, Number value) Convenience method to add a number member.void
addProperty
(String property, String value) Convenience method to add a string member.asMap()
Returns a mutableMap
view of thisJsonObject
.deepCopy()
Creates a deep copy of this element and all its children.entrySet()
Returns a set of members of this object.boolean
Returns whether the other object is equal to this.Returns the member with the specified name.getAsJsonArray
(String memberName) Convenience method to get the specified member as aJsonArray
.getAsJsonObject
(String memberName) Convenience method to get the specified member as aJsonObject
.getAsJsonPrimitive
(String memberName) Convenience method to get the specified member as aJsonPrimitive
.boolean
Convenience method to check if a member with the specified name is present in this object.int
hashCode()
Returns the hash code of this object.keySet()
Returns a set of members key values.Removes theproperty
from this object.int
size()
Returns the number of key/value pairs in the object.Methods inherited from class com.google.gson.JsonElement
getAsBigDecimal, getAsBigInteger, getAsBoolean, getAsByte, getAsCharacter, getAsDouble, getAsFloat, getAsInt, getAsJsonArray, getAsJsonNull, getAsJsonObject, getAsJsonPrimitive, getAsLong, getAsNumber, getAsShort, getAsString, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive, toString
-
Constructor Details
-
JsonObject
public JsonObject()Creates an empty JsonObject.
-
-
Method Details
-
deepCopy
Creates a deep copy of this element and all its children.- Specified by:
deepCopy
in classJsonElement
- Since:
- 2.8.2
-
add
Adds a member, which is a name-value pair, to self. The name must be a String, but the value can be an arbitraryJsonElement
, thereby allowing you to build a full tree of JsonElements rooted at this node.- Parameters:
property
- name of the member.value
- the member object.
-
remove
Removes theproperty
from this object.- Parameters:
property
- name of the member that should be removed.- Returns:
- the
JsonElement
object that is being removed, ornull
if no member with this name exists. - Since:
- 1.3
-
addProperty
Convenience method to add a string member. The specified value is converted to aJsonPrimitive
of String.- Parameters:
property
- name of the member.value
- the string value associated with the member.
-
addProperty
Convenience method to add a number member. The specified value is converted to aJsonPrimitive
of Number.- Parameters:
property
- name of the member.value
- the number value associated with the member.
-
addProperty
Convenience method to add a boolean member. The specified value is converted to aJsonPrimitive
of Boolean.- Parameters:
property
- name of the member.value
- the boolean value associated with the member.
-
addProperty
Convenience method to add a char member. The specified value is converted to aJsonPrimitive
of Character.- Parameters:
property
- name of the member.value
- the char value associated with the member.
-
entrySet
Returns a set of members of this object. The set is ordered, and the order is in which the elements were added.- Returns:
- a set of members of this object.
-
keySet
Returns a set of members key values.- Returns:
- a set of member keys as Strings
- Since:
- 2.8.1
-
size
public int size()Returns the number of key/value pairs in the object.- Returns:
- the number of key/value pairs in the object.
- Since:
- 2.7
-
has
Convenience method to check if a member with the specified name is present in this object.- Parameters:
memberName
- name of the member that is being checked for presence.- Returns:
- true if there is a member with the specified name, false otherwise.
-
get
Returns the member with the specified name.- Parameters:
memberName
- name of the member that is being requested.- Returns:
- the member matching the name, or
null
if no such member exists.
-
getAsJsonPrimitive
Convenience method to get the specified member as aJsonPrimitive
.- Parameters:
memberName
- name of the member being requested.- Returns:
- the
JsonPrimitive
corresponding to the specified member, ornull
if no member with this name exists. - Throws:
ClassCastException
- if the member is not of typeJsonPrimitive
.
-
getAsJsonArray
Convenience method to get the specified member as aJsonArray
.- Parameters:
memberName
- name of the member being requested.- Returns:
- the
JsonArray
corresponding to the specified member, ornull
if no member with this name exists. - Throws:
ClassCastException
- if the member is not of typeJsonArray
.
-
getAsJsonObject
Convenience method to get the specified member as aJsonObject
.- Parameters:
memberName
- name of the member being requested.- Returns:
- the
JsonObject
corresponding to the specified member, ornull
if no member with this name exists. - Throws:
ClassCastException
- if the member is not of typeJsonObject
.
-
asMap
Returns a mutableMap
view of thisJsonObject
. Changes to theMap
are visible in thisJsonObject
and the other way around.The
Map
does not permitnull
keys or values. UnlikeJsonObject
'snull
handling, aNullPointerException
is thrown when trying to addnull
. UseJsonNull
for JSON null values.- Returns:
- mutable
Map
view - Since:
- 2.10
-
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 ofJsonObject
and has equal members, ignoring order. -
hashCode
public int hashCode()Returns the hash code of this object. This method calculates the hash code based on the members of this object, ignoring order.
-