Fork me on GitHub
#luminus
<
2017-04-12
>
yogthos04:04:06

@juliobarros it looks like you'd have to specify custom transit encoder/decoder in the options: https://github.com/metosin/muuntaja#default-options something like

(defn joda-time-writer [t]
  ...)
then in the options:
...
:encoder [(partial formats/make-transit-encoder
                  :json
                   {:handlers {org.joda.time.DateTime joda-time-writer})]
...

yogthos05:04:36

so here's a full working version for the encoder:

(ns ...
   (:require
    ...
    [cognitect.transit :as transit]
    [clj-time.coerce :as coerce]
    [muuntaja.core :as m]
    [muuntaja.format.transit :as formats]))

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

(defn wrap-formats [handler]
  (let [wrapped (wrap-params
                  (wrap-format
                    handler
                    (update
                      m/default-options
                      :formats
                      merge
                      {"application/transit+json"
                       {:decoder [(partial formats/make-transit-decoder :json)]
                        :encoder [#(formats/make-transit-encoder
                                        :json
                                        (merge
                                          %
                                         {:handlers {org.joda.time.DateTime joda-time-writer}}))]}})))]
    (fn [request]
      ;; disable wrap-formats for websockets
      ;; since they're not compatible with this middleware
      ((if (:websocket? request) handler wrapped) request))))

yogthos05:04:16

@ikitommi might be an idea to add an example in docs ^