Cannot serialize LocalDate in Mongodb

前端 未结 3 467
闹比i
闹比i 2021-01-13 07:38

I am using the java 8 java.time.LocalDate to parse around dates.

But trying to insert a LocalDate object to mongodb. I get errors in the java driver:



        
3条回答
  •  攒了一身酷
    2021-01-13 07:49

    For anyone coming across this question, the following converter would get someone started in the right direction. This TypeConverter converts between Date and LocalDateTime (Making this distinction because the OP specifically asked about LocalDate).

    Add the following converter to Morphia as follows:

    morphia.getMapper().getConverters().addConverter(new LocalDateTimeConverter());
    

    Here is the converter class:

    public class LocalDateTimeConverter extends TypeConverter implements SimpleValueConverter {
    
        public LocalDateTimeConverter() {
            // TODO: Add other date/time supported classes here
            // Other java.time classes: LocalDate.class, LocalTime.class
            // Arrays: LocalDateTime[].class, etc
            super(LocalDateTime.class);
        }
    
        @Override
        public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
            if (fromDBObject == null) {
                return null;
            }
    
            if (fromDBObject instanceof Date) {
                return ((Date) fromDBObject).toInstant().atZone(ZoneOffset.systemDefault()).toLocalDateTime();
            }
    
            if (fromDBObject instanceof LocalDateTime) {
                return fromDBObject;
            }
    
            // TODO: decode other types
    
            throw new IllegalArgumentException(String.format("Cannot decode object of class: %s", fromDBObject.getClass().getName()));
        }
    
        @Override
        public Object encode(Object value, MappedField optionalExtraInfo) {
            if (value == null) {
                return null;
            }
    
            if (value instanceof Date) {
                return value;
            }
    
            if (value instanceof LocalDateTime) {
                ZonedDateTime zoned = ((LocalDateTime) value).atZone(ZoneOffset.systemDefault());
                return Date.from(zoned.toInstant());
            }
    
            // TODO: encode other types
    
            throw new IllegalArgumentException(String.format("Cannot encode object of class: %s", value.getClass().getName()));
        }
    }
    

提交回复
热议问题