/
encoders.cljc
53 lines (46 loc) · 1.98 KB
/
encoders.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(ns martian.encoders
(:require [clojure.string :as string]
[linked.core :as linked]
[cognitect.transit :as transit]
#?(:clj [clojure.edn :as edn]
:cljs [cljs.reader :as edn])
#?(:clj [cheshire.core :as json])
#?(:clj [clojure.java.io :as io]))
#?(:clj (:import [java.io ByteArrayInputStream ByteArrayOutputStream])))
(defn transit-decode [body type]
#?(:clj (transit/read (transit/reader (ByteArrayInputStream. body) type))
:cljs (transit/read (transit/reader type {}) body)))
(defn transit-encode [body type]
#?(:clj
(let [out (ByteArrayOutputStream. 4096)
writer (transit/writer out type)]
(transit/write writer body)
(io/input-stream (.toByteArray out)))
:cljs
(transit/write (transit/writer type {}) body)))
(defn json-encode [body]
#?(:clj (json/encode body)
:cljs (js/JSON.stringify (clj->js body))))
(defn json-decode [body key-fn]
#?(:clj (json/decode body key-fn)
:cljs
(if-let [v (if-not (string/blank? body) (js/JSON.parse body))]
(js->clj v :keywordize-keys key-fn))))
(defn default-encoders
([] (default-encoders keyword))
([key-fn]
(merge
#?(:clj
{"application/transit+msgpack" {:encode #(transit-encode % :msgpack)
:decode #(transit-decode % :msgpack)
:as :byte-array}
"application/transit+json" {:encode #(transit-encode % :json)
:decode #(transit-decode (.getBytes ^String %) :json)}})
#?(:cljs
{"application/transit+json" {:encode #(transit-encode % :json)
:decode #(transit-decode % :json)}})
(linked/map
"application/edn" {:encode pr-str
:decode edn/read-string}
"application/json" {:encode json-encode
:decode #(json-decode % key-fn)}))))