A type adapter for Class that throws an UnsupportedOperationException.
This commit is contained in:
parent
1c850dc3ea
commit
bd937fe7b5
@ -241,6 +241,7 @@ public final class Gson {
|
||||
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
|
||||
factories.add(ArrayTypeAdapter.FACTORY);
|
||||
factories.add(TypeAdapters.ENUM_FACTORY);
|
||||
factories.add(TypeAdapters.CLASS_FACTORY);
|
||||
factories.add(reflectiveTypeAdapterFactory);
|
||||
|
||||
this.factories = Collections.unmodifiableList(factories);
|
||||
|
@ -53,6 +53,24 @@ import java.util.UUID;
|
||||
public final class 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 BitSet read(JsonReader in) throws IOException {
|
||||
if (in.peek() == JsonToken.NULL) {
|
||||
|
@ -15,17 +15,7 @@
|
||||
*/
|
||||
package com.google.gson.functional;
|
||||
|
||||
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.reflect.TypeToken;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@ -49,8 +39,24 @@ import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
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
|
||||
* 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();
|
||||
}
|
||||
|
||||
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 {
|
||||
String urlValue = "http://google.com/";
|
||||
URL url = new URL(urlValue);
|
||||
@ -633,4 +657,21 @@ public class DefaultTypeAdaptersTest extends TestCase {
|
||||
StringBuffer sb = gson.fromJson("'abc'", StringBuffer.class);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user