com.google.gson
Class JsonPrimitive

java.lang.Object
  extended by com.google.gson.JsonElement
      extended by 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
JsonPrimitive(java.lang.Boolean bool)
          Create a primitive containing a boolean value.
JsonPrimitive(java.lang.Character c)
          Create a primitive containing a character.
JsonPrimitive(java.lang.Number number)
          Create a primitive containing a Number.
JsonPrimitive(java.lang.String string)
          Create a primitive containing a String value.
 
Method Summary
 boolean equals(java.lang.Object obj)
           
 java.math.BigDecimal getAsBigDecimal()
          convenience method to get this element as a BigDecimal.
 java.math.BigInteger getAsBigInteger()
          convenience method to get this element as a BigInteger.
 boolean getAsBoolean()
          convenience method to get this element as a boolean value.
 byte getAsByte()
          convenience method to get this element as a primitive byte value.
 char getAsCharacter()
          convenience method to get this element as a primitive character value.
 double getAsDouble()
          convenience method to get this element as a primitive double.
 float getAsFloat()
          convenience method to get this element as a float.
 int getAsInt()
          convenience method to get this element as a primitive integer.
 long getAsLong()
          convenience method to get this element as a primitive long.
 java.lang.Number getAsNumber()
          convenience method to get this element as a Number.
 short getAsShort()
          convenience method to get this element as a primitive short.
 java.lang.String getAsString()
          convenience method to get this element as a String.
 int hashCode()
           
 boolean isBoolean()
          Check whether this primitive contains a boolean value.
 boolean isNumber()
          Check whether this primitive contains a Number.
 boolean isString()
          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
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonPrimitive

public JsonPrimitive(java.lang.Boolean bool)
Create a primitive containing a boolean value.

Parameters:
bool - the value to create the primitive with.

JsonPrimitive

public JsonPrimitive(java.lang.Number number)
Create a primitive containing a Number.

Parameters:
number - the value to create the primitive with.

JsonPrimitive

public JsonPrimitive(java.lang.String string)
Create a primitive containing a String value.

Parameters:
string - the value to create the primitive with.

JsonPrimitive

public JsonPrimitive(java.lang.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

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:
getAsBoolean in class JsonElement
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 java.lang.Number getAsNumber()
convenience method to get this element as a Number.

Overrides:
getAsNumber in class JsonElement
Returns:
get this element as a Number.
Throws:
java.lang.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 java.lang.String getAsString()
convenience method to get this element as a String.

Overrides:
getAsString in class JsonElement
Returns:
get this element as a String.

getAsDouble

public double getAsDouble()
convenience method to get this element as a primitive double.

Overrides:
getAsDouble in class JsonElement
Returns:
get this element as a primitive double.
Throws:
java.lang.NumberFormatException - if the value contained is not a valid double.

getAsBigDecimal

public java.math.BigDecimal getAsBigDecimal()
convenience method to get this element as a BigDecimal.

Overrides:
getAsBigDecimal in class JsonElement
Returns:
get this element as a BigDecimal.
Throws:
java.lang.NumberFormatException - if the value contained is not a valid BigDecimal.

getAsBigInteger

public java.math.BigInteger getAsBigInteger()
convenience method to get this element as a BigInteger.

Overrides:
getAsBigInteger in class JsonElement
Returns:
get this element as a BigInteger.
Throws:
java.lang.NumberFormatException - if the value contained is not a valid BigInteger.

getAsFloat

public float getAsFloat()
convenience method to get this element as a float.

Overrides:
getAsFloat in class JsonElement
Returns:
get this element as a float.
Throws:
java.lang.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:
getAsLong in class JsonElement
Returns:
get this element as a primitive long.
Throws:
java.lang.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:
getAsShort in class JsonElement
Returns:
get this element as a primitive short.
Throws:
java.lang.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:
getAsInt in class JsonElement
Returns:
get this element as a primitive integer.
Throws:
java.lang.NumberFormatException - if the value contained is not a valid integer.

getAsByte

public byte getAsByte()
Description copied from class: JsonElement
convenience method to get this element as a primitive byte value.

Overrides:
getAsByte in class JsonElement
Returns:
get this element as a primitive byte value.

getAsCharacter

public char getAsCharacter()
Description copied from class: JsonElement
convenience method to get this element as a primitive character value.

Overrides:
getAsCharacter in class JsonElement
Returns:
get this element as a primitive char value.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object


Copyright © 2008-2011 Google, Inc.. All Rights Reserved.