Class JsonNumber

java.lang.Object
com.revetsec.json.JsonNumber
All Implemented Interfaces:
JsonValue

@Immutable public final class JsonNumber extends Object implements JsonValue
A JSON number, held exactly as a BigDecimal. There is no floating-point form.

Caps. A number has at most 4,096 significant digits (its BigDecimal.precision()) and an adjusted decimal exponent (precision − scale − 1, the exponent of its leading digit) of magnitude at most 100,000, so it always renders in bounded space and compares in bounded time.

Equality is by numeric value: 1, 1.0, 1E0 and 0.1E1 are equal and have the same hash code, which is computed once from the nearest double. No method expands a number to its plain form, so 1E+100000 costs no more to compare or hash than 1.

Rendering. toJson() writes the canonical form of BigDecimal.toString(), not the text a number was parsed from: 1e2 becomes 1E+2, -0 becomes 0, and 1.50 stays 1.50.

Since:
1.0.0
Author:
Mark Allen
  • Method Details

    • fromValue

      Returns a JSON number holding value exactly.

      A subclass of BigDecimal is first copied into a BigDecimal with the same unscaled value and scale, and the copy is what the number checks, holds and renders, so the number can never change later.

      Parameters:
      value - the number's value
      Returns:
      the JSON number
      Throws:
      NullPointerException - if value is null
      IllegalArgumentException - if value has more than 4,096 digits, or an adjusted exponent of magnitude above 100,000
      Since:
      1.0.0
    • fromValue

      public static @NonNull JsonNumber fromValue(@NonNull Long value)
      Returns a JSON number holding value.
      Parameters:
      value - the number's value
      Returns:
      the JSON number
      Throws:
      NullPointerException - if value is null
      Since:
      1.0.0
    • getValue

      Returns this number's exact value, with the scale it was given or parsed with. It is always an instance of BigDecimal itself, never a subclass.
      Returns:
      the value
      Since:
      1.0.0
    • getLongValueExact

      Returns this number as a long, if it is a whole number in the range of long. Trailing zeros after the decimal point do not matter: 42.0 and 4.2E+1 give 42.
      Returns:
      the value, or empty if it has a non-zero fraction or is outside the range of long
      Since:
      1.0.0
    • toJson

      Description copied from interface: JsonValue
      Serializes this value as compact JSON text: no insignificant whitespace, members in insertion order, and numbers in the canonical form of BigDecimal.toString() (1e2 becomes 1E+2).

      Strings escape only what JSON requires: the quotation mark, the reverse solidus and the control characters U+0000 to U+001F, using the two-character escapes where they exist. Every other character, U+2028 and U+2029 included, is written as itself.

      Unlike toString(), the result contains every string and number this value holds. It is not the text a value was parsed from, so never verify a signature over it.

      Specified by:
      toJson in interface JsonValue
      Returns:
      the JSON text
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      Overrides:
      toString in class Object