Class Json5Primitive


  • public final class Json5Primitive
    extends Json5Element
    A class representing a Json5 primitive value. A primitive value is either a String, a Java primitive, or a Java primitive wrapper type.

    See the Json5Element documentation for details on how to convert Json5Primitive and generally any Json5Element from and to Json5.

    • Method Detail

      • fromNull

        public static Json5Null fromNull()
        Create a primitive containing a null value.
        Returns:
        New Json5Null value
      • fromBoolean

        public static Json5Primitive fromBoolean​(java.lang.Boolean bool)
        Create a primitive containing a boolean value.
        Parameters:
        bool - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided Boolean value
      • fromInstant

        public static Json5Primitive fromInstant​(java.time.Instant instant)
        Create a primitive containing a Instant value.
        Parameters:
        instant - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided Instant value

        This is an extension that is not compliant to the official Json5 spec.

      • fromNumber

        public static Json5Primitive fromNumber​(java.lang.Number number,
                                                int radix)
        Creates a primitive containing a Number with specified radix base. If a radix base of 2, 8 or 16 is set, this method will ensure that the underlying number implementation is a BigInteger.
        Parameters:
        number - The number
        radix - Radix base
        Returns:
        Json5Primitive containing the provided Number with specified radix base
      • fromNumber

        public static Json5Primitive fromNumber​(java.lang.Number number)
        Create a primitive containing a decimal Number (radix base 10).
        Parameters:
        number - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided Number with radix base 10
      • fromBinaryString

        public static Json5Primitive fromBinaryString​(java.lang.String binaryString)
        Create a primitive containing a binary number (radix base 2). For example +0b1010..., 0b1010... or -0b1010....

        This is an extension that is not compliant to the official Json5 spec.

        Parameters:
        binaryString - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided binary number with radix base 2
      • fromOctalString

        public static Json5Primitive fromOctalString​(java.lang.String octalString)
        Create a primitive containing an octal number (radix base 8). For example +0o107..., 0o107... or -0o107....

        This is an extension that is not compliant to the official Json5 spec.

        Parameters:
        octalString - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided octal number with radix base 8
      • fromHexString

        public static Json5Primitive fromHexString​(java.lang.String hexString)
        Create a primitive containing a binary number (radix base 16). For example +0x09af..., 0x09af... or -0x09af....
        Parameters:
        hexString - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided hex number with radix base 16
      • fromString

        public static Json5Primitive fromString​(java.lang.String string)
        Create a primitive containing a String value.
        Parameters:
        string - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided String
      • fromCharacter

        public static Json5Primitive fromCharacter​(java.lang.Character c)
        Create a primitive containing a character. The character is turned into a one character String since Json5 only supports String.
        Parameters:
        c - the value to create the primitive with.
        Returns:
        Json5Primitive containing the provided Character
      • 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 Json5Element
        Returns:
        this element as a primitive boolean value.
      • isInstant

        public boolean isInstant()
        Check whether this primitive contains a Instant value.
        Returns:
        true if this primitive contains a Instant value, false otherwise.
      • getAsInstant

        public java.time.Instant getAsInstant()
        Description copied from class: Json5Element
        Convenience method to get this element as a Instant value.
        Overrides:
        getAsInstant in class Json5Element
        Returns:
        this element as a primitive Instant value.
      • isNumber

        public boolean isNumber()
        Check whether this primitive contains a Number.
        Returns:
        true if this primitive contains a Number, false otherwise.
      • getNumberRadix

        public int getNumberRadix()
      • isBinaryNumber

        public boolean isBinaryNumber()
      • isOctalNumber

        public boolean isOctalNumber()
      • isHexNumber

        public boolean isHexNumber()
      • 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 Json5Element
        Returns:
        this element as a Number.
        Throws:
        java.lang.UnsupportedOperationException - if this primitive is neither a number nor a string.
      • getAsBinaryString

        public java.lang.String getAsBinaryString()
        Description copied from class: Json5Element
        Convenience method to get this element as a primitive binary number (radix base 2) value.

        This is an extension that is not compliant to the official Json5 spec.

        Overrides:
        getAsBinaryString in class Json5Element
        Returns:
        this element as a primitive binary number value string.
      • getAsOctalString

        public java.lang.String getAsOctalString()
        Description copied from class: Json5Element
        Convenience method to get this element as a primitive octal number (radix base 8) value.

        This is an extension that is not compliant to the official Json5 spec.

        Overrides:
        getAsOctalString in class Json5Element
        Returns:
        this element as a primitive octal number value string.
      • getAsHexString

        public java.lang.String getAsHexString()
        Description copied from class: Json5Element
        Convenience method to get this element as a primitive hex number (radix base 16) value.
        Overrides:
        getAsHexString in class Json5Element
        Returns:
        this element as a primitive hex number value 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: Json5Element
        Convenience method to get this element as a string value.
        Overrides:
        getAsString in class Json5Element
        Returns:
        this element as a string value.
      • getAsDouble

        public double getAsDouble()
        Description copied from class: Json5Element
        Convenience method to get this element as a primitive double value.
        Overrides:
        getAsDouble in class Json5Element
        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: Json5Element
        Convenience method to get this element as a BigDecimal.
        Overrides:
        getAsBigDecimal in class Json5Element
        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: Json5Element
        Convenience method to get this element as a BigInteger.
        Overrides:
        getAsBigInteger in class Json5Element
        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: Json5Element
        Convenience method to get this element as a primitive float value.
        Overrides:
        getAsFloat in class Json5Element
        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 Json5Element
        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: Json5Element
        Convenience method to get this element as a primitive short value.
        Overrides:
        getAsShort in class Json5Element
        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: Json5Element
        Convenience method to get this element as a primitive integer value.
        Overrides:
        getAsInt in class Json5Element
        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: Json5Element
        Convenience method to get this element as a primitive byte value.
        Overrides:
        getAsByte in class Json5Element
        Returns:
        this element as a primitive byte value.
        Throws:
        java.lang.NumberFormatException - if the value contained is not a valid byte.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class Json5Element