/
serialization.clj
162 lines (135 loc) · 7.04 KB
/
serialization.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
; Copyright (c) Rich Hickey. 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.
;; Author: Chas Emerick
;; cemerick@snowtide.com
(ns clojure.test-clojure.serialization
(:use clojure.test)
(:import (System.IO MemoryStream) ;;;(java.io ObjectOutputStream ObjectInputStream
(System.Runtime.Serialization.Formatters.Binary BinaryFormatter))) ;;; ByteArrayOutputStream ByteArrayInputStream)))
(defn- serialize
"Serializes a single object, returning a byte array."
[v]
(with-open [memstr (MemoryStream.)] ;;; [bout (ByteArrayOutputStream.)
(let [bf (BinaryFormatter.)] ;;; oos (ObjectOutputStream. bout)]
(. bf (Serialize memstr v)) ;;; (.writeObject oos v)
(.Flush memstr) ;;; (.flush oos)
(.ToArray memstr)))) ;;; (.toByteArray bout)))
(defn- deserialize
"Deserializes and returns a single object from the given byte array."
[bytes]
(with-open [memstr (-> bytes (MemoryStream.))] ;;;[ois (-> bytes ByteArrayInputStream. ObjectInputStream.)]
(let [bf (BinaryFormatter.)] ;;; ADDED
(.Deserialize bf memstr)))) ;;; (.readObject ois)))
(defrecord SerializationRecord [a b c])
(defstruct SerializationStruct :a :b :c)
(defn- build-via-transient
[coll]
(persistent!
(reduce conj! (transient coll) (map vec (partition 2 (range 1000))))))
(defn- roundtrip
[v]
(let [rt (-> v serialize deserialize)
rt-seq (-> v seq serialize deserialize)]
(and (= v rt)
(= (seq v) (seq rt))
(= (seq v) rt-seq))))
(deftest sequable-serialization
(are [val] (roundtrip val)
; lists and related
;;; (list) <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
(apply list (range 10))
(cons 0 nil)
(clojure.lang.Cons. 0 nil)
; vectors
;;; [] <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
(into [] (range 10))
(into [] (range 25))
(into [] (range 100))
(into [] (range 500))
(into [] (range 1000))
; maps
;;; {} <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
{:a 5 :b 0}
(apply array-map (range 100))
(apply hash-map (range 100))
; sets
;;; #{} <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
#{'a 'b 'c}
(set (range 10))
(set (range 25))
(set (range 100))
(set (range 500))
(set (range 1000))
;;; (sorted-set) <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
(sorted-set 'a 'b 'c)
(apply sorted-set (reverse (range 10)))
(apply sorted-set (reverse (range 25)))
(apply sorted-set (reverse (range 100)))
(apply sorted-set (reverse (range 500)))
(apply sorted-set (reverse (range 1000)))
; queues
;;; clojure.lang.PersistentQueue/EMPTY <--- We cannot handle seq of this, which is nil. Cannot serialize nil.
(into clojure.lang.PersistentQueue/EMPTY (range 50))
; lazy seqs
;;;(lazy-seq nil) <-- TODO: Some LazySeqs won't serialize because of dynamic methods. Need a flag to control dynamic versus emitted methods.
;;;(lazy-seq (range 50)) <-- TODO: Some LazySeqs won't serialize because of dynamic methods. Need a flag to control dynamic versus emitted methods.
; transient / persistent! round-trip
(build-via-transient [])
(build-via-transient {})
(build-via-transient #{})
; array-seqs
(seq (make-array Object 10))
(seq (make-array Boolean 10)) ;;; Boolean/TYPE
(seq (make-array Byte 10)) ;;; Byte/TYPE
(seq (make-array Char 10)) ;;; Character/TYPE
(seq (make-array Double 10)) ;;; Double/TYPE
(seq (make-array Single 10)) ;;; Float/TYPE
(seq (make-array Int32 10)) ;;; Integer/TYPE
(seq (make-array Int64 10)) ;;; Long/TYPE
; "records"
;;;(SerializationRecord. 0 :foo (range 20)) <--- TODO: Figure out why the stub is being serialized. Clearly it should not be.
;;;(struct SerializationStruct 0 :foo (range 20)) <--- TODO: Figure out why the stub is being serialized. Clearly it should not be.
; misc seqs
(seq "s11n")
;;;(range 50) <--- TODO: Figure out why some LazySeqs bomb because unable to find assembly core.clj
(rseq (apply sorted-set (reverse (range 100))))))
(deftest misc-serialization
(are [v] (= v (-> v serialize deserialize))
25/3
:keyword
::namespaced-keyword
'symbol))
(deftest interned-serializations
(are [v] (identical? v (-> v serialize deserialize))
clojure.lang.RT/DEFAULT_COMPARER ;;; clojure.lang.RT/DEFAULT_COMPARATOR
; namespaces just get deserialized back into the same-named ns in the present runtime
; (they're referred to by defrecord instances)
*ns*))
;;;(deftest function-serialization
;;; (let [capture 5]
;;; (are [f] (= capture ((-> f serialize deserialize)))
;;; (constantly 5) <--- TODO: unable to find assembly core.clj
;;; (fn [] 5) <--- TODO: problem serializing delegates over dynamic
;;; #(do 5 <--- TODO: problem serializing delegates over dynamic
;;; (constantly capture) <--- TODO: unable to find assembly core.clj
;;; (fn [] capture) <--- TODO: problem serializing delegates over dynamic
;;; #(do capture)))) <--- TODO: problem serializing delegates over dynamic
(deftest check-unserializable-objects
(are [t] (thrown? System.Runtime.Serialization.SerializationException (serialize t)) ;;; java.io.NotSerializableException
;; transients
(transient [])
(transient {})
(transient #{})
;; reference types
(atom nil)
(ref nil)
(agent nil)
#'+
;; stateful seqs
(enumeration-seq (.GetEnumerator (range 50))) ;;; (java.util.Collections/enumeration (range 50)))
(iterator-seq (.GetEnumerator (range 50))))) ;;; (.iterator (range 50)))))