This commit is contained in:
Michiel Borkent 2021-05-16 23:28:45 +02:00
parent 82aa362710
commit df6e6cbb29
5 changed files with 50 additions and 5 deletions

View file

@ -39,14 +39,28 @@
(defn next-id [] (defn next-id []
(str (java.util.UUID/randomUUID))) (str (java.util.UUID/randomUUID)))
(defonce transit-read-handlers (atom {}))
(defn transit-json-read [^String s] (defn transit-json-read [^String s]
(with-open [bais (java.io.ByteArrayInputStream. (.getBytes s "UTF-8"))] (with-open [bais (java.io.ByteArrayInputStream. (.getBytes s "UTF-8"))]
(let [r (transit/reader bais :json)] (let [r (transit/reader bais :json @transit-read-handlers)]
(transit/read r)))) (transit/read r))))
;; https://www.cognitect.com/blog/2015/9/10/extending-transit
(defn add-transit-read-handler [tag fn]
(let [rh (transit/read-handler fn)]
(swap! transit-read-handlers assoc tag rh)))
(defonce transit-write-handlers (atom {}))
;; https://www.cognitect.com/blog/2015/9/10/extending-transit
(defn add-transit-write-handler [class tag fn]
(let [rh (transit/write-handler tag fn)]
(swap! transit-write-handlers assoc class rh)))
(defn transit-json-write [^String s] (defn transit-json-write [^String s]
(with-open [baos (java.io.ByteArrayOutputStream. 4096)] (with-open [baos (java.io.ByteArrayOutputStream. 4096)]
(let [w (transit/writer baos :json)] (let [w (transit/writer baos :json {:handlers @transit-write-handlers})]
(transit/write w s) (transit/write w s)
(str baos)))) (str baos))))

View file

@ -69,3 +69,9 @@
(defn invoke (defn invoke
([pod-id sym args] (invoke pod-id sym args {})) ([pod-id sym args] (invoke pod-id sym args {}))
([pod-id sym args opts] (impl/invoke-public pod-id sym args opts))) ([pod-id sym args opts] (impl/invoke-public pod-id sym args opts)))
(defn add-transit-read-handler [tag fn]
(impl/add-transit-read-handler tag fn))
(defn add-transit-write-handler [class tag fn]
(impl/add-transit-write-handler class tag fn))

View file

@ -79,3 +79,9 @@
(defn invoke (defn invoke
([pod-id sym args] (invoke pod-id sym args {})) ([pod-id sym args] (invoke pod-id sym args {}))
([pod-id sym args opts] (impl/invoke-public pod-id sym args opts))) ([pod-id sym args opts] (impl/invoke-public pod-id sym args opts)))
(defn add-transit-read-handler [tag fn]
(impl/add-transit-read-handler tag fn))
(defn add-transit-write-handler [class tag fn]
(impl/add-transit-write-handler class tag fn))

View file

@ -36,12 +36,21 @@
(defn transit-json-read [^String s] (defn transit-json-read [^String s]
(with-open [bais (java.io.ByteArrayInputStream. (.getBytes s "UTF-8"))] (with-open [bais (java.io.ByteArrayInputStream. (.getBytes s "UTF-8"))]
(let [r (transit/reader bais :json)] (let [r (transit/reader bais :json {:handlers
{(str ::local-date-time)
(transit/read-handler
(fn [s]
(java.time.LocalDateTime/parse s)))}})]
(transit/read r)))) (transit/read r))))
(defn transit-json-write [^String s] (defn transit-json-write [^String s]
(with-open [baos (java.io.ByteArrayOutputStream. 4096)] (with-open [baos (java.io.ByteArrayOutputStream. 4096)]
(let [w (transit/writer baos :json)] (let [w (transit/writer baos :json {:handlers
{java.time.LocalDateTime
(transit/write-handler
(str ::local-date-time)
(fn [s]
(java.time.LocalDateTime/parse s)))}})]
(transit/write w s) (transit/write w s)
(str baos)))) (str baos))))
@ -193,7 +202,12 @@
(write out (write out
{"status" ["done"] {"status" ["done"]
"id" id "id" id
"value" "#my/other-tag[1]"})) "value" "#my/other-tag[1]"})
pod.test-pod/local-date-time
(write out
{"status" ["done"]
"id" id
"value" (write-fn (java.time.LocalDateTime/now))}))
(recur)) (recur))
:shutdown (System/exit 0) :shutdown (System/exit 0)
:load-ns (let [ns (-> (get message "ns") :load-ns (let [ns (-> (get message "ns")

View file

@ -64,6 +64,10 @@
(def fn-called (pod.test-pod/fn-call inc 2)) (def fn-called (pod.test-pod/fn-call inc 2))
(def local-date-time
(when (= "transit+json" fmt)
(pod.test-pod/local-date-time)))
(require '[pod.test-pod.only-code :as only-code]) (require '[pod.test-pod.only-code :as only-code])
(def should-be-1 (only-code/foo)) (def should-be-1 (only-code/foo))
@ -90,4 +94,5 @@
other-tagged other-tagged
loaded loaded
fn-called fn-called
local-date-time
should-be-1] should-be-1]