Allow deserialization of a Number represented as a String (#964)

This commit is contained in:
Egor Neliuba 2016-11-26 09:40:14 +02:00 committed by inder123
parent 791236ac79
commit 44cad04a63
2 changed files with 6 additions and 0 deletions

View File

@ -368,6 +368,7 @@ public final class TypeAdapters {
in.nextNull();
return null;
case NUMBER:
case STRING:
return new LazilyParsedNumber(in.nextString());
default:
throw new JsonSyntaxException("Expecting number, got: " + jsonToken);

View File

@ -158,6 +158,11 @@ public class PrimitiveTest extends TestCase {
assertEquals(1L, actual.longValue());
}
public void testNumberAsStringDeserialization() {
Number value = gson.fromJson("\"18\"", Number.class);
assertEquals(18, value.intValue());
}
public void testPrimitiveDoubleAutoboxedSerialization() {
assertEquals("-122.08234335", gson.toJson(-122.08234335));
assertEquals("122.08112002", gson.toJson(new Double(122.08112002)));