spring-projects / spring-data-couchbase

Provides support to increase developer productivity in Java when using Couchbase. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-couchbase
Apache License 2.0
277 stars 191 forks source link

LocalDateTime converter compatible with Java SDK #1203

Open mikereiche opened 3 years ago

mikereiche commented 3 years ago

LocalDateTime converter compatible with Java SDK. This was requested by a specific customer that uses both spring-data-couchbase and the Java SDK.

private CouchbaseJsr310Converters() {
...
    public static Collection<Converter<?, ?>> getConvertersToRegister() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        // converters.add(NumberToLocalDateTimeConverter.INSTANCE);
        // converters.add(LocalDateTimeToLongConverter.INSTANCE);
        converters.add(LocalDateTimeToStringConverter.INSTANCE);
        converters.add(StringToLocalDateTimeConverter.INSTANCE);
    @ReadingConverter
    public enum StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {

        INSTANCE;

        @Override
        public LocalDateTime convert(String source) {
            return source == null ? null : LocalDateTime.parse(source);
        }
    }

    @WritingConverter
    public enum LocalDateTimeToStringConverter implements Converter<LocalDateTime, String> {

        INSTANCE;

        @Override
        public String convert(LocalDateTime source) {
            return source == null ? null : source.toString();
        }

    }
mikereiche commented 2 years ago

It looks like this has been added.

mikereiche commented 1 year ago

I'm not sure about that last comment - it does NOT appear to have been added. There is still only the conversion from/to Number (not string).

mikereiche commented 1 year ago

It seems that adding just StringToLocal... converters would allow spring data couchbase to read what was written by the java sdk. Writing is a bit tricky, as changing the serialization to String (on write) would cause problems for older versions of spring-data-couchbase that do not have the StringToLocal... converters. On the other hand, the current situation of serializing to Number on write (maybe) causes problems for reading with the java sdk. Maybe the java sdk can deserialize Numbers to LocalDateTime etc (?).