discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Gson1.3 can't handle big number strings with exponent #94

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
public class FindIssue {
    public double largeNumber;
    public static void main(String[] args) {
        Gson gson = new Gson();
        FindIssue f=gson.fromJson("{largeNumber:1.234567899E8}",
FindIssue.class);
        System.out.println(f.largeNumber);
    }
}

What is the expected output? What do you see instead?
expected output: 1.234567899E8
instead: Exception in thread "main" com.google.gson.JsonParseException:
Failed parsing JSON source: java.io.StringReader@f81843 to Json
    at com.google.gson.Gson.fromJson(Gson.java:386)
    at com.google.gson.Gson.fromJson(Gson.java:329)
    at com.google.gson.Gson.fromJson(Gson.java:305)
    at own.FindIssue.main(FindIssue.java:18)
Caused by: com.google.gson.ParseException: Encountered "E8" at line 1,
column 25.
Was expecting one of:
    <E> ...
    "}" ...
    "," ...

    at com.google.gson.JsonParser.generateParseException(JsonParser.java:624)
    at com.google.gson.JsonParser.jj_consume_token(JsonParser.java:504)
    at com.google.gson.JsonParser.JsonObject(JsonParser.java:50)
    at com.google.gson.JsonParser.parse(JsonParser.java:11)
    at com.google.gson.Gson.fromJson(Gson.java:378)
    ... 3 more

What version of the product are you using? On what operating system?
gson1.3-BETA, Linux (Ubuntu 8.10)

Please provide any additional information below.
Because of some changes in gson1.3, Parser can't detect literal E, it will
always find literal "E#number#" as one IDENTIFIER literal which is not
handled in JsonParser.JsonNumber()

Original issue reported on code.google.com by J.Berin...@gmail.com on 20 Jan 2009 at 10:07

GoogleCodeExporter commented 9 years ago
This problem occurs because we require + or - after the "e" sign. So, 
1.234567899E+8
will work fine.

Original comment by inder123 on 4 Mar 2009 at 6:52

GoogleCodeExporter commented 9 years ago
But Gson itself produce such json without + or - after e and it is a valid 
number
format. Following code produce the same Exception (gson1.3 Rev. 389):

public class FindIssue {
    public double largeNumber;
    public static void main(String[] args) {
        Gson gson = new Gson();
        FindIssue fi=new FindIssue();
        fi.largeNumber=1234567899;
        String json=gson.toJson(fi);
        System.out.println("json: "+json);
        FindIssue f=gson.fromJson(json, FindIssue.class);
        System.out.println(f.largeNumber);
    }
}

Original comment by J.Berin...@gmail.com on 5 Mar 2009 at 7:15

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
J.Beringer is right, Gson itself omits the "+" sign after E when emitting 
numbers,
but then throws an exception when trying to parse the string it generated.

According to the spec at json.org, the sign after E is not required.  All of 
these
formats should be recognized:

E+25
E-25
E25

e+25
e-25
e25

inder123, can you remove the sign requirement from your grammar?

Original comment by alexande...@gmail.com on 5 Mar 2009 at 9:28

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Fixed in r390. Updated Gson grammar to support floating point numbers without 
the +/-
sign after E. This was a bit tricky since E8 matches an identifier as well, and 
that
was the source of the earlier bug. Added tests in PrimitiveTest to verify this 
behavior.

Original comment by inder123 on 5 Mar 2009 at 11:07

GoogleCodeExporter commented 9 years ago

Original comment by inder123 on 27 Mar 2009 at 7:39