Issue 797: gson 2+ defaults numbers to double

This commit is contained in:
Adrian Cole 2012-01-01 15:48:55 -08:00
parent 82c1d11191
commit 7e21b0ad05
2 changed files with 14 additions and 13 deletions

View File

@ -58,20 +58,22 @@ public class JsonObjectTest {
public void testHash() {
String json = "{\"tomcat6\":{\"ssl_port\":8433}}";
Map<String, Object> map = ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433));
// gson deserialized numbers to double, so integers end up changed to fractions
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads.newStringPayload(json))),
ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433d)));
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads.newStringPayload(json))), map);
assertEquals(mapper.toJson(map), json);
assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433))), json);
}
public void testList() {
String json = "{\"list\":[8431,8433]}";
Map<String, Object> map = ImmutableMap.<String, Object> of("list", ImmutableList.of(8431, 8433));
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads.newStringPayload(json))), map);
assertEquals(mapper.toJson(map), json);
// gson deserialized numbers to double, so integers end up changed to fractions
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads.newStringPayload(json))),
ImmutableMap.<String, Object> of("list", ImmutableList.of(8431d, 8433d)));
assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("list", ImmutableList.of(8431, 8433))), json);
}
@ -86,9 +88,9 @@ public class JsonObjectTest {
}
public void testNumber() {
String json = "{\"number\":1}";
String json = "{\"number\":1.0}";
Map<String, Object> map = ImmutableMap.<String, Object> of("number", 1);
Map<String, Object> map = ImmutableMap.<String, Object> of("number", 1d);
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads.newStringPayload(json))), map);
assertEquals(mapper.toJson(map), json);

View File

@ -29,7 +29,6 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.JsonParseException;
import com.google.inject.Guice;
import com.google.inject.TypeLiteral;
@ -107,12 +106,12 @@ public class JsonTest {
public void testMapStringObjectWithAllValidValuesOneDeep() {
Map<String, Object> map = Maps.newHashMap();
map.put("string", "string");
map.put("number", 1);
map.put("number", 1.0);
map.put("boolean", true);
map.put("map", ImmutableMap.of("key", "value"));
map.put("list", ImmutableList.of("key", "value"));
assertEquals(json.toJson(map),
"{\"string\":\"string\",\"map\":{\"key\":\"value\"},\"list\":[\"key\",\"value\"],\"boolean\":true,\"number\":1}");
"{\"string\":\"string\",\"map\":{\"key\":\"value\"},\"list\":[\"key\",\"value\"],\"boolean\":true,\"number\":1.0}");
Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
}.getType());
assertEquals(map2, map);
@ -143,7 +142,7 @@ public class JsonTest {
assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInside.class).enumValue, EnumInside.Test.FOO);
}
@Test(expectedExceptions = JsonParseException.class)
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDeserializeEnumWhenBadValue() {
assertEquals(json.fromJson("{enumValue : \"s\"}", EnumInside.class).enumValue, EnumInside.Test.FOO);
}