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?
Asked
Active
Viewed 746 times
2 Answers
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
-
2This is explained in more detail here: http://increasinglyfunctional.com/2014/09/02/custom-transit-writers-clojure-joda-time/ – David J. Sep 02 '14 at 21:59
-
Where does `out` come from? – Petrus Theron Jun 29 '16 at 11:03
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