-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpack.clj
35 lines (29 loc) · 869 Bytes
/
msgpack.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
(ns fastbeans.msgpack
(:require
[clj-msgpack.core :as mp]
[clojure.java.io :as io])
(:import
[org.msgpack MessagePack]
[org.msgpack.packer Packer]))
(def packer (MessagePack.))
(defn pack
"Pack the objects into a byte array and return it."
[& objs]
(let [p (.createBufferPacker ^MessagePack packer)]
(apply mp/pack-into p objs)
(.toByteArray p)))
(defn unpack
[from]
(let [is (io/input-stream from)
u (.createUnpacker ^MessagePack packer is)]
(map mp/unwrap u)))
(extend-protocol mp/Packable
clojure.lang.Keyword
(pack-me [kw ^Packer packer]
(.write packer ^String (name kw)))
java.io.ByteArrayOutputStream
(pack-me [baos ^Packer packer]
(.write packer ^String (.toString baos)))
java.util.Date
(pack-me [date ^Packer packer]
(.write packer ^Number (.intValue (/ (.getTime date) 1000)))))