Gson enum loose values by restart of application

Viewed 34

I am working on game that has some effects while playing

public enum Effect {
    DoublePoints(),
    IncreaseSwitch(),
    ShowOne(),
    ShowSame();

    @SerializedName("stored")
    int stored;
    @SerializedName("leftTime")
    int leftTime;

    Effect() {

    }

    public int getStored() {
        return stored;
    }

    public void setStored(int stored) {
        this.stored = stored;
    }

    public int getLeftTime() {
        return leftTime;
    }

    public void setLeftTime(int leftTime) {
        this.leftTime = leftTime;
    }
}

with stored i check the count of effects i have, and leftTime is time till end of effect.

to read and write effectively i use a HashMap to work with effects in runtime

private Map<String, EnumCollection.Effect> gameEffects = new HashMap<>();

and store it as String in DB.

Now i use Gson to parse the effects from string by initialization of game and to string by save.

// read
public void setEffects() {
    if (game.getEffects() == null) {
        gameEffects.put(EnumCollection.Effect.DoublePoints.toString(), EnumCollection.Effect.DoublePoints);

        EnumCollection.Effect switchEffect = EnumCollection.Effect.IncreaseSwitch;
        switchEffect.setLeftTime(3);
        gameEffects.put(EnumCollection.Effect.IncreaseSwitch.toString(), switchEffect);
        gameEffects.put(EnumCollection.Effect.ShowOne.toString(), EnumCollection.Effect.ShowOne);
        gameEffects.put(EnumCollection.Effect.ShowSame.toString(), EnumCollection.Effect.ShowSame);
    } else {
        Type collectionType = new TypeToken<HashMap<String, EnumCollection.Effect>>() {
        }.getType();
        gameEffects = new Gson().fromJson(game.getEffects(), collectionType);
    }
}

// write
private void saveGameValues() {
    String effectsString = new Gson().toJson(gameEffects);
    ...
}

Now.

I can success close and open application. Data writed and readed successfully in DB.

By repen of application i have stored and leftTime values, that i expect.

BUT

As soon i restart the application with Android Studio rerun button, stored and leftTime are 0.

When i log effects String i receive:

effects: {"ShowSame":"ShowSame","DoublePoints":"DoublePoints","ShowOne":"ShowOne","IncreaseSwitch":"IncreaseSwitch"}

With @SerializedName the values should be stored, or not?

What am i missing?

Is there are some problem with TypeToken ?

1 Answers

Solved by going from enum to Object

public class EffectItem {

    int stored;
    int leftTime;

    public int getStored() {
        return stored;
    }

    public void setStored(int stored) {
        this.stored = stored;
    }

    public int getLeftTime() {
        return leftTime;
    }

    public void setLeftTime(int leftTime) {
        this.leftTime = leftTime;
    }
}

from that

Type collectionType = new TypeToken<HashMap<String, EffectItem>>() {
}.getType();
gameEffects = new Gson().fromJson(game.getEffects(), collectionType);

works as expected. And HashMap

private Map<String, EffectItem> gameEffects = new HashMap<>();

could still be used to better access to effects

Related