package org.springframework.data.cloudant.core;

import com.google.gson.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.lang.reflect.Type;

/**
 * Created by kevin on 7/14/15.
 */
public class DateTimeDataAdapter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
    private final DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZoneUTC();

    @Override
    public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
        return new JsonPrimitive(fmt.print(src));
    }

    @Override
    public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
            throws JsonParseException {
        return fmt.parseDateTime(json.getAsString());
    }
}


@@ -134,16 +134,60 @@
 meZone;%0A
+import org.joda.time.format.DateTimeFormat;%0A
 import o
@@ -225,16 +225,16 @@
 matter;%0A
-
 import o
@@ -303,16 +303,134 @@
 ct.Type;
+%0Aimport java.text.ParseException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Arrays;%0Aimport java.util.Locale;
 %0A%0A/**%0A *
@@ -647,16 +647,198 @@
 neUTC();
+%0A    private static final String%5B%5D DATE_FORMATS = new String%5B%5D %7B%0A            %22yyyy-MM-dd'T'HH:mm:ssZ%22,%0A            %22yyyy-MM-dd HH:mm:ss Z%22,%0A            %22yyyy-MM-dd HH:mm:ss z%22%0A    %7D;
 %0A%0A    @O
@@ -1162,54 +1162,429 @@
     
-return fmt.parseDateTime(json.getAsString(
+for (String format : DATE_FORMATS) %7B%0A            try %7B%0A                return DateTime.parse(json.getAsString(), DateTimeFormat.forPattern(format));%0A            %7D catch (IllegalArgumentException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A        throw new JsonParseException(%22Unparseable date: %5C%22%22 + json.getAsString()%0A                + %22%5C%22. Supported formats: %22 + Arrays.toString(DATE_FORMATS
 ));%0A
+%0A
     

Enable deserializer to support multi date formats