monger
monger copied to clipboard
Can't find a codec for class java.time.Instant.
When inserting ( monger.collection/insert) a document having
java.time.Instant` in values, error is thrown:
Can't find a codec for class java.time.Instant.
Since monger includes support for joda-time codecs, maybe it would be a good idea to include built-in java.time.Instant
too?
This piece of code can be used as a workaround:
(ns monger.java-instant
(:import [java.time Instant]
[java.util Date])
(:require [monger.conversion :refer :all]))
;;
;; API
;;
(extend-protocol ConvertToDBObject
java.time.Instant
(to-db-object [^Instant input]
(to-db-object (Date/from input))))
(extend-protocol ConvertFromDBObject
java.time.Instant
(from-db-object [^java.time.Instant input keywordize]
(java.time.Instant/parse input)))
;;
;; Reader extensions
;;
(defmethod print-dup java.time.Instant
[^java.time.Instant d out]
(print-dup (Date/from d) out))
;;
;; JSON serialization
;;
(require 'clojurewerkz.support.json)
Hint: Possibly updating mongo-java-driver to 4.1 may resolve this issue as it seems that codes for java.time.Instant
are implemented in their code.
@rgtk we would consider a PR that upgrades the driver and adds a test :)
This issue is not reproduced anymore. Tested on version 3.5.0
You are welcome to submit a PR
Well, the only thing that can be added are two additional tests for conversions between Instant
and mongo object. I can make a PR for that if you want me to