Interface JsonNumber


public interface JsonNumber

A JSON number in a representation that the model chooses.

JsonModel.numberValue(Object) returns a JsonNumber. For the common representations, use the factories of this interface:

JsonNumber small = JsonNumber.of(42);
JsonNumber exact = JsonNumber.of(new BigDecimal("0.1"));
Maybe<JsonNumber> binary = JsonNumber.of(1.5); // NaN and the infinities are not JSON numbers

A model can also implement this interface for its own representation, for example a number that it reads from bytes only when the evaluator compares it. The only obligation is exactValue().

A JsonNumber has no order of its own. The model compares numbers with JsonModel.compareNumbers(JsonNumber, JsonNumber), because only the model knows its representations and can compare them without conversion.

  • Method Details

    • exactValue

      JsonDecimal exactValue()

      Returns the exact value of the number.

      Each JSON number has an exact value, also with a very large exponent.

      Returns:
      the exact value
    • of

      static JsonNumber of(int value)
      Returns a number with an int value.
      Parameters:
      value - the value
      Returns:
      the number
    • of

      static JsonNumber of(long value)
      Returns a number with a long value.
      Parameters:
      value - the value
      Returns:
      the number
    • of

      static JsonNumber of(BigInteger value)
      Returns a number with a BigInteger value.
      Parameters:
      value - the value
      Returns:
      the number
    • of

      static JsonNumber of(BigDecimal value)
      Returns a number with a BigDecimal value.
      Parameters:
      value - the value
      Returns:
      the number
    • of

      static JsonNumber of(JsonDecimal value)
      Returns a number with a JsonDecimal value.
      Parameters:
      value - the value
      Returns:
      the number
    • of

      static Maybe<JsonNumber> of(double value)

      Returns a number with a double value.

      The exact value is the shortest decimal that gives the double (BigDecimal.valueOf(double)), so the JSON text 1.1, read as a double, equals the literal 1.1.

      Parameters:
      value - the value
      Returns:
      the number, or Maybe.None if the value is NaN or infinite, because these values are not JSON numbers