Disabled escaping of single quote since it is valid for JSON (though invalid for JavaScript).

Added null checks in contexts since the custom type adapter can pass nulls.
This commit is contained in:
Inderjeet Singh 2009-10-06 17:10:52 +00:00
parent d416361ac5
commit 536a968b32
5 changed files with 41 additions and 7 deletions

View File

@ -54,8 +54,10 @@ class Escaper {
htmlEscapeSet.add('>');
htmlEscapeSet.add('&');
htmlEscapeSet.add('=');
htmlEscapeSet.add('\'');
// htmlEscapeSet.add('/'); -- Removing slash for now since it causes some incompatibilities
// Removing ' for now since it is a valid character in JSON, but not javascript
// When enabling this, remember to enable the test EscaperTest.disable_testSingleQuoteEscaping
// htmlEscapeSet.add('\'');
// htmlEscapeSet.add('/'); -- Removing slash for now since it causes some incompatibilities
HTML_ESCAPE_CHARS = Collections.unmodifiableSet(htmlEscapeSet);
}

View File

@ -43,14 +43,14 @@ final class JsonDeserializationContextDefault implements JsonDeserializationCont
@SuppressWarnings("unchecked")
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
if (json.isJsonArray()) {
if (json == null || json.isJsonNull()) {
return null;
} else if (json.isJsonArray()) {
return (T) fromJsonArray(typeOfT, json.getAsJsonArray(), this);
} else if (json.isJsonObject()) {
return (T) fromJsonObject(typeOfT, json.getAsJsonObject(), this);
} else if (json.isJsonPrimitive()) {
return (T) fromJsonPrimitive(typeOfT, json.getAsJsonPrimitive(), this);
} else if (json.isJsonNull()) {
return null;
} else {
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json");
}

View File

@ -39,6 +39,9 @@ final class JsonSerializationContextDefault implements JsonSerializationContext
}
public JsonElement serialize(Object src) {
if (src == null) {
return JsonNull.createJsonNull();
}
return serialize(src, src.getClass());
}

View File

@ -65,7 +65,7 @@ public class EscaperTest extends TestCase {
assertEquals("123\\\"456", escapedString);
}
public void testSingleQuoteEscaping() throws Exception {
public void disable_testSingleQuoteEscaping() throws Exception {
String containsQuote = "123'456";
String escapedString = escapeHtmlChar.escapeJsonString(containsQuote);
assertEquals("123\\'456", escapedString);

View File

@ -21,6 +21,8 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives;
@ -173,7 +175,34 @@ public class NullObjectAndFieldTest extends TestCase {
ObjectWithField obj = gson.fromJson(json, ObjectWithField.class);
assertNull(obj.value);
}
public void testCustomTypeAdapterPassesNullSerialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonSerializer<ObjectWithField>() {
public JsonElement serialize(ObjectWithField src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(null);
}
}).create();
ObjectWithField target = new ObjectWithField();
target.value = "value1";
String json = gson.toJson(target);
assertFalse(json.contains("value1"));
}
public void testCustomTypeAdapterPassesNullDesrialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonDeserializer<ObjectWithField>() {
public ObjectWithField deserialize(JsonElement json, Type type,
JsonDeserializationContext context) {
return context.deserialize(null, type);
}
}).create();
String json = "{value:'value1'}";
ObjectWithField target = gson.fromJson(json, ObjectWithField.class);
assertFalse("value1".equals(target.value));
}
private static class ObjectWithField {
String value = "";
}