- java.lang.Object
-
- com.google.gson.JsonElement
-
- Direct Known Subclasses:
JsonArray
,JsonNull
,JsonObject
,JsonPrimitive
public abstract class JsonElement extends Object
A class representing an element of JSON. It could either be aJsonObject
, aJsonArray
, aJsonPrimitive
or aJsonNull
.- Author:
- Inderjeet Singh, Joel Leitch
-
-
Constructor Summary
Constructors Constructor Description JsonElement()
Deprecated.Creating customJsonElement
subclasses is highly discouraged and can lead to undefined behavior.
This constructor is only kept for backward compatibility.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract JsonElement
deepCopy()
Returns a deep copy of this element.BigDecimal
getAsBigDecimal()
Convenience method to get this element as aBigDecimal
.BigInteger
getAsBigInteger()
Convenience method to get this element as aBigInteger
.boolean
getAsBoolean()
Convenience method to get this element as a boolean value.byte
getAsByte()
Convenience method to get this element as a primitive byte value.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.double
getAsDouble()
Convenience method to get this element as a primitive double value.float
getAsFloat()
Convenience method to get this element as a primitive float value.int
getAsInt()
Convenience method to get this element as a primitive integer value.JsonArray
getAsJsonArray()
Convenience method to get this element as aJsonArray
.JsonNull
getAsJsonNull()
Convenience method to get this element as aJsonNull
.JsonObject
getAsJsonObject()
Convenience method to get this element as aJsonObject
.JsonPrimitive
getAsJsonPrimitive()
Convenience method to get this element as aJsonPrimitive
.long
getAsLong()
Convenience method to get this element as a primitive long value.Number
getAsNumber()
Convenience method to get this element as aNumber
.short
getAsShort()
Convenience method to get this element as a primitive short value.String
getAsString()
Convenience method to get this element as a string value.boolean
isJsonArray()
Provides a check for verifying if this element is a JSON array or not.boolean
isJsonNull()
Provides a check for verifying if this element represents a null value or not.boolean
isJsonObject()
Provides a check for verifying if this element is a JSON object or not.boolean
isJsonPrimitive()
Provides a check for verifying if this element is a primitive or not.String
toString()
Returns a String representation of this element.
-
-
-
Constructor Detail
-
JsonElement
@Deprecated public JsonElement()
Deprecated.Creating customJsonElement
subclasses is highly discouraged and can lead to undefined behavior.
This constructor is only kept for backward compatibility.
-
-
Method Detail
-
deepCopy
public abstract JsonElement deepCopy()
Returns a deep copy of this element. Immutable elements like primitives and nulls are not copied.- Since:
- 2.8.2
-
isJsonArray
public boolean isJsonArray()
Provides a check for verifying if this element is a JSON array or not.- Returns:
- true if this element is of type
JsonArray
, false otherwise.
-
isJsonObject
public boolean isJsonObject()
Provides a check for verifying if this element is a JSON object or not.- Returns:
- true if this element is of type
JsonObject
, false otherwise.
-
isJsonPrimitive
public boolean isJsonPrimitive()
Provides a check for verifying if this element is a primitive or not.- Returns:
- true if this element is of type
JsonPrimitive
, false otherwise.
-
isJsonNull
public boolean isJsonNull()
Provides a check for verifying if this element represents a null value or not.- Returns:
- true if this element is of type
JsonNull
, false otherwise. - Since:
- 1.2
-
getAsJsonObject
public JsonObject getAsJsonObject()
Convenience method to get this element as aJsonObject
. If this element is of some other type, anIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonObject()
first.- Returns:
- this element as a
JsonObject
. - Throws:
IllegalStateException
- if this element is of another type.
-
getAsJsonArray
public JsonArray getAsJsonArray()
Convenience method to get this element as aJsonArray
. If this element is of some other type, anIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonArray()
first.- Returns:
- this element as a
JsonArray
. - Throws:
IllegalStateException
- if this element is of another type.
-
getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
Convenience method to get this element as aJsonPrimitive
. If this element is of some other type, anIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonPrimitive()
first.- Returns:
- this element as a
JsonPrimitive
. - Throws:
IllegalStateException
- if this element is of another type.
-
getAsJsonNull
public JsonNull getAsJsonNull()
Convenience method to get this element as aJsonNull
. If this element is of some other type, anIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonNull()
first.- Returns:
- this element as a
JsonNull
. - Throws:
IllegalStateException
- if this element is of another type.- Since:
- 1.2
-
getAsBoolean
public boolean getAsBoolean()
Convenience method to get this element as a boolean value.- Returns:
- this element as a primitive boolean value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsNumber
public Number getAsNumber()
Convenience method to get this element as aNumber
.- Returns:
- this element as a
Number
. - Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
, or cannot be converted to a number.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsString
public String getAsString()
Convenience method to get this element as a string value.- Returns:
- this element as a string value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsDouble
public double getAsDouble()
Convenience method to get this element as a primitive double value.- Returns:
- this element as a primitive double value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid double.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsFloat
public float getAsFloat()
Convenience method to get this element as a primitive float value.- Returns:
- this element as a primitive float value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid float.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsLong
public long getAsLong()
Convenience method to get this element as a primitive long value.- Returns:
- this element as a primitive long value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid long.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsInt
public int getAsInt()
Convenience method to get this element as a primitive integer value.- Returns:
- this element as a primitive integer value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid integer.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
getAsByte
public byte getAsByte()
Convenience method to get this element as a primitive byte value.- Returns:
- this element as a primitive byte value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid byte.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.3
-
getAsCharacter
@Deprecated 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:
- the first character of the string value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
, or if its string value is empty.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.3
-
getAsBigDecimal
public BigDecimal getAsBigDecimal()
Convenience method to get this element as aBigDecimal
.- Returns:
- this element as a
BigDecimal
. - Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if this element is not a validBigDecimal
.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.2
-
getAsBigInteger
public BigInteger getAsBigInteger()
Convenience method to get this element as aBigInteger
.- Returns:
- this element as a
BigInteger
. - Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if this element is not a validBigInteger
.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.2
-
getAsShort
public short getAsShort()
Convenience method to get this element as a primitive short value.- Returns:
- this element as a primitive short value.
- Throws:
UnsupportedOperationException
- if this element is not aJsonPrimitive
orJsonArray
.NumberFormatException
- if the value contained is not a valid short.IllegalStateException
- if this element is of the typeJsonArray
but contains more than a single element.
-
-