/
fressian.clj
287 lines (249 loc) · 9.82 KB
/
fressian.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;; Copyright (c) Rich Hickey, Cognitect, Inc. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns ^{:author "Stuart Halloway"
:doc "Read/write fressian data. See http://www.edn-format.org/"}
clojure.data.fressian
(:refer-clojure :exclude (read))
(:require
[clojure.java.io :as io]
[clojure.string :as str])
(:import
[clojure.lang IRecord Ratio]
[java.io InputStream OutputStream]
[java.nio ByteBuffer]
[org.fressian FressianWriter StreamingWriter FressianReader TaggedObject Writer Reader]
[org.fressian.handlers WriteHandler ReadHandler ILookup WriteHandlerLookup]
[org.fressian.impl ByteBufferInputStream BytesOutputStream InheritanceLookup]))
(set! *warn-on-reflection* true)
(defn- write-named [tag ^Writer w s]
(.writeTag w tag 2)
(.writeObject w (namespace s) true)
(.writeObject w (name s) true))
(defn- ^ByteBuffer bytestream->buf
"Return a readable buf over the current internal state of a
BytesOutputStream."
[^BytesOutputStream stream]
(ByteBuffer/wrap (.internalBuffer stream) 0 (.length stream)))
(defprotocol FressianReadable
(to-input-stream [obj] "Implementation detail."))
(extend-protocol FressianReadable
Object
(to-input-stream
[obj]
(io/input-stream obj))
ByteBuffer
(to-input-stream
[bb]
(io/input-stream (ByteBufferInputStream. bb))))
(defn associative-lookup
"Build an ILookup from an associative collection."
[o]
(reify ILookup
(valAt [_ k] (get o k))))
(defn inheritance-lookup
"Returns an inheritance aware lookup based on lookup that will match
subclasses as well as exact matches. Will walk inheritance hierarchy
once per new type encountered to find the best match, then cache
results."
[lookup]
(InheritanceLookup. lookup))
(defn- class-sym
"Returns the class name of inst as a symbol."
[^Object inst]
(-> inst (.getClass) (.getName) symbol))
(def clojure-write-handlers
"Standard set of write handlers for Clojure data."
{Character
{"char"
(reify WriteHandler
(write [_ w ch]
(.writeTag w "char" 1)
(.writeInt w (int ch))))}
Ratio
{"ratio"
(reify WriteHandler
(write [_ w n]
(.writeTag w "ratio" 2)
(.writeObject w (.numerator ^Ratio n))
(.writeObject w (.denominator ^Ratio n))))}
IRecord
{"clojure/record"
(reify WriteHandler
(write [_ w rec]
(.writeTag w "record" 2)
(.writeObject w (class-sym rec) true)
(.writeTag w "map" 1)
(.beginClosedList ^StreamingWriter w)
(reduce-kv
(fn [^Writer w k v]
(.writeObject w k true)
(.writeObject w v))
w
rec)
(.endList ^StreamingWriter w)))}
clojure.lang.Keyword
{"key"
(reify WriteHandler
(write [_ w s]
(write-named "key" w s)))}
clojure.lang.BigInt
{"bigint"
(reify WriteHandler
(write [this w d]
(let [^BigInteger bi (if (instance? clojure.lang.BigInt d)
(.toBigInteger ^clojure.lang.BigInt d)
d)]
(.writeTag w "bigint" 1)
(.writeBytes w (.toByteArray bi)))))}
clojure.lang.Symbol
{"sym"
(reify WriteHandler
(write [_ w s]
(write-named "sym" w s)))}})
(defn field-caching-writer
"Returns a record writer that caches values for keys
matching cache-pred, which is typically specified
as a set, e.g. (field-caching-writer #{:color})"
[cache-pred]
(reify WriteHandler
(write [_ w rec]
(.writeTag w "record" 2)
(.writeObject w (class-sym rec) true)
(.writeTag w "map" 1)
(.beginClosedList ^StreamingWriter w)
(reduce-kv
(fn [^Writer w k v]
(.writeObject w k true)
(.writeObject w v (boolean (cache-pred k))))
w
rec)
(.endList ^StreamingWriter w))))
(defn- record-map-constructor-name
"Return the map constructor for a record"
[rname]
(let [comps (str/split (str rname) #"\.")]
(symbol (->> (butlast comps) (map #(str/replace % "_" "-"))
(str/join "."))
(str "map->" (last comps)))))
(def clojure-read-handlers
"Standard set of read handlers for Clojure data."
{"bigint"
(reify ReadHandler
(read [_ rdr tag component-count]
(let [^bytes bibytes (.readObject rdr)]
(bigint (BigInteger. bibytes)))))
"byte"
(reify ReadHandler (read [_ rdr tag component-count]
(byte (.readObject rdr))))
"record"
(reify ReadHandler (read [_ rdr tag component-count]
(let [rname (.readObject rdr)
rmap (.readObject rdr)]
(if-let [rcons (resolve (record-map-constructor-name rname))]
(rcons rmap)
(TaggedObject. "record" (into-array Object [rname rmap]))))))
"char"
(reify ReadHandler (read [_ rdr tag component-count]
(char (.readObject rdr))))
"ratio"
(reify ReadHandler (read [_ rdr tag component-count]
(Ratio. (biginteger (.readObject rdr))
(biginteger (.readObject rdr)))))
"key"
(reify ReadHandler (read [_ rdr tag component-count]
(keyword (.readObject rdr) (.readObject rdr))))
"sym"
(reify ReadHandler (read [_ rdr tag component-count]
(symbol (.readObject rdr) (.readObject rdr))))
"map"
(reify ReadHandler (read [_ rdr tag component-count]
(let [kvs ^java.util.List (.readObject rdr)]
(if (< (.size kvs) 16)
(clojure.lang.PersistentArrayMap. (.toArray kvs))
(clojure.lang.PersistentHashMap/create (seq kvs))))))})
(defn ^Writer create-writer
"Create a fressian writer targeting out. Handlers must be
a nested map of type => tag => WriteHandler wrapped with
associative-lookup and inheritance-lookup. See
clojure-write-handlers for an example."
[^OutputStream out & {:keys [handlers]}]
(FressianWriter. out (or handlers (-> clojure-write-handlers associative-lookup inheritance-lookup))))
(defn ^Reader create-reader
"Create a fressian reader targeting in, which must be compatible
with clojure.java.io/input-stream. Handlers must be a map of
tag => ReadHandler wrapped in associative-lookup. See
clojure-read-handlers for an example."
[^InputStream in & {:keys [handlers checksum?]}]
(FressianReader. in
(or handlers (associative-lookup clojure-read-handlers))
(boolean checksum?)))
(defn read-object
"Read a single object from a fressian reader."
[^Reader rdr]
(.readObject rdr))
(defn write-object
"Write a single object to a fressian reader. Returns the reader."
[^Writer writer obj]
(.writeObject writer obj))
(defn read
"Convenience method for reading a single fressian object.
Takes same options as create-reader. Readable can be
any type supported by clojure.java.io/input-stream, or
a ByteBuffer."
[readable & options]
(.readObject ^Reader (apply create-reader (to-input-stream readable) options)))
(defn ^ByteBuffer write
"Convenience method for writing a single object. Returns a
byte buffer. Options are the same as for create-reader,
with one additional option. If footer? is specified, will
write a fressian footer after writing the object."
([obj & options]
(let [{:keys [footer?]} (when options (apply hash-map options))
bos (BytesOutputStream.)
writer ^Writer (apply create-writer bos options)]
(.writeObject writer obj)
(when footer?
(.writeFooter writer))
(bytestream->buf bos))))
(defn tagged-object?
"Returns true if o is a tagged object, which will occur when
the reader does not recognized a specific type. Use tag
and tagged-value to access the contents of a tagged-object."
[o]
(instance? TaggedObject o))
(defn tag
"Returns the tag if object is a tagged-object, else nil."
[^TaggedObject obj]
(when (tagged-object? obj)
(.getTag obj)))
(defn tagged-value
"Returns the value (an Object arrray) wrapped by obj, or nil
if obj is not a tagged object."
[^TaggedObject obj]
(when (tagged-object? obj)
(.getValue obj)))
(defn ^Writer write-footer
[^Writer writer]
(.writeFooter writer))
(defn ^Writer begin-closed-list
"Begin writing a fressianed list. To end the list, call end-list.
Used to write sequential data whose size is not known in advance."
[^StreamingWriter writer]
(.beginClosedList writer))
(defn ^Writer end-list
"Ends a list begun with begin-closed-list."
[^StreamingWriter writer]
(.endList writer))
(defn ^Writer begin-open-list
"Advanced. Writes fressian code to begin an open list. An
open list can be terminated either by a call to end-list,
or by simply closing the stream. Used to write sequential
data whose size is not known in advance, in contexts where
stream failure can safely be interpreted as end of list."
[^StreamingWriter writer]
(.beginOpenList writer))