forked from FundingCircle/jackdaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edn.clj
52 lines (43 loc) · 1.65 KB
/
edn.clj
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
(ns jackdaw.serdes.edn
"DEPRECATION NOTICE:
This namespace is deprecated. Please use jackdaw.serdes/edn-serde.
The behavior of the new EDN serde is different. It does not print
the newline.
Implements an EDN SerDes (Serializer/Deserializer)."
{:license "BSD 3-Clause License <https://github.com/FundingCircle/jackdaw/blob/master/LICENSE>"}
(:require [clojure.edn]
[jackdaw.serdes.fn :as jsfn])
(:import java.nio.charset.StandardCharsets
org.apache.kafka.common.serialization.Serde
org.apache.kafka.common.serialization.Serdes))
(set! *warn-on-reflection* true)
(defn to-bytes
"Converts a string to a byte array."
[data]
(.getBytes ^String data StandardCharsets/UTF_8))
(defn from-bytes
"Converts a byte array to a string."
[^bytes data]
(String. data StandardCharsets/UTF_8))
(defn serializer
"Returns an EDN serializer."
[]
(jsfn/new-serializer {:serialize (fn [_ _ data]
(when data
(to-bytes
(binding [*print-length* false
*print-level* false]
(prn-str data)))))}))
(defn deserializer
"Returns an EDN deserializer."
([]
(deserializer {}))
([opts]
(let [opts (into {} opts)]
(jsfn/new-deserializer {:deserialize (fn [_ _ data]
(->> (from-bytes data)
(clojure.edn/read-string opts)))}))))
(defn serde
"Returns an EDN serde."
[& [opts]]
(Serdes/serdeFrom (serializer) (deserializer opts)))