JsonObject, a JsonArray, a JsonPrimitive or a JsonNull.
This class provides multiple getAs methods which allow
getAsString()
JsonElement subclasses in a convenient way, for example getAsJsonObject()
JsonElement from / to JSONJsonElement:
JsonParser, for example:
JsonObject jsonObject = JsonParser.parseString("{}").getAsJsonObject();
Gson.fromJson(..., JsonElement.class) JsonElement subclass, for example:
JsonObject jsonObject = gson.fromJson("{}", JsonObject.class);
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 dataJsonElement 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:
Gson.fromJson(JsonElement, ...) , for example:
JsonObject jsonObject = ...; MyClass myObj = gson.fromJson(jsonObject, MyClass.class);
Gson#toJsonTree(Object), for example:
MyClass myObj = ...; JsonElement json = gson.toJsonTree(myObj);
TypeAdapter class provides corresponding methods as well:
| Access | Constructor 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.
|
| Modifier and Type | Method and Description |
|---|---|
| public abstract JsonElement | |
| public BigDecimal | Returns: this element as aBigDecimal.Convenience method to get this element as a |
| public BigInteger | Returns: this element as aBigInteger.Convenience method to get this element as a |
| public boolean | Returns: this element as a primitive boolean value.Convenience method to get this element as a boolean value. |
| public byte | Returns: this element as a primitive byte value.Convenience method to get this element as a primitive byte value. |
| public char | Returns: the first character of the string value.
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.Convenience method to get this element as a primitive double value. |
| public float | Returns: this element as a primitive float value.Convenience method to get this element as a primitive float value. |
| public int | Returns: this element as a primitive integer value.Convenience method to get this element as a primitive integer value. |
| public JsonArray | Returns: this element as aJsonArray.Convenience method to get this element as a |
| public JsonNull | Returns: this element as aJsonNull.Convenience method to get this element as a |
| public JsonObject | Returns: this element as aJsonObject.Convenience method to get this element as a |
| public JsonPrimitive | Returns: this element as aJsonPrimitive.Convenience method to get this element as a |
| public long | Returns: this element as a primitive long value.Convenience method to get this element as a primitive long value. |
| public Number | |
| public short | Returns: this element as a primitive short value.Convenience method to get this element as a primitive short value. |
| public String | Returns: this element as a string value.Convenience method to get this element as a string value. |
| public boolean | Returns: true if this element is of typeJsonArray, false otherwise.Provides a check for verifying if this element is a JSON array or not. |
| public boolean | Returns: true if this element is of typeJsonNull, false otherwise.Provides a check for verifying if this element represents a null value or not. |
| public boolean | Returns: true if this element is of typeJsonObject, false otherwise.Provides a check for verifying if this element is a JSON object or not. |
| public boolean | Returns: true if this element is of typeJsonPrimitive, false otherwise.Provides a check for verifying if this element is a primitive or not. |
| public String |
| JsonElement | back to summary |
|---|---|
| public JsonElement()
Deprecated Creating custom
| |
| deepCopy | back to summary |
|---|---|
| public abstract JsonElement deepCopy() Returns a deep copy of this element. Immutable elements like primitives and nulls are not copied.
| |
| getAsBigDecimal | back to summary |
|---|---|
| public BigDecimal getAsBigDecimal() Convenience method to get this element as a
| |
| getAsBigInteger | back to summary |
|---|---|
| public BigInteger getAsBigInteger() Convenience method to get this element as a
| |
| getAsBoolean | back to summary |
|---|---|
| public boolean getAsBoolean() Convenience method to get this element as a boolean value.
| |
| getAsByte | back to summary |
|---|---|
| public byte getAsByte() Convenience method to get this element as a primitive byte value.
| |
| getAsCharacter | back 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.
| |
| getAsDouble | back to summary |
|---|---|
| public double getAsDouble() Convenience method to get this element as a primitive double value.
| |
| getAsFloat | back to summary |
|---|---|
| public float getAsFloat() Convenience method to get this element as a primitive float value.
| |
| getAsInt | back to summary |
|---|---|
| public int getAsInt() Convenience method to get this element as a primitive integer value.
| |
| getAsJsonArray | back to summary |
|---|---|
| public JsonArray getAsJsonArray() Convenience method to get this element as a
| |
| getAsJsonNull | back to summary |
|---|---|
| public JsonNull getAsJsonNull() Convenience method to get this element as a
| |
| getAsJsonObject | back to summary |
|---|---|
| public JsonObject getAsJsonObject() Convenience method to get this element as a
| |
| getAsJsonPrimitive | back to summary |
|---|---|
| public JsonPrimitive getAsJsonPrimitive() Convenience method to get this element as a
| |
| getAsLong | back to summary |
|---|---|
| public long getAsLong() Convenience method to get this element as a primitive long value.
| |
| getAsNumber | back to summary |
|---|---|
| public Number getAsNumber() Convenience method to get this element as a
| |
| getAsShort | back to summary |
|---|---|
| public short getAsShort() Convenience method to get this element as a primitive short value.
| |
| getAsString | back to summary |
|---|---|
| public String getAsString() Convenience method to get this element as a string value.
| |
| isJsonArray | back to summary |
|---|---|
| public boolean isJsonArray() Provides a check for verifying if this element is a JSON array or not.
| |
| isJsonNull | back to summary |
|---|---|
| public boolean isJsonNull() Provides a check for verifying if this element represents a null value or not.
| |
| isJsonObject | back to summary |
|---|---|
| public boolean isJsonObject() Provides a check for verifying if this element is a JSON object or not.
| |
| isJsonPrimitive | back to summary |
|---|---|
| public boolean isJsonPrimitive() Provides a check for verifying if this element is a primitive or not.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. 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 To get the contained String value (without enclosing
JsonPrimitive jsonPrimitive = new JsonPrimitive("with \" quote");
String json = jsonPrimitive.toString();
// json: "with \" quote"
String value = jsonPrimitive.getAsString();
// value: with " quote
| |