使用Gson显示枚举的地图及其属性

时间:2012-05-07 16:41:37

标签: java enums gson

我正在使用Gson,我想序列化并显示枚举。

我的枚举:

Tuple{

    SINGLE(1,"Single"), 
    DOUBLE(2,"Double")

    Tuple(int asInt,String properName){
        this.asInt=asInt;
        this.fullName=fullName;
    }

    public int AsInt;
    public String fullname;
}

我希望输出如下:

(SINGLE:{asInt:1,fullName:'Single'},DOUBLE:{asInt:2,fullname:'Double'})

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果我没有弄错,似乎你需要一些自定义序列化和反序列化。查看this链接。它有一个非常自我描述的例子,可以作为kickstart使用。