A type adapter for Class that throws an UnsupportedOperationException.

This commit is contained in:
Inderjeet Singh 2011-12-22 22:31:43 +00:00
parent 1c850dc3ea
commit bd937fe7b5
3 changed files with 71 additions and 11 deletions

View File

@ -241,6 +241,7 @@ public final class Gson {
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization)); factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
factories.add(ArrayTypeAdapter.FACTORY); factories.add(ArrayTypeAdapter.FACTORY);
factories.add(TypeAdapters.ENUM_FACTORY); factories.add(TypeAdapters.ENUM_FACTORY);
factories.add(TypeAdapters.CLASS_FACTORY);
factories.add(reflectiveTypeAdapterFactory); factories.add(reflectiveTypeAdapterFactory);
this.factories = Collections.unmodifiableList(factories); this.factories = Collections.unmodifiableList(factories);

View File

@ -53,6 +53,24 @@ import java.util.UUID;
public final class TypeAdapters { public final class TypeAdapters {
private TypeAdapters() {} private TypeAdapters() {}
@SuppressWarnings("rawtypes")
public static final TypeAdapter<Class> CLASS = new TypeAdapter<Class>() {
@Override
public void write(JsonWriter out, Class value) throws IOException {
if (value == null) {
return;
}
throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: "
+ value.getName() + ". Forgot to register a type adapter?");
}
@Override
public Class read(JsonReader in) throws IOException {
throw new UnsupportedOperationException(
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
}
};
public static final TypeAdapter.Factory CLASS_FACTORY = newFactory(Class.class, CLASS);
public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() { public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() {
public BitSet read(JsonReader in) throws IOException { public BitSet read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) { if (in.peek() == JsonToken.NULL) {

View File

@ -15,17 +15,7 @@
*/ */
package com.google.gson.functional; package com.google.gson.functional;
import com.google.gson.Gson; import java.io.IOException;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -49,8 +39,24 @@ import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.UUID; import java.util.UUID;
import junit.framework.TestCase; import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/** /**
* Functional test for Json serialization and deserialization for common classes for which default * Functional test for Json serialization and deserialization for common classes for which default
* support is provided in Gson. The tests for Map types are available in {@link MapTest}. * support is provided in Gson. The tests for Map types are available in {@link MapTest}.
@ -77,6 +83,24 @@ public class DefaultTypeAdaptersTest extends TestCase {
super.tearDown(); super.tearDown();
} }
public void testClassSerialization() {
try {
gson.toJson(String.class);
} catch (UnsupportedOperationException expected) {}
// Override with a custom type adapter for class.
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
assertEquals("\"java.lang.String\"", gson.toJson(String.class));
}
public void testClassDeserialization() {
try {
gson.fromJson("String.class", String.class.getClass());
} catch (UnsupportedOperationException expected) {}
// Override with a custom type adapter for class.
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
assertEquals(String.class, gson.fromJson("java.lang.String", Class.class));
}
public void testUrlSerialization() throws Exception { public void testUrlSerialization() throws Exception {
String urlValue = "http://google.com/"; String urlValue = "http://google.com/";
URL url = new URL(urlValue); URL url = new URL(urlValue);
@ -633,4 +657,21 @@ public class DefaultTypeAdaptersTest extends TestCase {
StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class); StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class);
assertEquals("abc", sb.toString()); assertEquals("abc", sb.toString());
} }
@SuppressWarnings("rawtypes")
private static class MyClassTypeAdapter extends TypeAdapter<Class> {
@Override
public void write(JsonWriter out, Class value) throws IOException {
out.value(value.getName());
}
@Override
public Class read(JsonReader in) throws IOException {
String className = in.nextString();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
}
} }