public interface ToNumberStrategy
Object
and Number
when a concrete type of the deserialized number is unknown in advance. By default, Gson uses the following
deserialization strategies:
Double
values are returned for JSON numbers if the deserialization type is declared as
Object
, see ToNumberPolicy.DOUBLE
;Number
,
see ToNumberPolicy.LAZILY_PARSED_NUMBER
.For historical reasons, Gson does not support deserialization of arbitrary-length numbers for
Object
and Number
by default, potentially causing precision loss. However,
RFC 8259 permits this:
This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754 binary64 (double precision) numbers [IEEE754] is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision. A JSON number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the software that created it expects receiving software to have greater capabilities for numeric magnitude and precision than is widely available.
To overcome the precision loss, use for example ToNumberPolicy.LONG_OR_DOUBLE
or
ToNumberPolicy.BIG_DECIMAL
.
Modifier and Type | Method and Description |
---|---|
java.lang.Number |
readNumber(JsonReader in)
Reads a number from the given JSON reader.
|
java.lang.Number readNumber(JsonReader in) throws java.io.IOException
null
.in
- JSON reader to read a number fromjava.io.IOException
Copyright © 2010 - 2023 Adobe. All Rights Reserved