У меня есть 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' кавычки? Кажется, что это сообщение об ошибке. –
Позвольте мне проверить это. – Tukajo
Да, похоже, это проблема; есть ли способ удалить кавычки? в противном случае я просто заменил 'parseLocalDate (dateAsString)' for 'parseLocalDate (je.getAsString())' – Tukajo