- java.lang.Object
-
- com.google.gson.JsonElement
-
- com.google.gson.JsonPrimitive
-
public final class JsonPrimitive extends JsonElement
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
Constructors Constructor Description JsonPrimitive(Boolean bool)Create a primitive containing a boolean value.JsonPrimitive(Character c)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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonPrimitivedeepCopy()Returns the same value as primitives are immutable.booleanequals(Object obj)BigDecimalgetAsBigDecimal()convenience method to get this element as aBigDecimal.BigIntegergetAsBigInteger()convenience method to get this element as aBigInteger.booleangetAsBoolean()convenience method to get this element as a boolean value.bytegetAsByte()convenience method to get this element as a primitive byte value.chargetAsCharacter()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.doublegetAsDouble()convenience method to get this element as a primitive double.floatgetAsFloat()convenience method to get this element as a float.intgetAsInt()convenience method to get this element as a primitive integer.longgetAsLong()convenience method to get this element as a primitive long.NumbergetAsNumber()convenience method to get this element as a Number.shortgetAsShort()convenience method to get this element as a primitive short.StringgetAsString()convenience method to get this element as a String.inthashCode()booleanisBoolean()Check 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 Detail
-
JsonPrimitive
public JsonPrimitive(Boolean bool)
Create a primitive containing a boolean value.- Parameters:
bool- the value to create the primitive with.
-
JsonPrimitive
public JsonPrimitive(Number number)
Create a primitive containing aNumber.- Parameters:
number- the value to create the primitive with.
-
JsonPrimitive
public JsonPrimitive(String string)
Create a primitive containing a String value.- Parameters:
string- the value to create the primitive with.
-
JsonPrimitive
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- the value to create the primitive with.
-
-
Method Detail
-
deepCopy
public JsonPrimitive 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.- Overrides:
getAsBooleanin classJsonElement- Returns:
- get 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
public Number getAsNumber()
convenience method to get this element as a Number.- Overrides:
getAsNumberin classJsonElement- Returns:
- get this element as a Number.
- Throws:
NumberFormatException- if the value contained is not a valid Number.
-
isString
public boolean isString()
Check whether this primitive contains a String value.- Returns:
- true if this primitive contains a String value, false otherwise.
-
getAsString
public String getAsString()
convenience method to get this element as a String.- Overrides:
getAsStringin classJsonElement- Returns:
- get this element as a String.
-
getAsDouble
public double getAsDouble()
convenience method to get this element as a primitive double.- Overrides:
getAsDoublein classJsonElement- Returns:
- get this element as a primitive double.
- Throws:
NumberFormatException- if the value contained is not a valid double.
-
getAsBigDecimal
public BigDecimal getAsBigDecimal()
convenience method to get this element as aBigDecimal.- Overrides:
getAsBigDecimalin classJsonElement- Returns:
- get this element as a
BigDecimal. - Throws:
NumberFormatException- if the value contained is not a validBigDecimal.
-
getAsBigInteger
public BigInteger getAsBigInteger()
convenience method to get this element as aBigInteger.- Overrides:
getAsBigIntegerin classJsonElement- Returns:
- get this element as a
BigInteger. - Throws:
NumberFormatException- if the value contained is not a validBigInteger.
-
getAsFloat
public float getAsFloat()
convenience method to get this element as a float.- Overrides:
getAsFloatin classJsonElement- Returns:
- get this element as a float.
- 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:
- get this element as a primitive long.
- Throws:
NumberFormatException- if the value contained is not a valid long.
-
getAsShort
public short getAsShort()
convenience method to get this element as a primitive short.- Overrides:
getAsShortin classJsonElement- Returns:
- get this element as a primitive short.
- Throws:
NumberFormatException- if the value contained is not a valid short value.
-
getAsInt
public int getAsInt()
convenience method to get this element as a primitive integer.- Overrides:
getAsIntin classJsonElement- Returns:
- get this element as a primitive integer.
- 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:
- get this element as a primitive byte value.
-
getAsCharacter
public char getAsCharacter()
Description copied from class:JsonElementconvenience 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.- Overrides:
getAsCharacterin classJsonElement- Returns:
- the first character of the string.
-
-