Custom Transit writer for Joda time in Clojure

783 Views Asked by At

Clojure's transit does not support the Joda time format out of the box. How can I add write support for org.joda.time.DateTime?

2

There are 2 best solutions below

2
On BEST ANSWER

Add this function:

(def joda-time-writer
  (transit/write-handler
   (constantly "m")
   #(-> % coerce/to-date .getTime)
   #(-> % coerce/to-date .getTime .toString)))

And use it like this:

(transit/writer out :json
                    {:handlers {org.joda.time.DateTime joda-time-writer}})
0
On

To get this to work with ring-middleware-format, do this, using the joda-time-writer function posted by David J.

(defn wrap-format [handler]
  (let [transit-opts {:handlers {org.joda.time.DateTime
                                 joda-time-writer}}]
    (wrap-restful-format handler
                         {:response-options
                          {:transit-json transit-opts
                           :transit-messagepack transit-opts}})))