Fix parsing of double and big decimals that have an exponent but no fraction part.

This commit is contained in:
Joel Leitch 2008-11-18 19:55:14 +00:00
parent e101ed1623
commit 788d2bfbbb
3 changed files with 36 additions and 0 deletions

View File

@ -208,6 +208,7 @@ final class JsonParser implements JsonParserConstants {
}
Number n;
if (exppart != null) {
fracpart = (fracpart == null) ? "" : fracpart;
n = new java.math.BigDecimal(intpart + fracpart + exppart);
} else if (fracpart != null) {
n = new Double(intpart + fracpart);

View File

@ -151,6 +151,7 @@ private JsonPrimitive JsonNumber() :
{
Number n;
if (exppart != null) {
fracpart = (fracpart == null) ? "" : fracpart;
n = new java.math.BigDecimal(intpart + fracpart + exppart);
} else if (fracpart != null) {
n = new Double(intpart + fracpart);

View File

@ -162,6 +162,26 @@ public class PrimitiveTest extends TestCase {
assertEquals("[-122.08]", gson.toJson(target, double[].class));
assertEquals("[-122.08]", gson.toJson(target, Double[].class));
}
public void testDoubleAsStringRepresentationDeserialization() {
String doubleValue = "1.0043E+5";
Double expected = Double.valueOf(doubleValue);
Double actual = gson.fromJson(doubleValue, Double.class);
assertEquals(expected, actual);
double actual1 = gson.fromJson(doubleValue, double.class);
assertEquals(expected.doubleValue(), actual1);
}
public void testDoubleNoFractAsStringRepresentationDeserialization() {
String doubleValue = "1E+5";
Double expected = Double.valueOf(doubleValue);
Double actual = gson.fromJson(doubleValue, Double.class);
assertEquals(expected, actual);
double actual1 = gson.fromJson(doubleValue, double.class);
assertEquals(expected.doubleValue(), actual1);
}
public void testPrimitiveDoubleAutoboxedInASingleElementArrayDeserialization() {
double expected = -122.08;
@ -213,6 +233,20 @@ public class PrimitiveTest extends TestCase {
assertEquals(expected, actual);
}
public void testBigDecimalAsStringRepresentationDeserialization() {
String doubleValue = "0.05E+5";
BigDecimal expected = new BigDecimal(doubleValue);
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
assertEquals(expected, actual);
}
public void testBigDecimalNoFractAsStringRepresentationDeserialization() {
String doubleValue = "5E+5";
BigDecimal expected = new BigDecimal(doubleValue);
BigDecimal actual = gson.fromJson(doubleValue, BigDecimal.class);
assertEquals(expected, actual);
}
public void testBigIntegerSerialization() {
BigInteger target = new BigInteger("12121211243123245845384534687435634558945453489543985435");
assertEquals(target.toString(), gson.toJson(target));