diff --git a/src/main/asciidoc/entity.adoc b/src/main/asciidoc/entity.adoc index c2c42647..069f114c 100644 --- a/src/main/asciidoc/entity.adoc +++ b/src/main/asciidoc/entity.adoc @@ -271,6 +271,7 @@ A populated object can look like: ---- ==== +Optionally, Date can be converted to and from ISO-8601 compliant strings by setting system property `org.springframework.data.couchbase.useISOStringConverterForDate` to true. If you want to override a converter or implement your own one, this is also possible. The library implements the general Spring Converter pattern. You can plug in custom converters on bean creation time in your configuration. Here's how you can configure it (in your overridden `AbstractCouchbaseConfiguration`): .Custom Converters diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseJsr310Converters.java b/src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseJsr310Converters.java index 9b04c141..0c065cd3 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseJsr310Converters.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseJsr310Converters.java @@ -81,7 +81,7 @@ public final class CouchbaseJsr310Converters { @Override public LocalDateTime convert(Number source) { return source == null ? null : ofInstant( - DateConverters.NumberToDateConverter.INSTANCE.convert(source) + DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source) .toInstant(), systemDefault()); } } @@ -107,7 +107,7 @@ public final class CouchbaseJsr310Converters { @Override public LocalDate convert(Number source) { return source == null ? null : ofInstant(ofEpochMilli( - DateConverters.NumberToDateConverter.INSTANCE.convert(source).getTime()), + DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source).getTime()), systemDefault()).toLocalDate(); } } @@ -132,7 +132,7 @@ public final class CouchbaseJsr310Converters { @Override public LocalTime convert(Number source) { return source == null ? null : ofInstant(ofEpochMilli( - DateConverters.NumberToDateConverter.INSTANCE.convert(source) + DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source) .getTime()), systemDefault()).toLocalTime(); } } @@ -156,7 +156,7 @@ public final class CouchbaseJsr310Converters { @Override public Instant convert(Number source) { - return source == null ? null : DateConverters.NumberToDateConverter.INSTANCE.convert(source).toInstant(); + return source == null ? null : DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source).toInstant(); } } diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java b/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java index 78ff3cde..68fa95e6 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors + * Copyright 2012-2017 the original author or authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package org.springframework.data.couchbase.core.convert; +import static java.time.ZoneId.systemDefault; + +import java.time.Instant; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -36,6 +39,7 @@ import org.springframework.util.ClassUtils; * Out of the box conversions for java dates and calendars. * * @author Michael Nitschinger + * @author Subhashni Balakrishnan */ public final class DateConverters { @@ -52,9 +56,18 @@ public final class DateConverters { public static Collection> getConvertersToRegister() { List> converters = new ArrayList>(); - converters.add(DateToLongConverter.INSTANCE); + boolean useISOStringConverterForDate = Boolean.parseBoolean( + System.getProperty("org.springframework.data.couchbase.useISOStringConverterForDate", "false")); + + if (useISOStringConverterForDate) { + converters.add(DateToStringConverter.INSTANCE); + } else { + converters.add(DateToLongConverter.INSTANCE); + } + + converters.add(SerializedObjectToDateConverter.INSTANCE); + converters.add(CalendarToLongConverter.INSTANCE); - converters.add(NumberToDateConverter.INSTANCE); converters.add(NumberToCalendarConverter.INSTANCE); if (JODA_TIME_IS_PRESENT) { @@ -71,6 +84,38 @@ public final class DateConverters { return converters; } + @WritingConverter + public enum DateToStringConverter implements Converter { + INSTANCE; + + @Override + public String convert(Date source) { + return source == null ? null : source.toInstant().toString(); + } + } + + @ReadingConverter + public enum SerializedObjectToDateConverter implements Converter { + INSTANCE; + + @Override + public Date convert(Object source) { + if (source == null) { + return null; + } + if (source instanceof Number) { + Date date = new Date(); + date.setTime(((Number) source).longValue()); + return date; + } else if (source instanceof String) { + return Date.from(Instant.parse((String) source).atZone(systemDefault()).toInstant()); + } else { + //Unsupported serialized object + return null; + } + } + } + @WritingConverter public enum DateToLongConverter implements Converter { INSTANCE; @@ -91,22 +136,6 @@ public final class DateConverters { } } - @ReadingConverter - public enum NumberToDateConverter implements Converter { - INSTANCE; - - @Override - public Date convert(Number source) { - if (source == null) { - return null; - } - - Date date = new Date(); - date.setTime(source.longValue()); - return date; - } - } - @ReadingConverter public enum NumberToCalendarConverter implements Converter { INSTANCE;