Clojure encode Joda DateTime with ring-json Clojure encode Joda DateTime with ring-json json json

Clojure encode Joda DateTime with ring-json


If you're using Cheshire to generate JSON, you can extend its protocol to handle serialization then it should "just work":

(extend-protocol cheshire.generate/JSONable  org.joda.time.DateTime  (to-json [dt gen]    (cheshire.generate/write-string gen (str dt))))


Additionally, the following modification made it work for projects that are using the time library tick.alpha.api. I was getting the error

#error {:cause Cannot JSON encode object of class: class java.time.Instant: 2019-10-23T00:31:40.668Z :via [{:type com.fasterxml.jackson.core.JsonGenerationException   :message Cannot JSON encode object of class: class java.time.Instant: 2019-10-23T00:31:40.668Z   :at [cheshire.generate$generate invokeStatic generate.clj 152]}]

Implementing the following in the file db/core.clj fixed the issue for me.

(extend-protocol cheshire.generate/JSONable  java.time.Instant  (to-json [dt gen]    (cheshire.generate/write-string gen (str dt))))