Class JsonElement

    • Constructor Summary

      Constructors 
      Constructor Description
      JsonElement()
      Deprecated.
      Creating custom JsonElement subclasses is highly discouraged and can lead to undefined behavior.
      This constructor is only kept for backward compatibility.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      abstract JsonElement deepCopy()
      Returns a deep copy of this element.
      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.
      JsonArray getAsJsonArray()
      Convenience method to get this element as a JsonArray.
      JsonNull getAsJsonNull()
      Convenience method to get this element as a JsonNull.
      JsonObject getAsJsonObject()
      Convenience method to get this element as a JsonObject.
      JsonPrimitive getAsJsonPrimitive()
      Convenience method to get this element as a JsonPrimitive.
      long getAsLong()
      Convenience method to get this element as a primitive long value.
      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.
      boolean isJsonArray()
      Provides a check for verifying if this element is a JSON array or not.
      boolean isJsonNull()
      Provides a check for verifying if this element represents a null value or not.
      boolean isJsonObject()
      Provides a check for verifying if this element is a JSON object or not.
      boolean isJsonPrimitive()
      Provides a check for verifying if this element is a primitive or not.
      java.lang.String toString()
      Returns a String representation of this element.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JsonElement

        @Deprecated
        public JsonElement()
        Deprecated.
        Creating custom JsonElement subclasses is highly discouraged and can lead to undefined behavior.
        This constructor is only kept for backward compatibility.
    • Method Detail

      • deepCopy

        public abstract JsonElement deepCopy()
        Returns a deep copy of this element. Immutable elements like primitives and nulls are not copied.
        Since:
        2.8.2
      • isJsonArray

        public boolean isJsonArray()
        Provides a check for verifying if this element is a JSON array or not.
        Returns:
        true if this element is of type JsonArray, false otherwise.
      • isJsonObject

        public boolean isJsonObject()
        Provides a check for verifying if this element is a JSON object or not.
        Returns:
        true if this element is of type JsonObject, false otherwise.
      • isJsonPrimitive

        public boolean isJsonPrimitive()
        Provides a check for verifying if this element is a primitive or not.
        Returns:
        true if this element is of type JsonPrimitive, false otherwise.
      • isJsonNull

        public boolean isJsonNull()
        Provides a check for verifying if this element represents a null value or not.
        Returns:
        true if this element is of type JsonNull, false otherwise.
        Since:
        1.2
      • getAsJsonObject

        public JsonObject getAsJsonObject()
        Convenience method to get this element as a JsonObject. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonObject() first.
        Returns:
        this element as a JsonObject.
        Throws:
        java.lang.IllegalStateException - if this element is of another type.
      • getAsJsonArray

        public JsonArray getAsJsonArray()
        Convenience method to get this element as a JsonArray. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonArray() first.
        Returns:
        this element as a JsonArray.
        Throws:
        java.lang.IllegalStateException - if this element is of another type.
      • getAsJsonPrimitive

        public JsonPrimitive getAsJsonPrimitive()
        Convenience method to get this element as a JsonPrimitive. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonPrimitive() first.
        Returns:
        this element as a JsonPrimitive.
        Throws:
        java.lang.IllegalStateException - if this element is of another type.
      • getAsJsonNull

        public JsonNull getAsJsonNull()
        Convenience method to get this element as a JsonNull. If this element is of some other type, an IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonNull() first.
        Returns:
        this element as a JsonNull.
        Throws:
        java.lang.IllegalStateException - if this element is of another type.
        Since:
        1.2
      • getAsBoolean

        public boolean getAsBoolean()
        Convenience method to get this element as a boolean value.
        Returns:
        this element as a primitive boolean value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsNumber

        public java.lang.Number getAsNumber()
        Convenience method to get this element as a Number.
        Returns:
        this element as a Number.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray, or cannot be converted to a number.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsString

        public java.lang.String getAsString()
        Convenience method to get this element as a string value.
        Returns:
        this element as a string value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsDouble

        public double getAsDouble()
        Convenience method to get this element as a primitive double value.
        Returns:
        this element as a primitive double value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid double.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsFloat

        public float getAsFloat()
        Convenience method to get this element as a primitive float value.
        Returns:
        this element as a primitive float value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid float.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsLong

        public long getAsLong()
        Convenience method to get this element as a primitive long value.
        Returns:
        this element as a primitive long value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid long.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsInt

        public int getAsInt()
        Convenience method to get this element as a primitive integer value.
        Returns:
        this element as a primitive integer value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid integer.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • getAsByte

        public byte getAsByte()
        Convenience method to get this element as a primitive byte value.
        Returns:
        this element as a primitive byte value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid byte.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
        Since:
        1.3
      • 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.
        Convenience method to get the first character of the string value of this element.
        Returns:
        the first character of the string value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray, or if its string value is empty.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
        Since:
        1.3
      • getAsBigDecimal

        public java.math.BigDecimal getAsBigDecimal()
        Convenience method to get this element as a BigDecimal.
        Returns:
        this element as a BigDecimal.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if this element is not a valid BigDecimal.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
        Since:
        1.2
      • getAsBigInteger

        public java.math.BigInteger getAsBigInteger()
        Convenience method to get this element as a BigInteger.
        Returns:
        this element as a BigInteger.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if this element is not a valid BigInteger.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
        Since:
        1.2
      • getAsShort

        public short getAsShort()
        Convenience method to get this element as a primitive short value.
        Returns:
        this element as a primitive short value.
        Throws:
        java.lang.UnsupportedOperationException - if this element is not a JsonPrimitive or JsonArray.
        java.lang.NumberFormatException - if the value contained is not a valid short.
        java.lang.IllegalStateException - if this element is of the type JsonArray but contains more than a single element.
      • toString

        public java.lang.String toString()
        Returns a String representation of this element.
        Overrides:
        toString in class java.lang.Object