Class 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object value  
    • Constructor Summary

      Constructors 
      Constructor Description
      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

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      JsonPrimitive deepCopy()
      Returns the same value as primitives are immutable.
      boolean equals​(java.lang.Object obj)
      Returns whether the other object is equal to this.
      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()
      Deprecated.
      This method is misleading, as it does not get this element as a char but rather as a string's first character.
      double getAsDouble()
      Convenience method to get this element as a primitive double value.
      float getAsFloat()
      Convenience method to get this element as a primitive float value.
      int getAsInt()
      Convenience method to get this element as a primitive integer value.
      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 value.
      java.lang.String getAsString()
      Convenience method to get this element as a string value.
      int hashCode()
      Returns the hash code of this object.
      boolean 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)
      boolean isNumber()
      Check whether this primitive contains a Number.
      boolean isString()
      Check whether this primitive contains a String value.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • value

        private final java.lang.Object value
    • 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. 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.
        Overrides:
        getAsBoolean in class JsonElement
        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

        public java.lang.Number 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).
        Overrides:
        getAsNumber in class JsonElement
        Returns:
        this element as a Number.
        Throws:
        java.lang.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

        public java.lang.String getAsString()
        Description copied from class: JsonElement
        Convenience method to get this element as a string value.
        Overrides:
        getAsString in class JsonElement
        Returns:
        this element as a string value.
      • getAsDouble

        public double getAsDouble()
        Description copied from class: JsonElement
        Convenience method to get this element as a primitive double value.
        Overrides:
        getAsDouble in class JsonElement
        Returns:
        this element as a primitive double value.
        Throws:
        java.lang.NumberFormatException - if the value contained is not a valid double.
      • getAsBigDecimal

        public java.math.BigDecimal getAsBigDecimal()
        Description copied from class: JsonElement
        Convenience method to get this element as a BigDecimal.
        Overrides:
        getAsBigDecimal in class JsonElement
        Returns:
        this element as a BigDecimal.
        Throws:
        java.lang.NumberFormatException - if this element is not a valid BigDecimal.
      • getAsBigInteger

        public java.math.BigInteger getAsBigInteger()
        Description copied from class: JsonElement
        Convenience method to get this element as a BigInteger.
        Overrides:
        getAsBigInteger in class JsonElement
        Returns:
        this element as a BigInteger.
        Throws:
        java.lang.NumberFormatException - if this element is not a valid BigInteger.
      • getAsFloat

        public float getAsFloat()
        Description copied from class: JsonElement
        Convenience method to get this element as a primitive float value.
        Overrides:
        getAsFloat in class JsonElement
        Returns:
        this element as a primitive float value.
        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:
        this element as a primitive long.
        Throws:
        java.lang.NumberFormatException - if the value contained is not a valid long.
      • getAsShort

        public short getAsShort()
        Description copied from class: JsonElement
        Convenience method to get this element as a primitive short value.
        Overrides:
        getAsShort in class JsonElement
        Returns:
        this element as a primitive short value.
        Throws:
        java.lang.NumberFormatException - if the value contained is not a valid short.
      • getAsInt

        public int getAsInt()
        Description copied from class: JsonElement
        Convenience method to get this element as a primitive integer value.
        Overrides:
        getAsInt in class JsonElement
        Returns:
        this element as a primitive integer value.
        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:
        this element as a primitive byte value.
        Throws:
        java.lang.NumberFormatException - if the value contained is not a valid byte.
      • getAsCharacter

        @Deprecated
        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.
        Description copied from class: JsonElement
        Convenience method to get the first character of the string value of this element.
        Overrides:
        getAsCharacter in class JsonElement
        Returns:
        the first character of the string value.
        Throws:
        java.lang.UnsupportedOperationException - if the string value of this primitive is empty.
      • hashCode

        public int hashCode()
        Returns the hash code of this object.
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        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.
        Overrides:
        equals in class java.lang.Object
      • isIntegral

        private static boolean isIntegral​(JsonPrimitive primitive)
        Returns true if the specified number is an integral type (Long, Integer, Short, Byte, BigInteger)