Top Description Constructors Methods
com.google.gson

public abstract Class JsonElement

extends Object
Class Inheritance
Known Direct Subclasses
com.google.gson.JsonNull, com.google.gson.JsonPrimitive, com.google.gson.JsonObject, com.google.gson.JsonArray
Imports
com.google.errorprone.annotations.CanIgnoreReturnValue, com.google.gson.internal.Streams, com.google.gson.stream.JsonWriter, java.io.IOException, java.math.BigDecimal, .BigInteger

A class representing an element of JSON. It could either be a JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.

This class provides multiple getAs methods which allow

Converting JsonElement from / to JSON

There are two ways to parse JSON data as a JsonElement: To convert a JsonElement to JSON either JsonElement.toString() or the method Gson#toJson(JsonElement) and its overloads can be used.

It is also possible to obtain the TypeAdapter for JsonElement from a Gson instance and then use it for conversion from and to JSON:

TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);

JsonElement value = adapter.fromJson("{}");
String json = adapter.toJson(value);

JsonElement as JSON data

JsonElement can also be treated as JSON data, allowing to deserialize from a JsonElement and serializing to a JsonElement. The Gson class offers these methods for this: The TypeAdapter class provides corresponding methods as well:
Authors
Inderjeet Singh, Joel Leitch

Constructor Summary

AccessConstructor and Description
public
JsonElement()
Deprecated Creating custom JsonElement subclasses is highly discouraged and can lead to undefined behavior.
This constructor is only kept for backward compatibility.

Method Summary

Modifier and TypeMethod and Description
public abstract JsonElement
deepCopy()

Returns a deep copy of this element.

public BigDecimal

Returns:

this element as a BigDecimal.
getAsBigDecimal
()

Convenience method to get this element as a BigDecimal.

public BigInteger

Returns:

this element as a BigInteger.
getAsBigInteger
()

Convenience method to get this element as a BigInteger.

public boolean

Returns:

this element as a primitive boolean value.
getAsBoolean
()

Convenience method to get this element as a boolean value.

public byte

Returns:

this element as a primitive byte value.
getAsByte
()

Convenience method to get this element as a primitive byte value.

public char

Returns:

the first character of the string value.
getAsCharacter
()
Deprecated This method is misleading, as it does not get this element as a char but rather as a string's first character.

Convenience method to get the first character of the string value of this element.

public double

Returns:

this element as a primitive double value.
getAsDouble
()

Convenience method to get this element as a primitive double value.

public float

Returns:

this element as a primitive float value.
getAsFloat
()

Convenience method to get this element as a primitive float value.

public int

Returns:

this element as a primitive integer value.
getAsInt
()

Convenience method to get this element as a primitive integer value.

public JsonArray

Returns:

this element as a JsonArray.
getAsJsonArray
()

Convenience method to get this element as a JsonArray.

public JsonNull

Returns:

this element as a JsonNull.
getAsJsonNull
()

Convenience method to get this element as a JsonNull.

public JsonObject

Returns:

this element as a JsonObject.
getAsJsonObject
()

Convenience method to get this element as a JsonObject.

public JsonPrimitive

Returns:

this element as a JsonPrimitive.
getAsJsonPrimitive
()

Convenience method to get this element as a JsonPrimitive.

public long

Returns:

this element as a primitive long value.
getAsLong
()

Convenience method to get this element as a primitive long value.

public Number

Returns:

this element as a Number.
getAsNumber
()

Convenience method to get this element as a Number.

public short

Returns:

this element as a primitive short value.
getAsShort
()

Convenience method to get this element as a primitive short value.

public String

Returns:

this element as a string value.
getAsString
()

Convenience method to get this element as a string value.

public boolean

Returns:

true if this element is of type JsonArray, false otherwise.
isJsonArray
()

Provides a check for verifying if this element is a JSON array or not.

public boolean

Returns:

true if this element is of type JsonNull, false otherwise.
isJsonNull
()

Provides a check for verifying if this element represents a null value or not.

public boolean

Returns:

true if this element is of type JsonObject, false otherwise.
isJsonObject
()

Provides a check for verifying if this element is a JSON object or not.

public boolean

Returns:

true if this element is of type JsonPrimitive, false otherwise.
isJsonPrimitive
()

Provides a check for verifying if this element is a primitive or not.

public String
toString()

Overrides java.lang.Object.toString.

Converts this element to a JSON string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

JsonElementback to summary
public JsonElement()

Deprecated

Creating custom JsonElement subclasses is highly discouraged and can lead to undefined behavior.
This constructor is only kept for backward compatibility.

Annotations
@Deprecated

Method Detail

deepCopyback to summary
public abstract JsonElement deepCopy()

Returns a deep copy of this element. Immutable elements like primitives and nulls are not copied.

Since
2.8.2
getAsBigDecimalback to summary
public BigDecimal getAsBigDecimal()

Convenience method to get this element as a BigDecimal.

Returns:BigDecimal

this element as a BigDecimal.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if this element is not a valid BigDecimal.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
Since
1.2
getAsBigIntegerback to summary
public BigInteger getAsBigInteger()

Convenience method to get this element as a BigInteger.

Returns:BigInteger

this element as a BigInteger.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if this element is not a valid BigInteger.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
Since
1.2
getAsBooleanback to summary
public boolean getAsBoolean()

Convenience method to get this element as a boolean value.

Returns:boolean

this element as a primitive boolean value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsByteback to summary
public byte getAsByte()

Convenience method to get this element as a primitive byte value.

Returns:byte

this element as a primitive byte value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid byte.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
Since
1.3
getAsCharacterback to summary
public char getAsCharacter()

Deprecated

This method is misleading, as it does not get this element as a char but rather as a string's first character.

Convenience method to get the first character of the string value of this element.

Returns:char

the first character of the string value.

Annotations
@Deprecated
Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray, or if its string value is empty.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
Since
1.3
getAsDoubleback to summary
public double getAsDouble()

Convenience method to get this element as a primitive double value.

Returns:double

this element as a primitive double value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid double.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsFloatback to summary
public float getAsFloat()

Convenience method to get this element as a primitive float value.

Returns:float

this element as a primitive float value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid float.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsIntback to summary
public int getAsInt()

Convenience method to get this element as a primitive integer value.

Returns:int

this element as a primitive integer value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid integer.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsJsonArrayback to summary
public JsonArray getAsJsonArray()

Convenience method to get this element as a JsonArray. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonArray() first.

Returns:JsonArray

this element as a JsonArray.

Exceptions
IllegalStateException:
if this element is of another type.
getAsJsonNullback to summary
public JsonNull getAsJsonNull()

Convenience method to get this element as a JsonNull. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonNull() first.

Returns:JsonNull

this element as a JsonNull.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalStateException:
if this element is of another type.
Since
1.2
getAsJsonObjectback to summary
public JsonObject getAsJsonObject()

Convenience method to get this element as a JsonObject. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonObject() first.

Returns:JsonObject

this element as a JsonObject.

Exceptions
IllegalStateException:
if this element is of another type.
getAsJsonPrimitiveback to summary
public JsonPrimitive getAsJsonPrimitive()

Convenience method to get this element as a JsonPrimitive. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonPrimitive() first.

Returns:JsonPrimitive

this element as a JsonPrimitive.

Exceptions
IllegalStateException:
if this element is of another type.
getAsLongback to summary
public long getAsLong()

Convenience method to get this element as a primitive long value.

Returns:long

this element as a primitive long value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid long.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsNumberback to summary
public Number getAsNumber()

Convenience method to get this element as a Number.

Returns:Number

this element as a Number.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray, or cannot be converted to a number.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsShortback to summary
public short getAsShort()

Convenience method to get this element as a primitive short value.

Returns:short

this element as a primitive short value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
NumberFormatException:
if the value contained is not a valid short.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
getAsStringback to summary
public String getAsString()

Convenience method to get this element as a string value.

Returns:String

this element as a string value.

Exceptions
UnsupportedOperationException:
if this element is not a JsonPrimitive or JsonArray.
IllegalStateException:
if this element is of the type JsonArray but contains more than a single element.
isJsonArrayback to summary
public boolean isJsonArray()

Provides a check for verifying if this element is a JSON array or not.

Returns:boolean

true if this element is of type JsonArray, false otherwise.

isJsonNullback to summary
public boolean isJsonNull()

Provides a check for verifying if this element represents a null value or not.

Returns:boolean

true if this element is of type JsonNull, false otherwise.

Since
1.2
isJsonObjectback to summary
public boolean isJsonObject()

Provides a check for verifying if this element is a JSON object or not.

Returns:boolean

true if this element is of type JsonObject, false otherwise.

isJsonPrimitiveback to summary
public boolean isJsonPrimitive()

Provides a check for verifying if this element is a primitive or not.

Returns:boolean

true if this element is of type JsonPrimitive, false otherwise.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Converts this element to a JSON string.

For example:

JsonObject object = new JsonObject();
object.add("a", JsonNull.INSTANCE);
JsonArray array = new JsonArray();
array.add(1);
object.add("b", array);

String json = object.toString();
// json: {"a":null,"b":[1]}
If this element or any nested elements contain NaN or Infinity that value is written to JSON, even though the JSON specification does not permit these values.

To customize formatting or to directly write to an Appendable instead of creating an intermediate String first, use Gson.toJson(JsonElement, ...).

To get the contained String value (without enclosing " and without escaping), use getAsString() instead:

JsonPrimitive jsonPrimitive = new JsonPrimitive("with \" quote");
String json = jsonPrimitive.toString();
// json: "with \" quote"
String value = jsonPrimitive.getAsString();
// value: with " quote
Annotations
@Override