java.lang.Object
com.google.gson.JsonElement
- Direct Known Subclasses:
JsonArray,JsonNull,JsonObject,JsonPrimitive
A class representing an element of JSON. It could either be a
JsonObject, a
JsonArray, a JsonPrimitive or a JsonNull.- Author:
- Inderjeet Singh, Joel Leitch
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract JsonElementdeepCopy()Returns a deep copy of this element.Convenience method to get this element as aBigDecimal.Convenience method to get this element as aBigInteger.booleanConvenience method to get this element as a boolean value.byteConvenience method to get this element as a primitive byte value.charDeprecated.This method is misleading, as it does not get this element as a char but rather as a string's first character.doubleConvenience method to get this element as a primitive double value.floatConvenience method to get this element as a primitive float value.intgetAsInt()Convenience method to get this element as a primitive integer value.Convenience method to get this element as aJsonArray.Convenience method to get this element as aJsonNull.Convenience method to get this element as aJsonObject.Convenience method to get this element as aJsonPrimitive.longConvenience method to get this element as a primitive long value.Convenience method to get this element as aNumber.shortConvenience method to get this element as a primitive short value.Convenience method to get this element as a string value.booleanProvides a check for verifying if this element is a JSON array or not.booleanProvides a check for verifying if this element represents a null value or not.booleanProvides a check for verifying if this element is a JSON object or not.booleanProvides a check for verifying if this element is a primitive or not.toString()Returns a String representation of this element.
-
Constructor Details
-
JsonElement
Deprecated.Creating customJsonElementsubclasses is highly discouraged and can lead to undefined behavior.
This constructor is only kept for backward compatibility.
-
-
Method Details
-
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
Convenience method to get this element as aJsonObject. If this element is of some other type, anIllegalStateExceptionwill 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
Convenience method to get this element as aJsonArray. If this element is of some other type, anIllegalStateExceptionwill 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
Convenience method to get this element as aJsonPrimitive. If this element is of some other type, anIllegalStateExceptionwill 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
Convenience method to get this element as aJsonNull. If this element is of some other type, anIllegalStateExceptionwill 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 aJsonPrimitiveorJsonArray.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.
-
getAsNumber
Convenience method to get this element as aNumber.- Returns:
- this element as a
Number. - Throws:
UnsupportedOperationException- if this element is not aJsonPrimitiveorJsonArray, or cannot be converted to a number.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.
-
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 aJsonPrimitiveorJsonArray.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid double.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid float.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid long.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid integer.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid byte.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.- Since:
- 1.3
-
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 aJsonPrimitiveorJsonArray, or if its string value is empty.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.- Since:
- 1.3
-
getAsBigDecimal
Convenience method to get this element as aBigDecimal.- Returns:
- this element as a
BigDecimal. - Throws:
UnsupportedOperationException- if this element is not aJsonPrimitiveorJsonArray.NumberFormatException- if this element is not a validBigDecimal.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.- Since:
- 1.2
-
getAsBigInteger
Convenience method to get this element as aBigInteger.- Returns:
- this element as a
BigInteger. - Throws:
UnsupportedOperationException- if this element is not aJsonPrimitiveorJsonArray.NumberFormatException- if this element is not a validBigInteger.IllegalStateException- if this element is of the typeJsonArraybut 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 aJsonPrimitiveorJsonArray.NumberFormatException- if the value contained is not a valid short.IllegalStateException- if this element is of the typeJsonArraybut contains more than a single element.
-
toString
Returns a String representation of this element.
-
JsonElementsubclasses is highly discouraged and can lead to undefined behavior.This constructor is only kept for backward compatibility.