serialize and deserialize enum with Gson [duplikat]

To pytanie ma już odpowiedź tutaj:

Jak mogę serializować i deserializować proste wyliczenie w gson 2.2.4 ?

public enum Color {

    RED, BLUE, YELLOW;
}
Author: MikO, 2013-05-24

3 answers

Według dokumentacja Gson API, Gson zapewnia domyślną serializację / deserializację Enum, więc zasadniczo powinna być serializowana i deserializowana przy użyciu standardowych metod toJson i fromJson, jak w przypadku każdego innego typu.

 24
Author: MikO,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-25 13:04:05

Możesz spróbować tego.

import com.google.gson.annotations.SerializedName;

public enum Color {

    @SerializedName("0")
    RED (0), 

    @SerializedName("1")
    BLUE (1),

    @SerializedName("2")
    YELLOW (2);

    private final int value;
    public int getValue() {
        return value;
    }

    private Color(int value) {
        this.value = value;
    }

}
 150
Author: Julio Rodrigues,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-07 23:01:28

To też działa dobrze, Nie wiem z której wersji GSON jednak:

public enum OrderLineTimeRegistrationStatus {
    None(0), Started(1), Paused(2);

    private int value;

    private OrderLineTimeRegistrationStatus(int value)
    {
        this.value=value;
    }

    public int getValue()
    {
        return(value);
    }
}
 3
Author: Bart Burg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 16:10:31