7

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?

David J.
  • 31,569
  • 22
  • 122
  • 174

2 Answers2

9

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}})
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
David J.
  • 31,569
  • 22
  • 122
  • 174
0

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}})))
Arne Brasseur
  • 1,468
  • 13
  • 18