Gson 通过重新启动应用程序枚举松散值
Gson enum loose values by restart of application
我正在开发在玩游戏时会产生一些效果的游戏
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
我检查我拥有的效果数,leftTime
是效果结束的时间。
为了有效地读写,我使用 HashMap
在运行时处理效果
private Map<String, EnumCollection.Effect> gameEffects = new HashMap<>();
并将其作为 String
存储在数据库中。
现在我使用 Gson 通过游戏初始化从字符串 解析效果 并通过保存 到字符串
// 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);
...
}
现在。
我可以成功关闭和打开应用程序。在数据库中成功写入和读取数据。
通过重新申请,我有 stored 和 leftTime 值,这是我期望的。
但是
一旦我重新启动应用程序Android Studio
重新运行按钮,存储和leftTime 是 0.
当我记录效果字符串时,我收到:
effects: {"ShowSame":"ShowSame","DoublePoints":"DoublePoints","ShowOne":"ShowOne","IncreaseSwitch":"IncreaseSwitch"}
对于 @SerializedName
,是否应该存储这些值?
我错过了什么?
TypeToken
有什么问题吗?
通过从 enum
到 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;
}
}
从那个
Type collectionType = new TypeToken<HashMap<String, EffectItem>>() {
}.getType();
gameEffects = new Gson().fromJson(game.getEffects(), collectionType);
按预期工作。并且 HashMap
private Map<String, EffectItem> gameEffects = new HashMap<>();
仍可用于更好地访问效果
我正在开发在玩游戏时会产生一些效果的游戏
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
我检查我拥有的效果数,leftTime
是效果结束的时间。
为了有效地读写,我使用 HashMap
在运行时处理效果
private Map<String, EnumCollection.Effect> gameEffects = new HashMap<>();
并将其作为 String
存储在数据库中。
现在我使用 Gson 通过游戏初始化从字符串 解析效果 并通过保存 到字符串
// 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);
...
}
现在。
我可以成功关闭和打开应用程序。在数据库中成功写入和读取数据。
通过重新申请,我有 stored 和 leftTime 值,这是我期望的。
但是
一旦我重新启动应用程序Android Studio
重新运行按钮,存储和leftTime 是 0.
当我记录效果字符串时,我收到:
effects: {"ShowSame":"ShowSame","DoublePoints":"DoublePoints","ShowOne":"ShowOne","IncreaseSwitch":"IncreaseSwitch"}
对于 @SerializedName
,是否应该存储这些值?
我错过了什么?
TypeToken
有什么问题吗?
通过从 enum
到 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;
}
}
从那个
Type collectionType = new TypeToken<HashMap<String, EffectItem>>() {
}.getType();
gameEffects = new Gson().fromJson(game.getEffects(), collectionType);
按预期工作。并且 HashMap
private Map<String, EffectItem> gameEffects = new HashMap<>();
仍可用于更好地访问效果