Don't permit a type adapter for String to be registered.
This commit is contained in:
parent
7b61e784c0
commit
3df2db1f16
|
@ -446,7 +446,7 @@ public final class GsonBuilder {
|
||||||
|| typeAdapter instanceof JsonDeserializer<?>
|
|| typeAdapter instanceof JsonDeserializer<?>
|
||||||
|| typeAdapter instanceof InstanceCreator<?>
|
|| typeAdapter instanceof InstanceCreator<?>
|
||||||
|| typeAdapter instanceof TypeAdapter<?>);
|
|| typeAdapter instanceof TypeAdapter<?>);
|
||||||
if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
|
if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type) || type == String.class) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Cannot register type adapters for " + type);
|
"Cannot register type adapters for " + type);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,11 @@
|
||||||
|
|
||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,6 +29,14 @@ import junit.framework.TestCase;
|
||||||
* @author Inderjeet Singh
|
* @author Inderjeet Singh
|
||||||
*/
|
*/
|
||||||
public class GsonBuilderTest extends TestCase {
|
public class GsonBuilderTest extends TestCase {
|
||||||
|
private static final TypeAdapter<Object> NULL_TYPE_ADAPTER = new TypeAdapter<Object>() {
|
||||||
|
@Override public void write(JsonWriter out, Object value) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
@Override public Object read(JsonReader in) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public void testCreatingMoreThanOnce() {
|
public void testCreatingMoreThanOnce() {
|
||||||
GsonBuilder builder = new GsonBuilder();
|
GsonBuilder builder = new GsonBuilder();
|
||||||
|
@ -39,6 +51,24 @@ public class GsonBuilderTest extends TestCase {
|
||||||
assertEquals("{\"d\":\"d\"}", gson.toJson(new HasModifiers()));
|
assertEquals("{\"d\":\"d\"}", gson.toJson(new HasModifiers()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRegisterTypeAdapterForUnsupportedType() {
|
||||||
|
Type[] types = {
|
||||||
|
byte.class,
|
||||||
|
int.class,
|
||||||
|
double.class,
|
||||||
|
Short.class,
|
||||||
|
Long.class,
|
||||||
|
String.class,
|
||||||
|
};
|
||||||
|
for (Type type : types) {
|
||||||
|
try {
|
||||||
|
new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER);
|
||||||
|
fail(type.toString());
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
static class HasModifiers {
|
static class HasModifiers {
|
||||||
private String a = "a";
|
private String a = "a";
|
||||||
|
|
Loading…
Reference in New Issue
Block a user