Make the nextJsonElement more robust
Add test cases
This commit is contained in:
parent
62a9702385
commit
ac14b4c197
|
@ -250,8 +250,14 @@ public final class JsonTreeReader extends JsonReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonElement nextJsonElement() throws IOException {
|
JsonElement nextJsonElement() throws IOException {
|
||||||
if (peek() == JsonToken.NAME) {
|
final JsonToken peeked = peek();
|
||||||
throw new IllegalStateException("Can't turn a name into a JsonElement");
|
if (
|
||||||
|
peeked == JsonToken.NAME
|
||||||
|
|| peeked == JsonToken.END_ARRAY
|
||||||
|
|| peeked == JsonToken.END_OBJECT
|
||||||
|
|| peeked == JsonToken.END_DOCUMENT
|
||||||
|
) {
|
||||||
|
throw new IllegalStateException("Unexpected " + peeked + " when reading a JsonElement.");
|
||||||
}
|
}
|
||||||
final JsonElement element = (JsonElement) peekStack();
|
final JsonElement element = (JsonElement) peekStack();
|
||||||
skipValue();
|
skipValue();
|
||||||
|
|
|
@ -18,6 +18,7 @@ package com.google.gson.internal.bind;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.JsonPrimitive;
|
||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
@ -298,6 +299,41 @@ public final class JsonElementReaderTest extends TestCase {
|
||||||
reader.endArray();
|
reader.endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testNextJsonElement() throws IOException {
|
||||||
|
final JsonElement element = JsonParser.parseString("{\"A\": 1, \"B\" : {}, \"C\" : []}");
|
||||||
|
JsonTreeReader reader = new JsonTreeReader(element);
|
||||||
|
reader.beginObject();
|
||||||
|
try {
|
||||||
|
reader.nextJsonElement();
|
||||||
|
fail();
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
}
|
||||||
|
reader.nextName();
|
||||||
|
assertEquals(reader.nextJsonElement(), new JsonPrimitive(1));
|
||||||
|
reader.nextName();
|
||||||
|
reader.beginObject();
|
||||||
|
try {
|
||||||
|
reader.nextJsonElement();
|
||||||
|
fail();
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
}
|
||||||
|
reader.endObject();
|
||||||
|
reader.nextName();
|
||||||
|
reader.beginArray();
|
||||||
|
try {
|
||||||
|
reader.nextJsonElement();
|
||||||
|
fail();
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
}
|
||||||
|
reader.endArray();
|
||||||
|
reader.endObject();
|
||||||
|
try {
|
||||||
|
reader.nextJsonElement();
|
||||||
|
fail();
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testEarlyClose() throws IOException {
|
public void testEarlyClose() throws IOException {
|
||||||
JsonElement element = JsonParser.parseString("[1, 2, 3]");
|
JsonElement element = JsonParser.parseString("[1, 2, 3]");
|
||||||
JsonTreeReader reader = new JsonTreeReader(element);
|
JsonTreeReader reader = new JsonTreeReader(element);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user