简体   繁体   中英

Deserialize JSON using GSON to Enum

My goal is to parse a JSON file or part of it to an Enum class in Java. I can do it easily, but for debugging reasons I would like to include a default value as well.

public enum MyEnum {
    @SerializedName("texts")
    TEXTS,
    @SerializedName("buttons")
    BUTTONS,
    @SerializedName("inputs")
    INPUTS,
    DEFAULT
}

So if I try to parse this JSON (using a wrapper class):

{enum: "buttons"}

I would get MyEnum.BUTTONS , but if I try to parse:

{enum: "divider"}

I would still like to know the value. I would like to map all values (eg "divider", "line", "color",...) to DEFAULT (maintaining the String which mapped to the DEFAULT value). Would it be possible to save value "divider" to MyEnum.DEFAULT property?

Implement a custom JsonDeserializer and install it using GsonBuilder.registerTypeAdapter . Your enum implementation could then be implemented as follows:

public enum MyEnum {
    TEXTS ("texts"),
    BUTTONS("buttons"),
    INPUTS("inputs"),
    DEFAULT(null);

    private String text;

    private MyEnum (String text) {
        this.text = text;
    }

    public String getValue () {
        return text;
    }

    public static MyEnum mapFrom (String serialized) {
        if (serialized == null) {
            DEFAULT.text = null;
            return DEFAULT;
        }
        for (MyEnum inst : MyEnum.values()) {
            if (serialized.equals(inst.getValue())) {
                return inst;
            }
        }
        DEFAULT.text = serialized;
        return DEFAULT;
    }
}

The implementation of your deserializer (and serializer, too) then uses the mapFrom method of your enum. The side effect is that the DEFAULT instance always has the latest value on a global scope, so I wouldn't recommend this approach if used wider than debugging, and to be honest I wouldn't expect another programmer to understand why I wrote this.

However, if you extract the serialized forms of your enums out of the enum definition, then in your deserializer you need to implement a switch that decides which enum to deserialize into. If you also separate your debugging code and call it from your deserializer, you don't need the serialized form in your enum anymore. Moreover, you separate different functionality - the Single Responsibility Principle .

The only other option left is using a non-enum class, so you can instantiate new objects upon deserialization time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM