Interface ToNumberStrategy

  • All Known Implementing Classes:
    ToNumberPolicy

    public interface ToNumberStrategy
    A strategy that is used to control how numbers should be deserialized for Object and Number when a concrete type of the deserialized number is unknown in advance. By default, Gson uses the following deserialization strategies:

    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.

    See Also:
    ToNumberPolicy, GsonBuilder.setObjectToNumberStrategy(ToNumberStrategy), GsonBuilder.setNumberToNumberStrategy(ToNumberStrategy)
    • 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 be null.
        Parameters:
        in - JSON reader to read a number from
        Returns:
        number read from the JSON reader.
        Throws:
        java.io.IOException