-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomis_edn.clj
60 lines (50 loc) · 1.59 KB
/
nomis_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
53
54
55
56
57
58
59
60
(ns com.nomistech.serdes.nomis-edn
"Implements a hacked EDN SerDes (Serializer/Deserializer), with
\"nomis-\" as a prefix."
(:require [clojure.edn]
[jackdaw.serdes.fn :as jsfn])
(:import (java.nio.charset StandardCharsets))
(:gen-class
:implements [org.apache.kafka.common.serialization.Serde]
:name com.nomistech.NomisEdnSerde
:prefix "NomisEdnSerde-"))
;;;; Based on `jackdaw.serdes.edn2`.
(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))
(def ^:private nomis-edn-prefix "nomis-edn: ")
(defn nomis-edn-serializer
"Return a Nomis EDN serializer."
[]
(jsfn/new-serializer
{:serialize (fn [_ _ data]
(when data
(let [s (str nomis-edn-prefix
(pr-str data))]
(to-bytes s))))}))
(defn nomis-edn-deserializer
"Return a Nomis EDN deserializer."
([]
(nomis-edn-deserializer {}))
([opts]
(let [opts (into {} opts)]
(jsfn/new-deserializer
{:deserialize (fn [_ _ data]
(let [s (from-bytes data)]
(->> (subs s
(count nomis-edn-prefix))
(clojure.edn/read-string opts))))}))))
(def NomisEdnSerde-configure
(constantly nil))
(defn NomisEdnSerde-serializer
[& _]
(nomis-edn-serializer))
(defn NomisEdnSerde-deserializer
[& _]
(nomis-edn-deserializer))