Don't support oversized values like 30-character integers

This commit is contained in:
Jesse Wilson 2011-09-09 05:11:59 +00:00
parent e19672d0a3
commit a7e9ac3612
2 changed files with 17 additions and 14 deletions

View File

@ -20,4 +20,9 @@ com.google.gson.functional.PrimitiveTest.testOverridingDefaultPrimitiveSerializa
GSON 1.x rejects integers that have any fraction, even if it is ".0"
GSON 2.x permits integers to have ".0" fractions like "1.0"
com.google.gson.functional.PrimitiveTest.testDeserializingDecimalPointValuesAsIntegerFails
com.google.gson.functional.PrimitiveTest.testDeserializingDecimalPointValuesAsIntegerFails
GSON 1.x truncates oversized large integers and longs
GSON 2.x fails on oversized large integers and longs
com.google.gson.functional.PrimitiveTest.testDeserializingBigIntegerAsInteger
com.google.gson.functional.PrimitiveTest.testDeserializingBigIntegerAsLong

View File

@ -18,18 +18,14 @@ package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.common.TestTypes.CrazyLongTypeAdapter;
import junit.framework.TestCase;
import java.io.Serializable;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import junit.framework.TestCase;
/**
* Functional tests for Json primitive values: integers, and floating point numbers.
@ -748,17 +744,19 @@ public class PrimitiveTest extends TestCase {
}
public void testDeserializingBigIntegerAsInteger() {
String bigIntegerValue = "12121211243123245845384534687435634558945453489543985435";
int actual = gson.fromJson(bigIntegerValue, Integer.class);
int expected = new BigInteger(bigIntegerValue).and(MAX_INT_VALUE).intValue();
assertEquals(expected, actual);
try {
gson.fromJson("12121211243123245845384534687435634558945453489543985435", Integer.class);
fail();
} catch (JsonSyntaxException expected) {
}
}
public void testDeserializingBigIntegerAsLong() {
String bigIntegerValue = "12121211243123245845384534687435634558945453489543985435";
long actual = gson.fromJson(bigIntegerValue, long.class);
long expected = new BigInteger(bigIntegerValue).and(MAX_LONG_VALUE).longValue();
assertEquals(expected, actual);
try {
gson.fromJson("12121211243123245845384534687435634558945453489543985435", Long.class);
fail();
} catch (JsonSyntaxException expected) {
}
}
public void testDeserializingBigDecimalAsLongFails() {