0
我有一個LocalDate
被序列化爲JSON
爲:"2014-10-17"
。當我嘗試用下面提到的代碼解析它時,我得到一個格式不正確的logcat錯誤。JodaTime LocalDate JSON反序列化ISO 8601問題
這裏是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格式?
'dateAsString'是否包含引號?錯誤消息似乎表明。 – 2014-10-17 04:04:49
讓我測試一下。 – Tukajo 2014-10-17 04:05:31
是的,似乎是問題;有沒有辦法刪除引號?否則,我只是將'parseLocalDate(dateAsString)'替換爲'parseLocalDate(je.getAsString())' – Tukajo 2014-10-17 04:08:22