clojuretransit

Custom Transit writer for Joda time in Clojure


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?


Solution

  • 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}})