我正在尝试将对象保存到共享的首选项中,然后进行检索。
该对象包括当日的单个DateTime,以及最大跌幅股票的对象列表(股票符号为字符串,百分比变化为双倍)。
对象模型是:
public class DayLosersSharedPreference {
LocalDateTime dateTime;
List<PercentageChange> percentageChangesList;
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
public List<PercentageChange> getPercentageChangesList() {
return percentageChangesList;
}
public void setPercentageChangesList(List<PercentageChange> percentageChangesList) {
this.percentageChangesList = percentageChangesList;
}
}
将单个对象(包含DateTime和对象List)传递给我的共享首选项类进行存储,如下所示:
public class SharedPreferences {
Context context = MySuperAppApplication.getContext();
android.content.SharedPreferences sharedPreferences = context.getSharedPreferences(STRINGS.LOSERS_SP(), Context.MODE_PRIVATE);
Gson gson = new Gson();
public void storeLosers(DayLosersSharedPreference losers) {
System.out.println("Object being stored date: " + losers.getDateTime());
System.out.println("Object being stored array: " + losers.getPercentageChangesList());
String dailyLosersJson = gson.toJson(losers);
System.out.println("Object being stored as Json String: " + dailyLosersJson);
sharedPreferences.edit()
.putString(STRINGS.DAILY_LOSERS_SP(), dailyLosersJson)
.apply();
}
}
println()的输出显示在Json String转换良好之前的对象:
I/System.out: Object being stored date: 2019-03-18T00:00
I/System.out: Object being stored array: [com.roboticc.alpha.Models.PercentageChange@8bef7ab, com.roboticc.alpha.Models.PercentageChange@207be08, com.roboticc.alpha.Models.PercentageChange@b6289a1, com.roboticc.alpha.Models.PercentageChange@269d7c6, com.roboticc.alpha.Models.PercentageChange@ff36387, com.roboticc.alpha.Models.PercentageChange@45eb2b4, com.roboticc.alpha.Models.PercentageChange@1fd1edd, com.roboticc.alpha.Models.PercentageChange@41baa52, com.roboticc.alpha.Models.PercentageChange@b18b123, com.roboticc.alpha.Models.PercentageChange@38e4620]
但是我如何将其转换为Json字符串存在问题。当我查看转换后的输出时,它丢失了DateTime元素:
I/System.out: Object being stored as Json String: {"dateTime":{},"percentageChangesList":[{"percentageChange":-0.15908367801463796,"symbol":"AAL"},{"percentageChange":0.0,"symbol":"AAME"},{"percentageChange":0.19011406844106543,"symbol":"AABA"},{"percentageChange":0.500000000000002,"symbol":"AAOI"},{"percentageChange":0.6455517450070613,"symbol":"AAWW"},{"percentageChange":0.8957770510450668,"symbol":"AAXJ"},{"percentageChange":1.0208467655276197,"symbol":"AAPL"},{"percentageChange":1.081223628691974,"symbol":"ABCB"},{"percentageChange":2.0748867159551576,"symbol":"AAON"},{"percentageChange":4.29524603836531,"symbol":"AAXN"}]}
一旦我从共享的首选项中检索到它,可以预见的是,日期时间会得到一个空指针,因为它没有保存,但可以访问百分比更改列表。
我认为从对象到Json字符串的转换方式有问题,我是否需要做类似传递模型类型的事情?还是不能以这种格式将DateTime存储在Json中?
感谢您的帮助
编辑:我能够将DateTime更改为字符串,并在检索后将其转换回来以解决此问题。我仍然很感兴趣,为什么没有DateTime转换就不能直接使用它。特别是考虑到可以传递整个对象列表。
答案 0 :(得分:1)
gson不知道LocalDateTime对象,因此无法解析它。如果确实希望它直接解析LocalDateTime而不必先将其转换为String,则必须告诉gson HOW使用registerTypeAdaptor解析LocalDateTime。
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
@Override
public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Instant instant = Instant.ofEpochMilli(json.getAsJsonPrimitive().getAsLong());
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
}).create();
这也许是GSON的较新版本可以处理的:https://github.com/google/gson/pull/1266/files
我不确定上面的提交是否已经发布到他们的任何发行版中,但是可以肯定的是某些未来版本将能够自动处理此问题。