Serializing custom object that contains JodaTime objects into JSON

后端 未结 1 613
甜味超标
甜味超标 2021-01-02 19:07

After running a second activity that creates an object and stores it into an ArrayList inside of a Singleton I get this error when returning to the

相关标签:
1条回答
  • 2021-01-02 19:51

    Converters from https://github.com/gkopff/gson-javatime-serialisers are for java.time classes from Java8, e.g. java.time.LocalDate.

    But you need converters for org.joda.time classes from 3rd part library. You can create custom converters for it, like:

    Converter for org.joda.time.LocalDate

    public class LocalDateSerializer implements JsonDeserializer<LocalDate>, JsonSerializer<LocalDate>
    {
       private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.date();
    
       @Override
       public LocalDate deserialize(final JsonElement je, final Type type,
                               final JsonDeserializationContext jdc) throws JsonParseException
       {
          final String dateAsString = je.getAsString();
          if (dateAsString.length() == 0)
          {
             return null;
          }
          else
          {
             return DATE_FORMAT.parseLocalDate(dateAsString);         
          }
       }
    
       @Override
       public JsonElement serialize(final LocalDate src, final Type typeOfSrc,
                                    final JsonSerializationContext context)
       {
          String retVal;
          if (src == null)
          {
             retVal = "";
          }
          else
          {
             retVal = DATE_FORMAT.print(src);
          }
          return new JsonPrimitive(retVal);
       }
    }   
    

    Converter for org.joda.time.LocalTime

    public class LocalTimeSerializer implements JsonDeserializer<LocalTime>, JsonSerializer<LocalTime>
    {
    
       private static final DateTimeFormatter TIME_FORMAT = ISODateTimeFormat.timeNoMillis();
    
       @Override
       public LocalTime deserialize(final JsonElement je, final Type type,
                               final JsonDeserializationContext jdc) throws JsonParseException
       {
          final String dateAsString = je.getAsString();
          if (dateAsString.length() == 0)
          {
             return null;
          }
          else
          {
             return TIME_FORMAT.parseLocalTime(dateAsString);         
          }
       }
    
       @Override
       public JsonElement serialize(final LocalTime src, final Type typeOfSrc,
                                    final JsonSerializationContext context)
       {
          String retVal;
          if (src == null)
          {
             retVal = "";
          }
          else
          {
             retVal = TIME_FORMAT.print(src);
          }
          return new JsonPrimitive(retVal);
       }
    
    }  
    

    Usage

    public void retrieveGlobalDataFromStorage(Context context)
    {  
    // ...  
    
        final GsonBuilder builder = new GsonBuilder()
           .registerTypeAdapter(LocalDate.class, new LocalDateSerializer())
           .registerTypeAdapter(LocalTime.class, new LocalTimeSerializer());
        final Gson gson = builder.create();  
    // ...
    
    0 讨论(0)
提交回复
热议问题