Class JsonNumber
- All Implemented Interfaces:
JsonValue
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 Summary
Modifier and TypeMethodDescriptionbooleanstatic @NonNull JsonNumberReturns a JSON number holdingvalue.static @NonNull JsonNumberfromValue(@NonNull BigDecimal value) Returns a JSON number holdingvalueexactly.Returns this number as along, if it is a whole number in the range oflong.getValue()Returns this number's exact value, with the scale it was given or parsed with.inthashCode()toJson()Serializes this value as compact JSON text: no insignificant whitespace, members in insertion order, and numbers in the canonical form ofBigDecimal.toString()(1e2becomes1E+2).toString()
-
Method Details
-
fromValue
Returns a JSON number holdingvalueexactly.A subclass of
BigDecimalis first copied into aBigDecimalwith 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- ifvalueisnullIllegalArgumentException- ifvaluehas more than 4,096 digits, or an adjusted exponent of magnitude above 100,000- Since:
- 1.0.0
-
fromValue
Returns a JSON number holdingvalue.- Parameters:
value- the number's value- Returns:
- the JSON number
- Throws:
NullPointerException- ifvalueisnull- 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 ofBigDecimalitself, never a subclass.- Returns:
- the value
- Since:
- 1.0.0
-
getLongValueExact
Returns this number as along, if it is a whole number in the range oflong. Trailing zeros after the decimal point do not matter:42.0and4.2E+1give 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:JsonValueSerializes this value as compact JSON text: no insignificant whitespace, members in insertion order, and numbers in the canonical form ofBigDecimal.toString()(1e2becomes1E+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. -
equals
-
hashCode
-
toString
-