Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when deserializing Double.NaN, Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY #81

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 12 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?

public class GsonDouble
{
    private double negInf = Double.NEGATIVE_INFINITY;
    private double posInf = Double.POSITIVE_INFINITY;
    private double notANumber = Double.NaN;
    private double number = 2.0;

    public static void main(String[] args)
    {
        Gson gson = new Gson();

        String json = gson.toJson(new GsonDouble());
        System.out.println(json);

        GsonDouble gsonDouble = gson.fromJson(json, GsonDouble.class);

    }
}

Exception in thread "main" com.google.gson.JsonParseException: Failed
parsing JSON source: java.io.StringReader@134bed0 to Json
    at com.google.gson.Gson.fromJson(Gson.java:376)
    at com.google.gson.Gson.fromJson(Gson.java:321)
    at com.google.gson.Gson.fromJson(Gson.java:297)
    at GsonDouble.main(GsonDouble.java:29)
Caused by: com.google.gson.TokenMgrError: Lexical error at line 1, column
12.  Encountered: "I" (73), after : ""
    at
com.google.gson.JsonParserTokenManager.getNextToken(JsonParserTokenManager.java:
999)
    at com.google.gson.JsonParser.jj_consume_token(JsonParser.java:366)
    at com.google.gson.JsonParser.Digits(JsonParser.java:267)
    at com.google.gson.JsonParser.JsonInt(JsonParser.java:241)
    at com.google.gson.JsonParser.JsonNumber(JsonParser.java:192)
    at com.google.gson.JsonParser.JsonValue(JsonParser.java:131)
    at com.google.gson.JsonParser.Pair(JsonParser.java:78)
    at com.google.gson.JsonParser.Members(JsonParser.java:61)
    at com.google.gson.JsonParser.JsonObject(JsonParser.java:42)
    at com.google.gson.JsonParser.parse(JsonParser.java:11)
    at com.google.gson.Gson.fromJson(Gson.java:370)
    ... 3 more


The serialized String looks as follows:

{"negInf":-Infinity,"posInf":Infinity,"notANumber":NaN,"number":2.0}

I guess the serialization is ok, since JavaScript uses Number.NaN,
Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY and the text
representation fits the above JSON string.
The deserializer seems to be unaware of this.

I would be great if you could fix this for the upcoming release.

Original issue reported on code.google.com by nit...@googlemail.com on 12 Dec 2008 at 1:09

@zipswich
Copy link

The same problem with Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY as of V2.8.4.

@hanslovsky
Copy link

@zipswich, what error message do you observe? I see

Caused by: java.lang.IllegalArgumentException: -Infinity is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.

and when I use

GsonBuilder.serializeSpecialFloatingPointValues() 

to create my Gson I can serialize both float and double infinities and NaNs (on version 2.8.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants