JodaTime LocalDate/LocalTime not Parsing with custom JSON Serializer classes

两盒软妹~` 提交于 2019-12-06 06:15:18

The problem lies with the code you've got that's serializing the data.

In your original question, you have this code when you're going to deserialize (retrieveGlobalDataFromStorage):

final GsonBuilder builder = new GsonBuilder()
   .registerTypeAdapter(LocalDate.class, new LocalDateSerializer())
   .registerTypeAdapter(LocalTime.class, new LocalTimeSerializer());
final Gson gson = builder.create();  

But when you're going to serialize (storeGlobalData) you just have:

Gson gson = new Gson();

You should be registering the type adapters in both places. I'd extract that code (Gson initialization) to a separate method which you can call from both your methods.

It's also worth to be careful in deserialize() with

    return fmt.parseLocalDate(json.toString());

as it produces the likely unexpected quotation marks ""2014-10-28"".

It may be better to use instead:

    return fmt.parseLocalDate(json.getAsString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!