Package com.google.gson
Interface ToNumberStrategy
-
- All Known Implementing Classes:
ToNumberPolicy
public interface ToNumberStrategy
A strategy that is used to control how numbers should be deserialized forObject
andNumber
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 asObject
, seeToNumberPolicy.DOUBLE
;- Lazily parsed number values are returned if the deserialization type is declared as
Number
, seeToNumberPolicy.LAZILY_PARSED_NUMBER
.
For historical reasons, Gson does not support deserialization of arbitrary-length numbers for
Object
andNumber
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
orToNumberPolicy.BIG_DECIMAL
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.Number
readNumber(JsonReader in)
Reads a number from the given JSON reader.
-
-
-
Method Detail
-
readNumber
java.lang.Number readNumber(JsonReader in) throws java.io.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 benull
.- Parameters:
in
- JSON reader to read a number from- Returns:
- number read from the JSON reader.
- Throws:
java.io.IOException
-
-