Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nounder opened this issue Sep 17, 2020 · 6 comments
Open

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

nounder opened this issue Sep 17, 2020 · 6 comments

Comments

@nounder
Copy link

nounder commented Sep 17, 2020

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
Copy link
Author

nounder commented Sep 17, 2020

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
Copy link
Author

nounder commented Sep 17, 2020

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.

@michaelklishin
Copy link
Owner

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

@coder11
Copy link

coder11 commented Nov 5, 2021

This issue is not reproduced anymore. Tested on version 3.5.0

@michaelklishin
Copy link
Owner

You are welcome to submit a PR

@coder11
Copy link

coder11 commented Nov 7, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants