My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
             
New issue | Search
for
| Advanced search | Search tips
Issue 94: Gson1.3 can't handle big number strings with exponent
2 people starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  ----
Closed:  Mar 2009
Type-Defect
Priority-Medium
Milestone-Release1.3


Sign in to add a comment
 
Reported by J.Beringer, Jan 20, 2009
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()

Comment 1 by inder123, Mar 04, 2009
This problem occurs because we require + or - after the "e" sign. So, 1.234567899E+8
will work fine.

Comment 2 by J.Beringer, Mar 04, 2009
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);
    }
}
Comment 4 by alexander.epshteyn, Mar 05, 2009
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?

Comment 6 by inder123, Mar 05, 2009
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.

Comment 7 by inder123, Mar 27, 2009
(No comment was entered for this change.)
Labels: Milestone-Release1.3
Sign in to add a comment

Hosted by Google Code