monger icon indicating copy to clipboard operation
monger copied to clipboard

Can't find a codec for class java.time.Instant.

Open nounder opened this issue 4 years ago • 6 comments

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?

nounder avatar Sep 17 '20 11:09 nounder

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)

nounder avatar Sep 17 '20 11:09 nounder

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.

nounder avatar Sep 17 '20 12:09 nounder

@rgtk we would consider a PR that upgrades the driver and adds a test :)

michaelklishin avatar Sep 23 '20 12:09 michaelklishin

This issue is not reproduced anymore. Tested on version 3.5.0

coder11 avatar Nov 05 '21 19:11 coder11

You are welcome to submit a PR

michaelklishin avatar Nov 07 '21 18:11 michaelklishin

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

coder11 avatar Nov 07 '21 18:11 coder11