You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by ke...@sz.murata.com.cn on 2010/11/17 08:07:54 UTC

[Trouble]JSONSerializer.parseDouble

Hi All,

Below is example.when the number value was convertted to E*,JSONSerializer
will hit this trouble.
 when I use arraylist convert map to json and would like re-convert to map
object,this error often to occurs.
Do you have any idea to solve it?
My Povit version is 1.5.2

|--------------------------------------------------------------------------|
|double a=1.000005;                                                        |
|double b=0.000005;                                                        |
|System.out.println(a);                                                    |
|System.out.println(b);                                                    |
|String testStr1=JSONSerializer.toString(a);                               |
|String testStr2=JSONSerializer.toString(b);                               |
|System.out.println(testStr1);                                             |
|System.out.println(testStr2);                                             |
|try {                                                                     |
|      System.out.println(JSONSerializer.parseDouble(testStr1));           |
|      System.out.println(JSONSerializer.parseDouble(testStr2));           |
|} catch (SerializationException e) {                                      |
|      // TODO Auto-generated catch block                                  |
|      e.printStackTrace();                                                |
|}                                                                         |
|1.000005                                                                  |
|5.0E-6                                                                    |
|1.000005                                                                  |
|5.0E-6                                                                    |
|1.000005                                                                  |
|Exception in thread "main" java.lang.NumberFormatException: For input     |
|string: "5.0E"                                                            |
|      at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)    |
|      at java.lang.Double.parseDouble(Unknown Source)                     |
|      at org.apache.pivot.json.JSONSerializer.readNumber(                 |
|JSONSerializer.java:403)                                                  |
|      at org.apache.pivot.json.JSONSerializer.readValue(                  |
|JSONSerializer.java:219)                                                  |
|      at org.apache.pivot.json.JSONSerializer.readObject(                 |
|JSONSerializer.java:193)                                                  |
|      at org.apache.pivot.json.JSONSerializer.parse(                      |
|JSONSerializer.java:810)                                                  |
|      at org.apache.pivot.json.JSONSerializer.parseDouble(                |
|JSONSerializer.java:906)                                                  |
|      at testing.JsonTest.main(JsonTest.java:23)                          |
|--------------------------------------------------------------------------|


Best regards,
Ken Jiang

*******************************************
Murata Electronics Trading  (Shenzhen) Co.,Ltd
Tel:86-755-82847251
E-mail:kenjiang@sz.murata.com.cn
*******************************************


Re: [Trouble]JSONSerializer.parseDouble

Posted by Greg Brown <gk...@mac.com>.
Good catch. JSONSerializer was not checking for the "-" sign in the floating point exponent value. I have fixed this in both the 1.5.x and 2.0 branches. Unfortunately, we will probably not be doing a 1.5.3 release, so you may need to either build 1.5 from source or upgrade to 2.0 to get the fix. The relevant change is here:

Modified: pivot/branches/1.5.x/core/src/org/apache/pivot/json/JSONSerializer.java
URL: http://svn.apache.org/viewvc/pivot/branches/1.5.x/core/src/org/apache/pivot/json/JSONSerializer.java?rev=1036009&r1=1036008&r2=1036009&view=diff
==============================================================================
--- pivot/branches/1.5.x/core/src/org/apache/pivot/json/JSONSerializer.java (original)
+++ pivot/branches/1.5.x/core/src/org/apache/pivot/json/JSONSerializer.java Wed Nov 17 12:44:53 2010
@@ -382,7 +382,7 @@ public class JSONSerializer implements S
        }

        while (c != -1 && (Character.isDigit(c) || c == '.'
-            || c == 'e' || c == 'E')) {
+            || c == 'e' || c == 'E' || c == '-')) {
            stringBuilder.append((char)c);
            integer &= !(c == '.');
            c = reader.read();

G

On Nov 17, 2010, at 2:07 AM, kenjiang@sz.murata.com.cn wrote:

> 
> Hi All,
> 
> Below is example.when the number value was convertted to E*,JSONSerializer
> will hit this trouble.
> when I use arraylist convert map to json and would like re-convert to map
> object,this error often to occurs.
> Do you have any idea to solve it?
> My Povit version is 1.5.2
> 
> |--------------------------------------------------------------------------|
> |double a=1.000005;                                                        |
> |double b=0.000005;                                                        |
> |System.out.println(a);                                                    |
> |System.out.println(b);                                                    |
> |String testStr1=JSONSerializer.toString(a);                               |
> |String testStr2=JSONSerializer.toString(b);                               |
> |System.out.println(testStr1);                                             |
> |System.out.println(testStr2);                                             |
> |try {                                                                     |
> |      System.out.println(JSONSerializer.parseDouble(testStr1));           |
> |      System.out.println(JSONSerializer.parseDouble(testStr2));           |
> |} catch (SerializationException e) {                                      |
> |      // TODO Auto-generated catch block                                  |
> |      e.printStackTrace();                                                |
> |}                                                                         |
> |1.000005                                                                  |
> |5.0E-6                                                                    |
> |1.000005                                                                  |
> |5.0E-6                                                                    |
> |1.000005                                                                  |
> |Exception in thread "main" java.lang.NumberFormatException: For input     |
> |string: "5.0E"                                                            |
> |      at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)    |
> |      at java.lang.Double.parseDouble(Unknown Source)                     |
> |      at org.apache.pivot.json.JSONSerializer.readNumber(                 |
> |JSONSerializer.java:403)                                                  |
> |      at org.apache.pivot.json.JSONSerializer.readValue(                  |
> |JSONSerializer.java:219)                                                  |
> |      at org.apache.pivot.json.JSONSerializer.readObject(                 |
> |JSONSerializer.java:193)                                                  |
> |      at org.apache.pivot.json.JSONSerializer.parse(                      |
> |JSONSerializer.java:810)                                                  |
> |      at org.apache.pivot.json.JSONSerializer.parseDouble(                |
> |JSONSerializer.java:906)                                                  |
> |      at testing.JsonTest.main(JsonTest.java:23)                          |
> |--------------------------------------------------------------------------|
> 
> 
> Best regards,
> Ken Jiang
> 
> *******************************************
> Murata Electronics Trading  (Shenzhen) Co.,Ltd
> Tel:86-755-82847251
> E-mail:kenjiang@sz.murata.com.cn
> *******************************************
>