在Clojure中为Joda时间定制Transit作家

时间:2014-08-29 19:57:04

标签: clojure transit

Clojure's transit不支持开箱即用的Joda时间格式。如何为org.joda.time.DateTime添加写支持?

2 个答案:

答案 0 :(得分:8)

添加此功能:

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

并像这样使用它:

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

答案 1 :(得分:0)

要使用ring-middleware-format工作,请使用David J发布的joda-time-writer函数执行此操作。

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