Get JsonReader2 to the point that it's passing most tests.

Still missing: 
 - non-execute prefixes
 - rolling back 'pos' when a double fails to parse
 - octal prefix failures
This commit is contained in:
Jesse Wilson 2012-08-26 04:02:09 +00:00
parent c5c65ba626
commit 46b4346505
2 changed files with 1530 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -16,10 +16,14 @@
package com.google.gson.stream; package com.google.gson.stream;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
public final class JsonReaderTest extends TestCase { public final class JsonReaderTest extends TestCase {
@ -425,7 +429,24 @@ public final class JsonReaderTest extends TestCase {
assertEquals(-9223372036854775808L, reader.nextLong()); assertEquals(-9223372036854775808L, reader.nextLong());
} }
/**
* This test fails because there's no double for -9223372036854775809, and our
* long parsing uses Double.parseDouble() for fractional values.
*/
public void testPeekLargerThanLongMinValue() throws IOException { public void testPeekLargerThanLongMinValue() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[-9223372036854775809]"));
reader.setLenient(true);
reader.beginArray();
assertEquals(JsonToken.NUMBER, reader.peek());
try {
reader.nextLong();
fail();
} catch (NumberFormatException expected) {
}
assertEquals(-9223372036854775809d, reader.nextDouble());
}
public void testPeekMuchLargerThanLongMinValue() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[-92233720368547758080]")); JsonReader reader = new JsonReader(new StringReader("[-92233720368547758080]"));
reader.setLenient(true); reader.setLenient(true);
reader.beginArray(); reader.beginArray();
@ -572,6 +593,18 @@ public final class JsonReaderTest extends TestCase {
reader.close(); reader.close();
} }
public void testIntegerMismatchFailuresDoNotAdvance() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[1.5]"));
reader.beginArray();
try {
reader.nextInt();
fail();
} catch (NumberFormatException expected) {
}
assertEquals(1.5d, reader.nextDouble());
reader.endArray();
}
public void testStringNullIsNotNull() throws IOException { public void testStringNullIsNotNull() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[\"null\"]")); JsonReader reader = new JsonReader(new StringReader("[\"null\"]"));
reader.beginArray(); reader.beginArray();
@ -1022,6 +1055,22 @@ public final class JsonReaderTest extends TestCase {
} }
} }
public void testStrictTopLevelString() {
JsonReader reader = new JsonReader(new StringReader("\"a\""));
try {
reader.nextBoolean();
fail();
} catch (IOException expected) {
}
}
public void testLenientTopLevelString() throws IOException {
JsonReader reader = new JsonReader(new StringReader("\"a\""));
reader.setLenient(true);
assertEquals("a", reader.nextString());
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
}
public void testStrictTopLevelValueType() { public void testStrictTopLevelValueType() {
JsonReader reader = new JsonReader(new StringReader("true")); JsonReader reader = new JsonReader(new StringReader("true"));
try { try {
@ -1108,18 +1157,18 @@ public final class JsonReaderTest extends TestCase {
} }
public void testFailWithPosition() throws IOException { public void testFailWithPosition() throws IOException {
testFailWithPosition("Expected literal value at line 6 column 3", testFailWithPosition("Expected value at line 6 column 3",
"[\n\n\n\n\n0,}]"); "[\n\n\n\n\n0,}]");
} }
public void testFailWithPositionGreaterThanBufferSize() throws IOException { public void testFailWithPositionGreaterThanBufferSize() throws IOException {
String spaces = repeat(' ', 8192); String spaces = repeat(' ', 8192);
testFailWithPosition("Expected literal value at line 6 column 3", testFailWithPosition("Expected value at line 6 column 3",
"[\n\n" + spaces + "\n\n\n0,}]"); "[\n\n" + spaces + "\n\n\n0,}]");
} }
public void testFailWithPositionIsOffsetByBom() throws IOException { public void testFailWithPositionIsOffsetByBom() throws IOException {
testFailWithPosition("Expected literal value at line 1 column 4", testFailWithPosition("Expected value at line 1 column 4",
"\ufeff[0,}]"); "\ufeff[0,}]");
} }
@ -1284,6 +1333,45 @@ public final class JsonReaderTest extends TestCase {
assertEquals(JsonToken.STRING, reader.peek()); assertEquals(JsonToken.STRING, reader.peek());
} }
public void testEmptyStringName() throws IOException {
JsonReader reader = new JsonReader(new StringReader("{\"\":true}"));
reader.setLenient(true);
assertEquals(JsonToken.BEGIN_OBJECT, reader.peek());
reader.beginObject();
assertEquals(JsonToken.NAME, reader.peek());
assertEquals("", reader.nextName());
assertEquals(JsonToken.BOOLEAN, reader.peek());
assertEquals(true, reader.nextBoolean());
assertEquals(JsonToken.END_OBJECT, reader.peek());
reader.endObject();
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
}
public void testStrictExtraCommasInMaps() throws IOException {
JsonReader reader = new JsonReader(new StringReader("{\"a\":\"b\",}"));
reader.beginObject();
assertEquals("a", reader.nextName());
assertEquals("b", reader.nextString());
try {
reader.peek();
fail();
} catch (IOException expected) {
}
}
public void testLenientExtraCommasInMaps() throws IOException {
JsonReader reader = new JsonReader(new StringReader("{\"a\":\"b\",}"));
reader.setLenient(true);
reader.beginObject();
assertEquals("a", reader.nextName());
assertEquals("b", reader.nextString());
try {
reader.peek();
fail();
} catch (IOException expected) {
}
}
private String repeat(char c, int count) { private String repeat(char c, int count) {
char[] array = new char[count]; char[] array = new char[count];
Arrays.fill(array, c); Arrays.fill(array, c);