我正在尝试将从服务器获取的 JSON 反序列化为其原始 POJO 形式。我知道原始对象(我有服务器代码的来源),但似乎我遗漏了一些东西。
这是我可以组装的最小代码示例,它说明了我的问题:
package mycompany.mypackage;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
public class GsonSerializeTest {
/* main method to illustrate the problem */
public static void main(String[] args) {
Serializable[] identifiers= {"ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321};
EntityUid uid = new GsonSerializeTest().new EntityUid(identifiers);
Gson converter = new GsonBuilder().registerTypeAdapter(Serializable.class, new GsonSerializeTest().new SerializableInstanceCreator()).create();
String json = converter.toJson(uid);
System.out.println("Converted to string: " + json);
EntityUid uid2 = converter.fromJson(json, EntityUid.class); // ERROR
System.out.println("Converted back to object: " + uid2);
}
/* the POJO that gets serialized and fails while deserializing */
public class EntityUid implements Serializable {
private final List<Serializable> identifier = new ArrayList<Serializable>();
public EntityUid(final Serializable... identifier) {
for (Serializable partialIdentifier : identifier) {
this.identifier.add(partialIdentifier);
}
}
}
/* Class for generating Serializable instances */
public class SerializableInstanceCreator implements InstanceCreator<Serializable> {
public Serializable createInstance(Type arg0) {
return new String();
}
}
}
这准备了 EntityUid 对象,将它(就像服务器一样)序列化为:
Converted to string: {"identifier":["ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321]}
然后尝试将其反序列化为原始对象,但失败并显示以下内容:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at mycompany.mypackage.GsonSerializeTest.main(GsonSerializeTest.java:19)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
... 10 more
有人可以为我提供一个关于我可能缺少什么的指针吗?提前非常感谢!