java.lang.Object
com.google.gson.JsonElement
com.google.gson.JsonPrimitive
A class representing a JSON primitive value. A primitive value
is either a String, a Java primitive, or a Java primitive
wrapper type.
- Author:
- Inderjeet Singh, Joel Leitch
-
Constructor Summary
ConstructorsConstructorDescriptionJsonPrimitive(Boolean bool) Create a primitive containing a boolean value.Create a primitive containing a character.JsonPrimitive(Number number) Create a primitive containing aNumber.JsonPrimitive(String string) Create a primitive containing a String value. -
Method Summary
Modifier and TypeMethodDescriptiondeepCopy()Returns the same value as primitives are immutable.booleanReturns whether the other object is equal to this.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.longConvenience method to get this element as a primitive long.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.inthashCode()Returns the hash code of this object.booleanCheck whether this primitive contains a boolean value.booleanisNumber()Check whether this primitive contains a Number.booleanisString()Check whether this primitive contains a String value.Methods inherited from class com.google.gson.JsonElement
getAsJsonArray, getAsJsonNull, getAsJsonObject, getAsJsonPrimitive, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive, toString
-
Constructor Details
-
JsonPrimitive
Create a primitive containing a boolean value.- Parameters:
bool- the value to create the primitive with.
-
JsonPrimitive
Create a primitive containing aNumber.- Parameters:
number- the value to create the primitive with.
-
JsonPrimitive
Create a primitive containing a String value.- Parameters:
string- the value to create the primitive with.
-
JsonPrimitive
Create a primitive containing a character. The character is turned into a one character String since JSON only supports String.- Parameters:
c- the value to create the primitive with.
-
-
Method Details
-
deepCopy
Returns the same value as primitives are immutable.- Specified by:
deepCopyin classJsonElement- Since:
- 2.8.2
-
isBoolean
public boolean isBoolean()Check whether this primitive contains a boolean value.- Returns:
- true if this primitive contains a boolean value, false otherwise.
-
getAsBoolean
public boolean getAsBoolean()Convenience method to get this element as a boolean value. If this primitive is not a boolean, the string value is parsed usingBoolean.parseBoolean(String). This means"true"(ignoring case) is consideredtrueand any other value is consideredfalse.- Overrides:
getAsBooleanin classJsonElement- Returns:
- this element as a primitive boolean value.
-
isNumber
public boolean isNumber()Check whether this primitive contains a Number.- Returns:
- true if this primitive contains a Number, false otherwise.
-
getAsNumber
Convenience method to get this element as aNumber. If this primitive is a string, a lazily parsedNumberis constructed which parses the string when any of its methods are called (which can lead to aNumberFormatException).- Overrides:
getAsNumberin classJsonElement- Returns:
- this element as a
Number. - Throws:
UnsupportedOperationException- if this primitive is neither a number nor a string.
-
isString
public boolean isString()Check whether this primitive contains a String value.- Returns:
- true if this primitive contains a String value, false otherwise.
-
getAsString
Description copied from class:JsonElementConvenience method to get this element as a string value.- Overrides:
getAsStringin classJsonElement- Returns:
- this element as a string value.
-
getAsDouble
public double getAsDouble()Description copied from class:JsonElementConvenience method to get this element as a primitive double value.- Overrides:
getAsDoublein classJsonElement- Returns:
- this element as a primitive double value.
- Throws:
NumberFormatException- if the value contained is not a valid double.
-
getAsBigDecimal
Description copied from class:JsonElementConvenience method to get this element as aBigDecimal.- Overrides:
getAsBigDecimalin classJsonElement- Returns:
- this element as a
BigDecimal. - Throws:
NumberFormatException- if this element is not a validBigDecimal.
-
getAsBigInteger
Description copied from class:JsonElementConvenience method to get this element as aBigInteger.- Overrides:
getAsBigIntegerin classJsonElement- Returns:
- this element as a
BigInteger. - Throws:
NumberFormatException- if this element is not a validBigInteger.
-
getAsFloat
public float getAsFloat()Description copied from class:JsonElementConvenience method to get this element as a primitive float value.- Overrides:
getAsFloatin classJsonElement- Returns:
- this element as a primitive float value.
- Throws:
NumberFormatException- if the value contained is not a valid float.
-
getAsLong
public long getAsLong()Convenience method to get this element as a primitive long.- Overrides:
getAsLongin classJsonElement- Returns:
- this element as a primitive long.
- Throws:
NumberFormatException- if the value contained is not a valid long.
-
getAsShort
public short getAsShort()Description copied from class:JsonElementConvenience method to get this element as a primitive short value.- Overrides:
getAsShortin classJsonElement- Returns:
- this element as a primitive short value.
- Throws:
NumberFormatException- if the value contained is not a valid short.
-
getAsInt
public int getAsInt()Description copied from class:JsonElementConvenience method to get this element as a primitive integer value.- Overrides:
getAsIntin classJsonElement- Returns:
- this element as a primitive integer value.
- Throws:
NumberFormatException- if the value contained is not a valid integer.
-
getAsByte
public byte getAsByte()Description copied from class:JsonElementConvenience method to get this element as a primitive byte value.- Overrides:
getAsBytein classJsonElement- Returns:
- this element as a primitive byte value.
- Throws:
NumberFormatException- if the value contained is not a valid byte.
-
getAsCharacter
Deprecated.This method is misleading, as it does not get this element as a char but rather as a string's first character.Description copied from class:JsonElementConvenience method to get the first character of the string value of this element.- Overrides:
getAsCharacterin classJsonElement- Returns:
- the first character of the string value.
- Throws:
UnsupportedOperationException- if the string value of this primitive is empty.
-
hashCode
public int hashCode()Returns the hash code of this 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 ofJsonPrimitiveand has an equal value.
-