Top Description Fields Constructors Methods
com.google.gson

public final Class JsonPrimitive

extends JsonElement
Class Inheritance
Imports
com.google.gson.internal.LazilyParsedNumber, .NumberLimits, java.math.BigDecimal, .BigInteger, java.util.Objects

A class representing a JSON primitive value. A primitive value is either a String, a Java primitive, or a Java primitive wrapper type.

See the JsonElement documentation for details on how to convert JsonPrimitive and generally any JsonElement from and to JSON.

Authors
Inderjeet Singh, Joel Leitch

Field Summary

Modifier and TypeField and Description
private final Object

Constructor Summary

AccessConstructor and Description
public
JsonPrimitive(Boolean
the value to create the primitive with.
bool
)

Create a primitive containing a boolean value.

public
JsonPrimitive(Number
the value to create the primitive with.
number
)

Create a primitive containing a Number.

public
JsonPrimitive(String
the value to create the primitive with.
string
)

Create a primitive containing a String value.

public
JsonPrimitive(Character
the value to create the primitive with.
c
)

Create a primitive containing a character.

Method Summary

Modifier and TypeMethod and Description
public JsonPrimitive
deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Returns the same value as primitives are immutable.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this.

public BigDecimal
getAsBigDecimal()

Overrides com.google.gson.JsonElement.getAsBigDecimal.

Convenience method to get this element as a BigDecimal.

public BigInteger
getAsBigInteger()

Overrides com.google.gson.JsonElement.getAsBigInteger.

Convenience method to get this element as a BigInteger.

public boolean
getAsBoolean()

Overrides com.google.gson.JsonElement.getAsBoolean.

Convenience method to get this element as a boolean value.

public byte
getAsByte()

Overrides com.google.gson.JsonElement.getAsByte.

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

public char
getAsCharacter()

Overrides com.google.gson.JsonElement.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
getAsDouble()

Overrides com.google.gson.JsonElement.getAsDouble.

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

public float
getAsFloat()

Overrides com.google.gson.JsonElement.getAsFloat.

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

public int
getAsInt()

Overrides com.google.gson.JsonElement.getAsInt.

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

public long

Returns:

this element as a primitive long.
getAsLong
()

Overrides com.google.gson.JsonElement.getAsLong.

Convenience method to get this element as a primitive long.

public Number
getAsNumber()

Overrides com.google.gson.JsonElement.getAsNumber.

Convenience method to get this element as a Number.

public short
getAsShort()

Overrides com.google.gson.JsonElement.getAsShort.

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

public String
getAsString()

Overrides com.google.gson.JsonElement.getAsString.

Convenience method to get this element as a string value.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this object.

public boolean

Returns:

true if this primitive contains a boolean value, false otherwise.
isBoolean
()

Check whether this primitive contains a boolean value.

private static boolean
isIntegral(JsonPrimitive primitive)

Returns true if the specified number is an integral type (Long, Integer, Short, Byte, BigInteger)

public boolean

Returns:

true if this primitive contains a Number, false otherwise.
isNumber
()

Check whether this primitive contains a Number.

public boolean

Returns:

true if this primitive contains a String value, false otherwise.
isString
()

Check whether this primitive contains a String value.

Inherited from com.google.gson.JsonElement:
getAsJsonArraygetAsJsonNullgetAsJsonObjectgetAsJsonPrimitiveisJsonArrayisJsonNullisJsonObjectisJsonPrimitivetoString

Field Detail

valueback to summary
private final Object value

Constructor Detail

JsonPrimitiveback to summary
public JsonPrimitive(Boolean bool)

Create a primitive containing a boolean value.

Parameters
bool:Boolean

the value to create the primitive with.

Annotations
@SuppressWarnings:deprecation, UnnecessaryBoxedVariable
JsonPrimitiveback to summary
public JsonPrimitive(Number number)

Create a primitive containing a Number.

Parameters
number:Number

the value to create the primitive with.

Annotations
@SuppressWarnings:deprecation
JsonPrimitiveback to summary
public JsonPrimitive(String string)

Create a primitive containing a String value.

Parameters
string:String

the value to create the primitive with.

Annotations
@SuppressWarnings:deprecation
JsonPrimitiveback to summary
public JsonPrimitive(Character c)

Create a primitive containing a character. The character is turned into a one character String since JSON only supports String.

Parameters
c:Character

the value to create the primitive with.

Annotations
@SuppressWarnings:deprecation, UnnecessaryBoxedVariable

Method Detail

deepCopyback to summary
public JsonPrimitive deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Returns the same value as primitives are immutable.

Annotations
@Override
Since
2.8.2
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this. This method only considers the other object to be equal if it is an instance of JsonPrimitive and has an equal value.

Annotations
@Override
getAsBigDecimalback to summary
public BigDecimal getAsBigDecimal()

Overrides com.google.gson.JsonElement.getAsBigDecimal.

Doc from com.google.gson.JsonElement.getAsBigDecimal.

Convenience method to get this element as a BigDecimal.

Annotations
@Override
Exceptions
NumberFormatException:
if this element is not a valid BigDecimal.
getAsBigIntegerback to summary
public BigInteger getAsBigInteger()

Overrides com.google.gson.JsonElement.getAsBigInteger.

Doc from com.google.gson.JsonElement.getAsBigInteger.

Convenience method to get this element as a BigInteger.

Annotations
@Override
Exceptions
NumberFormatException:
if this element is not a valid BigInteger.
getAsBooleanback to summary
public boolean getAsBoolean()

Overrides com.google.gson.JsonElement.getAsBoolean.

Convenience method to get this element as a boolean value. If this primitive is not a boolean, the string value is parsed using Boolean#parseBoolean(String). This means "true" (ignoring case) is considered true and any other value is considered false.

Annotations
@Override
getAsByteback to summary
public byte getAsByte()

Overrides com.google.gson.JsonElement.getAsByte.

Doc from com.google.gson.JsonElement.getAsByte.

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

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid byte.
getAsCharacterback to summary
public char getAsCharacter()

Overrides com.google.gson.JsonElement.getAsCharacter.

Doc from com.google.gson.JsonElement.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.

Annotations
@Deprecated
@Override
Exceptions
UnsupportedOperationException:
if the string value of this primitive is empty.
getAsDoubleback to summary
public double getAsDouble()

Overrides com.google.gson.JsonElement.getAsDouble.

Doc from com.google.gson.JsonElement.getAsDouble.

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

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid double.
getAsFloatback to summary
public float getAsFloat()

Overrides com.google.gson.JsonElement.getAsFloat.

Doc from com.google.gson.JsonElement.getAsFloat.

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

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid float.
getAsIntback to summary
public int getAsInt()

Overrides com.google.gson.JsonElement.getAsInt.

Doc from com.google.gson.JsonElement.getAsInt.

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

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid integer.
getAsLongback to summary
public long getAsLong()

Overrides com.google.gson.JsonElement.getAsLong.

Convenience method to get this element as a primitive long.

Returns:long

this element as a primitive long.

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid long.
getAsNumberback to summary
public Number getAsNumber()

Overrides com.google.gson.JsonElement.getAsNumber.

Convenience method to get this element as a Number. If this primitive is a string, a lazily parsed Number is constructed which parses the string when any of its methods are called (which can lead to a NumberFormatException).

Annotations
@Override
Exceptions
UnsupportedOperationException:
if this primitive is neither a number nor a string.
getAsShortback to summary
public short getAsShort()

Overrides com.google.gson.JsonElement.getAsShort.

Doc from com.google.gson.JsonElement.getAsShort.

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

Annotations
@Override
Exceptions
NumberFormatException:
if the value contained is not a valid short.
getAsStringback to summary
public String getAsString()

Overrides com.google.gson.JsonElement.getAsString.

Doc from com.google.gson.JsonElement.getAsString.

Convenience method to get this element as a string value.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this object.

Annotations
@Override
isBooleanback to summary
public boolean isBoolean()

Check whether this primitive contains a boolean value.

Returns:boolean

true if this primitive contains a boolean value, false otherwise.

isIntegralback to summary
private static boolean isIntegral(JsonPrimitive primitive)

Returns true if the specified number is an integral type (Long, Integer, Short, Byte, BigInteger)

isNumberback to summary
public boolean isNumber()

Check whether this primitive contains a Number.

Returns:boolean

true if this primitive contains a Number, false otherwise.

isStringback to summary
public boolean isString()

Check whether this primitive contains a String value.

Returns:boolean

true if this primitive contains a String value, false otherwise.