有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用接口反序列化gson,需要澄清

给定

DefaultAConfig implements AConfig
DefaultLConfig implements LConfig
DefaultConfiguration implements Configuration

以及位于externalConfigFile的json文件,I

Gson gson = new GsonBuilder()
        .registerTypeAdapter(Configuration.class, new CInstanceCreator())
        .registerTypeAdapter(LConfig.class, new LInstanceCreator())
        .registerTypeAdapter(AConfig.class, new AInstanceCreator())
        .create();

config = gson.fromJson(readFile(externalConfigFile), DefaultConfiguration.class);

public static class AInstanceCreator implements InstanceCreator<AConfig> {
    public AConfig createInstance(Type type) {
        return new DefaultAConfig();
    }
}

public static class LInstanceCreator implements InstanceCreator<LConfig> {
    public LConfig createInstance(Type type) {
        return new DefaultLConfig();
    }
}

public static class CInstanceCreator implements InstanceCreator<Configuration> {
    public Configuration createInstance(Type type) {
        return new DefaultConfiguration();
    }
}

但据我所见,未设置字段:

DefaultConfiguration{config={k1=23, k2=word}, sectionL=sectionL{default_level='null', impl='null', levels=[]}, sectionA=[DefaultAConfig{format='null', filename='null', rotation='null', threshold=null}, DefaultAConfig{format='null', filename='null', rotation='null', threshold=null}]}

Json结构如下所示:

{
    "config": {
        "k1": "23",
        "k2": "word"
    },
    "sectionL": {
        "default_level": "whatever",
        "impl": "whatever",
        "levels": [
            "a",
            "b"
        ]
    },
    "sectionA": [
        {
            "format": "text",
            "filename": "/a/b/c",
            "rotation": "asdasd",
            "threshold": "isdals"
        } ,
        {
            "format": "text",
            "filename": "/a/b/d",
            "rotation": "asdasd",
            "threshold": "isdals"
        }
    ]
}

为什么没有设置sectionAsectionL内容


共 (0) 个答案