JodaTime LocalDate JSON反序列化ISO 8601问题

时间:2014-10-17 04:00:11

标签: android json serialization jodatime iso8601

我有一个LocalDate被序列化为JSON"2014-10-17"。当我尝试使用下面提到的代码解析它时,我得到一个logcat错误,格式不正确。

这是logcat:

10-16 22:55:30.123: E/AndroidRuntime(31319): FATAL EXCEPTION: main
10-16 22:55:30.123: E/AndroidRuntime(31319): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.cnc/com.nanospark.cnc.MainActivity}: java.lang.IllegalArgumentException: Invalid format: ""2014-10-17""
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.os.Looper.loop(Looper.java:137)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at java.lang.reflect.Method.invoke(Method.java:525)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at dalvik.system.NativeStart.main(Native Method)
10-16 22:55:30.123: E/AndroidRuntime(31319): Caused by: java.lang.IllegalArgumentException: Invalid format: ""2014-10-17""
10-16 22:55:30.123: E/AndroidRuntime(31319):    at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:854)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:798)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.nanospark.cnc.LocalDateSerializer.deserialize(LocalDateSerializer.java:37)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.nanospark.cnc.LocalDateSerializer.deserialize(LocalDateSerializer.java:1)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.Gson.fromJson(Gson.java:805)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.Gson.fromJson(Gson.java:770)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.google.gson.Gson.fromJson(Gson.java:719)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.nanospark.cnc.GlobalData.retrieveGlobalDataFromStorage(GlobalData.java:115)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at com.nanospark.cnc.MainActivity.onCreate(MainActivity.java:35)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.Activity.performCreate(Activity.java:5133)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 22:55:30.123: E/AndroidRuntime(31319):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-16 22:55:30.123: E/AndroidRuntime(31319):    ... 11 more

以及处理序列化和反序列化的类:

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();
      Log.v("My Date Deserialized", je.getAsString());
      if (je.isJsonNull() || 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);
      }
      Log.v("My Date Serialized", retVal);
      return new JsonPrimitive(retVal);
   }
}   

yyyy-MM-dd是否被视为ISO 8601格式?

1 个答案:

答案 0 :(得分:0)

问题原因是我的dateAsString值附近添加了引号。

只需用parseLocalDate(dateAsString)替换parseLocalDate(je.getAsString())就可以解决问题。