DATACOUCH-147 - Allow to convert Date to ISO-8601 compliant strings
instead of long Using the system property org.springframework.data.couchbase.useISOStringConverterForDate the conversion can be enabled so this doesn't break backward compatibility Original pull request: #143.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Converter<?, ?>> getConvertersToRegister() {
|
||||
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
|
||||
|
||||
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<Date, String> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(Date source) {
|
||||
return source == null ? null : source.toInstant().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum SerializedObjectToDateConverter implements Converter<Object, Date> {
|
||||
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<Date, Long> {
|
||||
INSTANCE;
|
||||
@@ -91,22 +136,6 @@ public final class DateConverters {
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum NumberToDateConverter implements Converter<Number, Date> {
|
||||
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<Number, Calendar> {
|
||||
INSTANCE;
|
||||
|
||||
Reference in New Issue
Block a user