Treat negative zero as a number, not a long. (#285)

Updates logic from https://github.com/google/gson/issues/1053
This commit is contained in:
Eric Cochran
2017-04-20 15:22:06 -07:00
committed by Jesse Wilson
parent b4c43ae771
commit 448a2d3298
2 changed files with 8 additions and 1 deletions

View File

@@ -464,7 +464,8 @@ final class JsonUtf8Reader extends JsonReader {
} }
// We've read a complete number. Decide if it's a PEEKED_LONG or a PEEKED_NUMBER. // We've read a complete number. Decide if it's a PEEKED_LONG or a PEEKED_NUMBER.
if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative)) { if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative)
&& (value != 0 || !negative)) {
peekedLong = negative ? value : -value; peekedLong = negative ? value : -value;
buffer.skip(i); buffer.skip(i);
return peeked = PEEKED_LONG; return peeked = PEEKED_LONG;

View File

@@ -330,6 +330,12 @@ public final class JsonUtf8ReaderTest {
assertThat(reader.nextDouble()).isEqualTo(-92233720368547758080d); assertThat(reader.nextDouble()).isEqualTo(-92233720368547758080d);
} }
@Test public void negativeZeroIsANumber() throws Exception {
JsonReader reader = newReader("-0");
assertEquals(NUMBER, reader.peek());
assertEquals("-0", reader.nextString());
}
@Test public void quotedNumberWithEscape() throws IOException { @Test public void quotedNumberWithEscape() throws IOException {
JsonReader reader = newReader("[\"12\u00334\"]"); JsonReader reader = newReader("[\"12\u00334\"]");
reader.setLenient(true); reader.setLenient(true);