gpt4 book ai didi

java - 使用 Gson 将基类列表反序列化为派生类

转载 作者:行者123 更新时间:2023-12-01 06:11:29 35 4
gpt4 key购买 nike

我正在构建 Android 应用,它使用 JSON 与 C# 服务器通信。

我有一个要序列化的数据类(实际上是将接收到的数据反序列化到其中),其中包含派生类的 List 字段,但将它们存储为它们的基类:

public class ToSerializeClass{
@SerializedName("TestString")
private String testString = "TestStringValue";

@SerializedName("DerivedClasses")
private List<BaseClass> derivedClasses;

public List<BaseClass> getDerivedClasses() {
return derivedClasses;
}

public ToSerializeClass(List<BaseClass> derivedClasses){
this.derivedClasses= derivedClasses;
}
}

例如,从 C# 端,如果我收到以下 ToSerializeClass 实例,名为 serializeClass:

List<BaseClass> derivedClasses = new ArrayList<>();
derivedClasses.add(new DerivedClassA());
derivedClasses.add(new DerivedClassB());
ToSerializeClass serializeClass = new ToSerializeClass(derivedClasses);

JSON 字符串将是:

{"__type":"ToSerializeClass","DerivedClasses":[{"__type":"DerivedA","FieldA":"This is a derived class."},{"__type":"DerivedB","FieldB":"This is ANOTHER derived class.", "IntValue":10}],"TestString":"TestStringValue"}

JSON 字符串的 "__type":"SimpleClassName" 字段显示序列化类的简单名称。这些是由 C# 端的序列化器添加的。如有必要,我可以使这些字段消失,但这是 C# 端同一问题的解决方案。如果没有类型,它看起来像这样:

{"DerivedClasses":[{"FieldA":"This is a derived class."},{"FieldB":"This is ANOTHER derived class.", "IntValue":10}],"TestString":"TestStringValue"}

问题是,当我尝试将 JSON 字符串反序列化ToSerializeClass 类的实例时:

Gson serializer = new Gson();
ToSerializeClass deserialized = serializer.fromJson(jsonString, ToSerializeClass.class);

我有一个反序列化类,它是一个ToSerializeClass实例,但是衍生类列表是基类而不是派生类的集合,并且所有派生信息都会丢失。

如何将字符串反序列化为 ToSerializeClass 实例以获得派生类列表?

我可以完全控制源代码,因此我可以修改我的数据类,必要时使用不同的集合,创建一些包装器,修改 JSON 字符串,但我想解决它如果可能的话使用 Gson,并且不需要太多开销即可完成。

谢谢!

编辑:例如DerivedClassADerivedClassB:

public class DerivedClassA extends BaseClass{
@SerializedName("FieldA")
private String fieldA = "This is a derived class.";

...

public DerivedClassA(){
super();
}
}

public class DerivedClassB extends BaseClass{
@SerializedName("FieldB")
private String fieldB = "This is ANOTHER derived class.";

@SerializedName("IntValue")
private int intValue = 10;
...

public DerivedClassB(){
super();
}
}

最佳答案

我创建了一个快速解决方案,它接近我正在寻找的解决方案(我不是 Java 人员,所以如果它伤害了您的大脑,我很抱歉,请随时发表评论建议)。

数据类:

public class BaseClass {
@SerializedName("Method")
private String method;

public void setMethod(String method){
this.method = method;
}

public String getMethod(){
return method;
}

public BaseClass(String method){
this.method = method;
}
}

public class DerivedClassA extends BaseClass{
@SerializedName("FieldA")
private String fieldA = "This is a derived class.";

public DerivedClassA(){
super("ClassA");
}
}

public class DerivedClassB extends BaseClass{
@SerializedName("FieldB")
private String fieldB = "This is ANOTHER derived class.";

@SerializedName("IntValue")
private int intValue = 10;

public DerivedClassB(){
super("ClassB");
}
}

public class ToSerializeClass{
@SerializedName("TestString")
private String testString = "TestStringValue";

@SerializedName("DerivedClasses")
private List<BaseClass> derivedClasses;

public ToSerializeClass(List<BaseClass> derivedClasses){
this.derivedClasses= derivedClasses;
}
}

解决方案:ClassDeserializerAdapter实现:

public class ClassDeserializerAdapter implements JsonDeserializer<BaseClass>
{
private String typeName;
private Gson gson;
private Map<String, Class<? extends BaseClass>> classTypeRegistry;

ClassDeserializerAdapter(String typeName)
{
this.typeName = typeName;
gson = new Gson();
classTypeRegistry = new HashMap<>();
}

void registerClassType(String classTypeName, Class<? extends BaseClass> classType)
{
// registering Types to Strings
classTypeRegistry.put(classTypeName, classType);
}

@Override
public BaseClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException
{
JsonObject jsonObject = json.getAsJsonObject();
JsonElement typeElement = jsonObject.get(typeName);
String method = typeElement.getAsString();
Class<? extends BaseClass> classType = classTypeRegistry.get(method);
BaseClass result = gson.fromJson(json, classType);
return result;
}
}

这对我来说效果很好,甚至不需要临时类(class)等。

注意:通过反射,它可以自动注册从 BaseClass 继承的每个类,您甚至不需要处理注册过程。

示例:main()方法的主体是:

// **SERIALIZATION PART** (nothing special, simple Gson serialization)
// Creating a list to pass as parameter to the container class
List<BaseClass> derivedClasses = new ArrayList<>();
derivedClasses.add(new DerivedClassA());
derivedClasses.add(new DerivedClassB());
// Creating the container class to be serialized
ToSerializeClass serializeClass = new ToSerializeClass(derivedClasses);

Gson gson = new Gson();

String json = gson.toJson(serializeClass);
// json = {"TestString":"TestStringValue","DerivedClasses":[{"FieldA":"This is a derived class.","Method":"ClassA"},{"FieldB":"This is ANOTHER derived class.","IntValue":10,"Method":"ClassB"}]}


// **DESERIALIZATION PART** (with custom deserializer)
// creating the custom deserializer, which will find the derived class' type as the class' "Method" field value. With that value, it can resolve the type.. see below
ClassDeserializerAdapter deserializer = new ClassDeserializerAdapter("Method");
// registering each Type into the Deserializer's HashMap (key-value pair), where the key (String) must be carried by the object (you can find it in the BaseClass, called "Method")
deserializer.registerClassType("ClassA", DerivedClassA.class);
deserializer.registerClassType("ClassB", DerivedClassB.class);
Gson gsonB = new GsonBuilder().registerTypeAdapter(BaseClass.class, deserializer).create();

// deserializing
ToSerializeClass deserialized = gsonB.fromJson(json, ToSerializeClass.class); // CORRECT!

关于java - 使用 Gson 将基类列表反序列化为派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857091/

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