Using Enums while parsing JSON with GSON

I want to expand a bit NAZIK/user2724653 answer (for my case). Here is a Java code:

public class Item {
    @SerializedName("status")
    private Status currentState = null;

    // other fields, getters, setters, constructor and other code...

    public enum Status {
        @SerializedName("0")
        BUY,
        @SerializedName("1")
        DOWNLOAD,
        @SerializedName("2")
        DOWNLOADING,
        @SerializedName("3")
        OPEN
     }
}

in the json file you have just a field "status": "N",, where N=0,1,2,3 – depend on the Status values. So that’s all, GSON works fine with the values for the nested enum class. In my case i’ve parsed a list of Items from json array:

List<Item> items = new Gson().<List<Item>>fromJson(json,
                                          new TypeToken<List<Item>>(){}.getType());

Leave a Comment