Package com.google.gson
Class JsonElement
- java.lang.Object
-
- com.google.gson.JsonElement
-
- Direct Known Subclasses:
JsonArray
,JsonNull
,JsonObject
,JsonPrimitive
public abstract class JsonElement extends java.lang.Object
A class representing an element of Json. It could either be aJsonObject
, aJsonArray
, aJsonPrimitive
or aJsonNull
.
-
-
Constructor Summary
Constructors Constructor Description JsonElement()
-
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.java.math.BigDecimal
getAsBigDecimal()
convenience method to get this element as aBigDecimal
.java.math.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.java.lang.Number
getAsNumber()
convenience method to get this element as aNumber
.short
getAsShort()
convenience method to get this element as a primitive short value.java.lang.String
getAsString()
convenience method to get this element as a string value.boolean
isJsonArray()
provides check for verifying if this element is an array or not.boolean
isJsonNull()
provides check for verifying if this element represents a null value or not.boolean
isJsonObject()
provides check for verifying if this element is a Json object or not.boolean
isJsonPrimitive()
provides check for verifying if this element is a primitive or not.java.lang.String
toString()
Returns a String representation of this element.
-
-
-
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 check for verifying if this element is an array or not.- Returns:
- true if this element is of type
JsonArray
, false otherwise.
-
isJsonObject
public boolean isJsonObject()
provides 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 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 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 the element is of some other type, aIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonObject()
first.- Returns:
- get this element as a
JsonObject
. - Throws:
java.lang.IllegalStateException
- if the element is of another type.
-
getAsJsonArray
public JsonArray getAsJsonArray()
convenience method to get this element as aJsonArray
. If the element is of some other type, aIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonArray()
first.- Returns:
- get this element as a
JsonArray
. - Throws:
java.lang.IllegalStateException
- if the element is of another type.
-
getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
convenience method to get this element as aJsonPrimitive
. If the element is of some other type, aIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonPrimitive()
first.- Returns:
- get this element as a
JsonPrimitive
. - Throws:
java.lang.IllegalStateException
- if the element is of another type.
-
getAsJsonNull
public JsonNull getAsJsonNull()
convenience method to get this element as aJsonNull
. If the element is of some other type, aIllegalStateException
will result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJsonNull()
first.- Returns:
- get this element as a
JsonNull
. - Throws:
java.lang.IllegalStateException
- if the element is of another type.- Since:
- 1.2
-
getAsBoolean
public boolean getAsBoolean()
convenience method to get this element as a boolean value.- Returns:
- get this element as a primitive boolean value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid boolean value.java.lang.IllegalStateException
- if the element is of the typeJsonArray
but contains more than a single element.
-
getAsNumber
public java.lang.Number getAsNumber()
convenience method to get this element as aNumber
.- Returns:
- get this element as a
Number
. - Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid number.java.lang.IllegalStateException
- if the element is of the typeJsonArray
but contains more than a single element.
-
getAsString
public java.lang.String getAsString()
convenience method to get this element as a string value.- Returns:
- get this element as a string value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid string value.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive double value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid double value.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive float value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid float value.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive long value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid long value.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive integer value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid integer value.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive byte value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid byte value.java.lang.IllegalStateException
- if the 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 this element as a string or the first character of this array's first element as a string.- Returns:
- the first character of the string.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid string value.java.lang.IllegalStateException
- if the element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.3
-
getAsBigDecimal
public java.math.BigDecimal getAsBigDecimal()
convenience method to get this element as aBigDecimal
.- Returns:
- get this element as a
BigDecimal
. - Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
. * @throws NumberFormatException if the element is not a validBigDecimal
.java.lang.IllegalStateException
- if the element is of the typeJsonArray
but contains more than a single element.- Since:
- 1.2
-
getAsBigInteger
public java.math.BigInteger getAsBigInteger()
convenience method to get this element as aBigInteger
.- Returns:
- get this element as a
BigInteger
. - Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
.java.lang.NumberFormatException
- if the element is not a validBigInteger
.java.lang.IllegalStateException
- if the 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:
- get this element as a primitive short value.
- Throws:
java.lang.ClassCastException
- if the element is of not aJsonPrimitive
and is not a valid short value.java.lang.IllegalStateException
- if the element is of the typeJsonArray
but contains more than a single element.
-
toString
public java.lang.String toString()
Returns a String representation of this element.- Overrides:
toString
in classjava.lang.Object
-
-