diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 9a6a541..e237b92 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -8,9 +8,10 @@ (encryption :as encryption :refer (aes128-encryptor))]) (:import [java.io DataInputStream DataOutputStream ByteArrayOutputStream ByteArrayInputStream] + [java.lang.reflect Method] [clojure.lang Keyword BigInt Ratio PersistentQueue PersistentTreeMap PersistentTreeSet IPersistentList IPersistentVector IPersistentMap - IPersistentSet IPersistentCollection])) + IPersistentSet IPersistentCollection IRecord])) ;;;; Nippy 2.x+ header spec (4 bytes) (def ^:private ^:const head-version 1) @@ -61,6 +62,8 @@ (def ^:const id-ratio (int 70)) +(def ^:const id-record (int 80)) + ;;; DEPRECATED (old types will be supported only for thawing) (def ^:const id-old-reader (int 1)) ; as of 0.9.2, for +64k support (def ^:const id-old-string (int 11)) ; as of 0.9.2, for +64k support @@ -137,6 +140,10 @@ (str ns "/" (name x)) (name x)))) +(freezer IRecord id-record + (write-utf8 s (.getName (class x))) + (freeze-to-stream s (into {} x))) + (coll-freezer PersistentQueue id-queue) (coll-freezer PersistentTreeSet id-sorted-set) (kv-freezer PersistentTreeMap id-sorted-map) @@ -261,6 +268,12 @@ id-ratio (/ (bigint (read-biginteger s)) (bigint (read-biginteger s))) + id-record + (let [class ^Class (Class/forName (read-utf8 s)) + meth-sig (into-array Class [IPersistentMap]) + method ^Method (.getMethod class "create" meth-sig)] + (.invoke method class (into-array Object [(thaw-from-stream s)]))) + ;;; DEPRECATED id-old-reader (read-string (.readUTF s)) id-old-string (.readUTF s) diff --git a/test/taoensso/nippy/tests/main.clj b/test/taoensso/nippy/tests/main.clj index 0cc34c8..8ec9c7a 100644 --- a/test/taoensso/nippy/tests/main.clj +++ b/test/taoensso/nippy/tests/main.clj @@ -30,6 +30,11 @@ (thaw (org.iq80.snappy.Snappy/uncompress iq80-ba 0 (alength iq80-ba))) (thaw (org.iq80.snappy.Snappy/uncompress xerial-ba 0 (alength xerial-ba)))))) +;;; Records +(defrecord RecordType [data]) +(expect RecordType (thaw (freeze (RecordType. "Joe")))) +(expect (RecordType. "Joe") (thaw (freeze (RecordType. "Joe")))) + ;;; Custom types (defrecord MyType [data]) (nippy/extend-freeze MyType 1 [x s] (.writeUTF s (:data x)))