Cannot serialize LocalDate in Mongodb

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

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

private def writeData(measure: DBCollection, installation: Int, date: String, dates: ListBuffer[LocalDate],
                    values: ListBuffer[BigDecimal], validated: Boolean, overwrite: Boolean) {
  val timeValues: BasicDBList = new BasicDBList
  var i = 0
  while ( i < dates.size )  {
    val obj: BasicDBObject = new BasicDBObject("time", dates(i))
    obj.put("value", values(i).toString())
    timeValues.add(obj)
    i += 1
  }
  if ( debug ) System.out.println("Storedata: " + timeValues.toString) <-- error here

Errorlog:

java.lang.RuntimeException: json cannot serialize type: class java.time.LocalDate to com.mongodb.util.ClassMapBasedObjectSerializer.serialize (ClassMapBasedObjectSerializer.java:77) on com.mongodb.util.JSONSONerializers $ MapSerializer.serialize (. java: 317) at com.mongodb.util.ClassMapBasedObjectSerializer.serialize (ClassMapBasedObjectSerializer.java:79) on com.mongodb.util.JSONSerializers $ IterableSerializer .serialize (JSONSerializers.java:290) at com.mongerialbap.utilizerbut (ClassMapBasedObjectSerializer.java:79) on com.mongodb.util.JSON.serialize (JSON.java:54) on com.mongodb.util.JSON.serialize (JSON.java:40) on com.mongodb.BasicDBList.toString ( BasicDBList.java:38) in the web.MeasureAccess.writeData file (MeasureAccess.scala: 203) in web.MeasureAccess.firstTime (MeasureAccess.scala: 52) at web.MeasureAccess $ .main (MeasureAccess.scala: 262) at web.MeasureAccess.main (MeasureAccess.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0 (native method) at sun.reflect. NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java-00-0083. atex.intex.intex.intex.intex.intex.intex .application.AppMain.main (AppMain.java:134)invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:483) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:134)invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:483) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:134)

mongo-java-driver-2.13.0-rc1.jar Scala 2.11.4 java 1.8.0_25

.

+4
3

, MongoDB java.util.Date, . docs

, LocalDate , :

MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("test");
DBCollection coll = db.getCollection("testcol");

LocalDate ld = LocalDate.now();
Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);

BasicDBObject doc = new BasicDBObject("localdate", date);
coll.insert(doc);

- Morphia Jongo, MongoDB, , mappers, " ", LocalDate ..

+9

, , - . TypeConverter Date LocalDateTime ( , OP LocalDate).

Morphia :

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

:

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()));
    }
}
+4

As Oliver Girke mentions here

This data type is not yet supported. I hope this will be available soon.

+1
source

Source: https://habr.com/ru/post/1570159/


All Articles