Support unquoted single word strings in lenient mode!

Fixes issue 282.
This commit is contained in:
Jesse Wilson 2011-02-10 01:36:27 +00:00
parent 6e81cfdbb4
commit b649f2768c
4 changed files with 14 additions and 23 deletions

View File

@ -1097,7 +1097,8 @@ public final class JsonReader implements Closeable {
token = JsonToken.NUMBER;
} catch (NumberFormatException ignored) {
// this must be an unquoted string
throw syntaxError("invalid number or unquoted string");
checkLenient();
token = JsonToken.STRING;
}
}
}

View File

@ -18,12 +18,10 @@ package com.google.gson;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.stream.JsonReader;
import junit.framework.TestCase;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.StringReader;
import junit.framework.TestCase;
/**
* Unit test for {@link JsonParser}
@ -40,10 +38,11 @@ public class JsonParserTest extends TestCase {
}
public void testParseUnquotedStringArrayFails() {
try {
parser.parse("[a,b,c]");
fail();
} catch (JsonSyntaxException expected) {}
JsonElement element = parser.parse("[a,b,c]");
assertEquals("a", element.getAsJsonArray().get(0).getAsString());
assertEquals("b", element.getAsJsonArray().get(1).getAsString());
assertEquals("c", element.getAsJsonArray().get(2).getAsString());
assertEquals(3, element.getAsJsonArray().size());
}
public void testParseString() {
@ -66,10 +65,7 @@ public class JsonParserTest extends TestCase {
}
public void testParseUnquotedSingleWordStringFails() {
try {
parser.parse("Test");
fail();
} catch (JsonSyntaxException expected) { }
assertEquals("Test", parser.parse("Test").getAsString());
}
public void testParseUnquotedMultiWordStringFails() {

View File

@ -572,10 +572,7 @@ public class PrimitiveTest extends TestCase {
}
public void testUnquotedStringDeserializationFails() throws Exception {
try {
gson.fromJson("UnquotedSingleWord", String.class);
fail();
} catch (JsonSyntaxException expected) { }
assertEquals("UnquotedSingleWord", gson.fromJson("UnquotedSingleWord", String.class));
String value = "String Blah Blah Blah...1, 2, 3";
try {

View File

@ -16,10 +16,9 @@
package com.google.gson.stream;
import junit.framework.TestCase;
import java.io.IOException;
import java.io.StringReader;
import junit.framework.TestCase;
public final class JsonReaderTest extends TestCase {
@ -565,10 +564,7 @@ public final class JsonReaderTest extends TestCase {
JsonReader reader = new JsonReader(new StringReader("[a]"));
reader.setLenient(true);
reader.beginArray();
try {
reader.nextString();
fail();
} catch (MalformedJsonException expected) { }
assertEquals("a", reader.nextString());
}
public void testStrictSingleQuotedStrings() throws IOException {
@ -761,7 +757,8 @@ public final class JsonReaderTest extends TestCase {
JsonReader reader = new JsonReader(new StringReader(")]}' []"));
reader.setLenient(true);
try {
reader.beginArray();
assertEquals(")", reader.nextString());
reader.nextString();
fail();
} catch (IOException expected) {
}