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; token = JsonToken.NUMBER;
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
// this must be an unquoted string // 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.common.TestTypes.BagOfPrimitives;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import junit.framework.TestCase;
import java.io.CharArrayReader; import java.io.CharArrayReader;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;
import java.io.StringReader; import java.io.StringReader;
import junit.framework.TestCase;
/** /**
* Unit test for {@link JsonParser} * Unit test for {@link JsonParser}
@ -40,10 +38,11 @@ public class JsonParserTest extends TestCase {
} }
public void testParseUnquotedStringArrayFails() { public void testParseUnquotedStringArrayFails() {
try { JsonElement element = parser.parse("[a,b,c]");
parser.parse("[a,b,c]"); assertEquals("a", element.getAsJsonArray().get(0).getAsString());
fail(); assertEquals("b", element.getAsJsonArray().get(1).getAsString());
} catch (JsonSyntaxException expected) {} assertEquals("c", element.getAsJsonArray().get(2).getAsString());
assertEquals(3, element.getAsJsonArray().size());
} }
public void testParseString() { public void testParseString() {
@ -66,10 +65,7 @@ public class JsonParserTest extends TestCase {
} }
public void testParseUnquotedSingleWordStringFails() { public void testParseUnquotedSingleWordStringFails() {
try { assertEquals("Test", parser.parse("Test").getAsString());
parser.parse("Test");
fail();
} catch (JsonSyntaxException expected) { }
} }
public void testParseUnquotedMultiWordStringFails() { public void testParseUnquotedMultiWordStringFails() {

View File

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

View File

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