gpt4 book ai didi

java - Gson 在尝试字符串化对象时抛出 UnsupportedOperationException,该对象包含 java.lang.Class 作为字段

转载 作者:行者123 更新时间:2023-12-05 04:10:37 26 4
gpt4 key购买 nike

我写了 ClassTypeAdapter,它对类来说是正确的,但是当我尝试使用包含类作为字段的对象时失败了。

具有可执行 main 方法的 ClassTypeAdapter 可重现附加问题。

有什么想法吗?

    import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class ClassTypeAdapter extends TypeAdapter<Class<?>> {
private static final String PARAM_NAME = "className";

@Override
public void write(JsonWriter out, Class<?> value) throws IOException {
out.beginObject();

out.name(PARAM_NAME).value(value.getName());

out.endObject();
}

@Override
public Class<?> read(JsonReader in) throws IOException {
Class<?> readClass = null;

in.beginObject();

while (in.hasNext()) {
if (PARAM_NAME.equals(in.nextName())) {
try {
readClass = Class.forName(in.nextString());
} catch (ClassNotFoundException e) {
throw new IOException("Class not found", e);
}
}
}

in.endObject();

return readClass;
}

public static class TestClass<T> {
private Class<T> aClass;
}

public static void main(String[] args) {
final Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter()).create();

final TestClass testClass = new TestClass<>();

System.out.println(gson.toJson(testClass)); // {}
System.out.println(gson.toJson(Object.class)); // {"className":"java.lang.Object"}

testClass.aClass = Object.class;

System.out.println(gson.toJson(testClass)); // UnsupportedOperationException
}
}

错误堆栈跟踪:

Exception in thread "main" java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: java.lang.Object. Forgot to register a type adapter?
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:76)
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:69)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson.toJson(Gson.java:669)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
at ClassTypeAdapter.main(ClassTypeAdapter.java:56)

最佳答案

我找到了解决方案 here .

public class ClassTypeAdapter implements JsonSerializer<Class<?>>, JsonDeserializer<Class<?>> {
@Override
public JsonElement serialize(Class<?> src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getName());
}

@Override
public Class<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return Class.forName(json.getAsString());
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
}

JUnit 测试:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ClassTypeAdapterTest {
@Test
public void testReadWrite() {
final Class<?> classToWrite = ClassTypeAdapter.class;

final Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter()).create();

final String writtenClass = gson.toJson(classToWrite);
final Class readClass = gson.fromJson(writtenClass, Class.class);

assertEquals(classToWrite, readClass);
}

@Test
public void testInnerClassProblem() {
final Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter()).create();

final TestClass testClass = new TestClass<>();
testClass.innerClass = Object.class;

final String writtenClass = gson.toJson(testClass);
final TestClass readClass = gson.fromJson(writtenClass, TestClass.class);

assertEquals(testClass.innerClass, readClass.innerClass);
}

private static class TestClass<T> {
private Class<T> innerClass;
}
}

关于java - Gson 在尝试字符串化对象时抛出 UnsupportedOperationException,该对象包含 java.lang.Class 作为字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com