如何使用一个gson反序列化器使用泛型反序列化不同的子类型

时间:2017-02-03 09:38:47

标签: java generics inheritance gson type-erasure

我必须为不同的测试用例保存配置对象。我决定使用GSON,因为我已经使用过了。问题是配置文件的结构/继承在其定义中包含泛型。如果这是重复的,我已阅读该主题,但无法将其应用于我的问题。代码被细分为最低限度。

配置类

除了其他易于序列化的组件外,它还有一个ArrayList<T>

//There are classes that inherit from that too
public class Configuration<T extends ParamSet> implements Iterable<T>{

    // this list is what i want to set from my ApplicationClass
    private ArrayList<T> _paramSets = new ArrayList<T>();

    //getter, setter and constructor
}

参数集定义

这些课程没什么特别之处。只需整数和布尔值。

public class ParamSet {}

public class ChildParamSet extends ParamSet{}

抽象应用类

这是保存过程开始的地方。我已经定义了一个抽象方法,后来在运行时已知T的类中实现。

public abstract class ApplicationClass<T extends ParamSet>{

    private Configuration<T> config;

    // in this method i want to set either a new config or new params in the config
    public setupConfig(){
        //some checks and if the user wants to load a config from file
        loadConfig(file);       
    }
    //abstract method to be called in more concrete classes
    abstract protected Configuration<T> loadConfig(File file);
}

具体应用类

那么我知道T的类型,我想调用getConfiguration方法。

public abstract class MoreConcreteApplicationClass extends ApplicationClass<ChildParamSet>{

    //constructor and other stuff

    @Override
    protected Configuration<ChildParamSet> loadConfig(File file){
        return ConfigurationPersistenceHelper.getConfiguration(file);
    }   
}

配置持久性助手 - 问题

public class ConfigurationPersistenceHelper(){

    //i get the configuration with a linked treeMap --> wrong type 
    public static <T extends ParamSet> Configuration<T> getConfiguration(final File location){
        GsonBuilder builder = new GsonBuilder();
        GsonBuilder builder.enableComplexMapKeySerialization.create();

        final Type type = new TypeToken<Configuration<T>>(){}.getType();

        final String jsonRepresentation =  new String(Files.readAllBytes(location.toPath()));
        return gson.fromJson(jsonRepresentation, type);

    }
}

所以问题是ConfigurationPersistenceHelper中的getConfiguration方法。当我运行此方法时,而不是ArrayList<ChildParamSet>我在配置中获得Linked TreeMap

我想这是因为在运行时期间对泛型的类型擦除以及我使用T extends ParamSet定义方法。

但我该如何解决这个问题呢?是否有另一种方法来调用方法,以便类型不会丢失?

我知道

的变通方法

我知道我可以在每个具体应用程序类的loadConfig方法中实现反序列化逻辑。但是这会导致很多重复的代码。这是我目前的解决方案。

我也知道我可以将类型从loadConfig(类型在运行时仍然可用)传递给序列化器到getConfiguration方法。这是不好的做法吗?

我也知道我可以编写自己的反序列化程序但是我还需要运行时的类型,这是我目前的问题。

1 个答案:

答案 0 :(得分:1)

是的,Type Erasure就是问题所在。只需传递类型信息:

abstract protected Configuration<T> loadConfig(File file, Class<T> clazz);

在该方法中使用ArrayList的自定义ParameterizedType,请参阅: