struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcucchi...@apache.org
Subject svn commit: r1200422 - in /struts/struts2/trunk/plugins/json/src: main/java/org/apache/struts2/json/JSONReader.java test/java/org/apache/struts2/json/JSONReaderTest.java
Date Thu, 10 Nov 2011 16:51:47 GMT
Author: mcucchiara
Date: Thu Nov 10 16:51:46 2011
New Revision: 1200422

URL: http://svn.apache.org/viewvc?rev=1200422&view=rev
Log:
WW-3701 - Wrong parsing of number with exponents

Added:
    struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONReaderTest.java
Modified:
    struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONReader.java

Modified: struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONReader.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONReader.java?rev=1200422&r1=1200421&r2=1200422&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONReader.java
(original)
+++ struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONReader.java
Thu Nov 10 16:51:46 2011
@@ -176,6 +176,7 @@ class JSONReader {
 
     private Object number() {
         this.buf.setLength(0);
+        boolean toDouble = false;
 
         if (this.c == '-') {
             this.add();
@@ -184,11 +185,13 @@ class JSONReader {
         this.addDigits();
 
         if (this.c == '.') {
+            toDouble = true;
             this.add();
             this.addDigits();
         }
 
         if ((this.c == 'e') || (this.c == 'E')) {
+            toDouble = true;
             this.add();
 
             if ((this.c == '+') || (this.c == '-')) {
@@ -198,8 +201,11 @@ class JSONReader {
             this.addDigits();
         }
 
-        return (this.buf.indexOf(".") >= 0) ? (Object) Double.parseDouble(this.buf.toString())
-                : (Object) Long.parseLong(this.buf.toString());
+        if (toDouble) {
+            return Double.parseDouble(this.buf.toString());
+        } else {
+            return Long.parseLong(this.buf.toString());
+        }
     }
 
     private Object string(char quote) {

Added: struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONReaderTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONReaderTest.java?rev=1200422&view=auto
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONReaderTest.java
(added)
+++ struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONReaderTest.java
Thu Nov 10 16:51:46 2011
@@ -0,0 +1,47 @@
+package org.apache.struts2.json;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * User: mcucchiara
+ * Date: 10/11/11
+ * Time: 17.26
+ */
+public class JSONReaderTest {
+    private JSONReader reader = new JSONReader();
+
+    @Test
+    public void testExponentialNumber() throws Exception {
+        Object ret = reader.read("5e-5");
+        assertNotNull(ret);
+        assertEquals(Double.class, ret.getClass());
+        assertEquals(5.0E-5, ret);
+    }
+
+    @Test
+    public void testExponentialNumber2() throws Exception {
+        Object ret = reader.read("123.4e10");
+        assertNotNull(ret);
+        assertEquals(Double.class, ret.getClass());
+        assertEquals(123.4e10, ret);
+    }
+
+    @Test
+    public void testDecimalNumber() throws Exception {
+        Object ret = reader.read("3.2");
+        assertNotNull(ret);
+        assertEquals(Double.class, ret.getClass());
+        assertEquals(3.2, ret);
+    }
+
+    @Test
+    public void testNaturalNumber() throws Exception {
+        Object ret = reader.read("123");
+        assertNotNull(ret);
+        assertEquals(Long.class, ret.getClass());
+        assertEquals(123L, ret);
+    }
+}



Mime
View raw message