WW-3701 - Wrong parsing of number with exponents

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1200422 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maurizio Cucchiara
2011-11-10 16:51:46 +00:00
parent eb17fbb3a0
commit d0bdf09e7d
2 changed files with 55 additions and 2 deletions
@@ -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) {
@@ -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);
}
}