Fix parsing of double and big decimals that have an exponent but no fraction part.
This commit is contained in:
parent
e101ed1623
commit
788d2bfbbb
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -163,6 +163,26 @@ public class PrimitiveTest extends TestCase {
|
||||
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;
|
||||
double actual = gson.fromJson("[-122.08]", double.class);
|
||||
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user