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.
ToNumberPolicy, GsonBuilder#setObjectToNumberStrategy(ToNumberStrategy), GsonBuilder#setNumberToNumberStrategy(ToNumberStrategy)
| Modifier and Type | Method and Description |
|---|---|
| public Number | Returns: number read from the JSON reader.JSON reader to read a number from in)Reads a number from the given JSON reader. |
| readNumber | back to summary |
|---|---|
| public Number readNumber(JsonReader in) throws IOException Reads a number from the given JSON reader. A strategy is supposed to read a single value from
the reader, and the read value is guaranteed never to be
| |