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:
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 can't serialize type : class java.time.LocalDate
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:317)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSONSerializers$IterableSerializer.serialize(JSONSerializers.java:290)
at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
at com.mongodb.util.JSON.serialize(JSON.java:54)
at com.mongodb.util.JSON.serialize(JSON.java:40)
at com.mongodb.BasicDBList.toString(BasicDBList.java:38)
at web.MeasureAccess.writeData(MeasureAccess.scala:203)
at 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)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
I am using the mongo-java-driver-2.13.0-rc1.jar
Scala 2.11.4
and java 1.8.0_25
For completeness.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…