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

时间:2013-07-09 16:45:10

标签: java gson

鉴于

DefaultAConfig implements AConfig
DefaultLConfig implements LConfig
DefaultConfiguration implements Configuration

和json文件位于externalConfigFile,我

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 个答案:

没有答案