Class JSONValueNumber


  • public class JSONValueNumber
    extends JSONValuePrimitive
    Represents one JSON 'number' value.

    According to RFC 8259 - 6. Numbers, JSON does not set limit for 'number' values. Very large or very precise numbers cannot be represented by Java primitive types. Therefore, this class internally holds a text representation of a JSON 'number' value.

    Instances of this class are immutable.

    • Constructor Detail

      • JSONValueNumber

        public JSONValueNumber​(String text)
                        throws NumberFormatException
        Create an instance of a Java representation of a JSON number value.
        Parameters:
        text - value represented by a Java String object of this JSON string value. Cannot be null.
        Throws:
        NumberFormatException - when the given text does not fulfill the number format defined by RFC 8259 - 6. Numbers.
      • JSONValueNumber

        public JSONValueNumber​(long value)
        Create an instance of a Java representation of a JSON number value from a Java long value.
        Parameters:
        value - Java long value which represents this JSON number value
      • JSONValueNumber

        public JSONValueNumber​(double value)
                        throws IllegalArgumentException
        Create an instance of a Java representation of a JSON number value from a Java double value.
        Parameters:
        value - Java long value which represents this JSON number value
        Throws:
        IllegalArgumentException - if the given value is NaN, +Inf or -Inf
      • JSONValueNumber

        public JSONValueNumber​(JSONTokenNumber token)
        Create an instance of a Java representation of a JSON number value from a JSON number token.
        Parameters:
        token - source token of this JSON string value.
    • Method Detail

      • text

        public String text()
        Return a JSON text representation of this JSON number value.

        The returned text is not normalized. For example, there are many JSON texts that represent one thousand; 1000, 1.0e3, 1000.00. This method returns one of valid JSON text representations. There is no guarantee that this method returns the same text representation for the same mathematical value.

        Returned text always complies with RFC 8259 - 6. Numbers.

        Returns:
        text which represents this JSON number value.
      • toDouble

        public double toDouble()
        This method returns a Java double value that this JSON number represents.

        Note that not all valid values of JSON "number" primitives can be precisely represented by a Java double value. For example, JSON "number" primitive allows very large number like 1E400, but Java double treats such large value as Double.POSITIVE_INFINITY. Or, some fractions may be simply ignored. Therefore, the application programmer who calls this method must not assume that the returned double value precisely represents the original number in the JSON text.

        This method internally calls Double.parseDouble(String). Too big values like "1e309" is converted to Double.POSITIVE_INFINITY, for example. Too small values like "-2.4E-324" is converted to -0.0.

        Returns:
        a Java double value that this token represents
      • toLong

        public long toLong()
                    throws NumberFormatException
        This method returns a Java long value that this token represents.

        Note that not all valid values of JSON "number" primitives can be represented by a Java long value. For example, JSON "number" primitive allows a fraction, but Java long value does not accept a fraction. If this JSON "number" primitive value cannot be converted to a Java long value, this method raises NumberFormatException.

        Returns:
        a Java long value that this token represents.
        Throws:
        NumberFormatException - if the value cannot be converted to a Java long value
      • canBeLong

        public boolean canBeLong()
        Returns if this token can be converted to a Java long value. If this method returns true, toLong() can be executed without an exception.
        Returns:
        whether this token can be converted to a Java long value.
      • hashCode

        public int hashCode()
        To best comply with JSON Schema this method tries to return the same hash for two JSONValueNumber which have mathematically have the same numeric value. For example, 10e9 and 1e10 returns the same hash.

        However, big values which BigDecimal cannot handle may or may not return the same hash.

        Overrides:
        hashCode in class Object
        Returns:
        hash code for this JSONValueNumber
      • equals

        public boolean equals​(Object obj)
        To best comply with JSON Schema this method tries to compare the value mathematically using BigDecimal. However, big values which BigDecimal cannot handle will not be mathematically evaluated. For example, 10e10000000000 and 1e10000000001 are mathematically equal, but this method considers that they are different.
        Overrides:
        equals in class Object
        Parameters:
        obj - Object to which this JSONValueNumber is to be compared.
        Returns:
        true if and only if the specified Object is a JSONValueNumber whose value and scale are equal to this JSONValueNumber's.
      • toTokenString

        @NotNull
        public @NotNull String toTokenString()
        Description copied from class: JSONValue
        Convert this JSON value to String which can be saved as a JSON text to a file or transmitted to network. The output text is optimized for machine, so no indent or new lines will be inserted.

        Note that, according to RFC 8259 - 8.1 Character Encoding, your application program must encode the returned String using UTF-8 without BOM before writing the returned String to a file or a network stream, or network datagram(s). It is caller's responsibility to correctly encode it.

        Specified by:
        toTokenString in class JSONValue
        Returns:
        a string representation of this JSON value
      • toTokenString

        @NotNull
        public @NotNull String toTokenString​(String newline,
                                             String indent)
        Description copied from class: JSONValue
        Convert this JSON value to String which can be saved as a JSON text to a file or transmitted to network. The output text is optimized for human. Indents and new line characters are inserted accordingly.

        Note that, according to RFC 8259 - 8.1 Character Encoding, your application program must encode the returned String using UTF-8 without BOM before writing the returned String to a file or a network stream, or network datagram(s). It is caller's responsibility to correctly encode it.

        Specified by:
        toTokenString in class JSONValue
        Parameters:
        newline - line separator; "\r", "\n" or "\r\n"
        indent - indent string. Must consist of " " and "\t".
        Returns:
        a string representation of this JSON value