diff --git a/README.md b/README.md index 1184b4b..fa7aac5 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,9 @@ nippy/stress-data :ratio 22/7 :uuid (java.util.UUID/randomUUID) - :date (java.util.Date.)} + :date (java.util.Date.) + + :stress-record (->StressRecord "data")} ``` Serialize it: diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index ecdb62f..ce3d591 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -154,7 +154,7 @@ (freezer-coll LazySeq id-seq) (freezer IRecord id-record - (write-utf8 s (.getName (class x))) + (write-utf8 s (.getName (class x))) ; Reflect (freeze-to-stream s (into {} x))) (freezer Byte id-byte (.writeByte s x)) @@ -419,6 +419,7 @@ ;;;; Stress data +(defrecord StressRecord [data]) (def stress-data "Reference data used for tests & benchmarks." (let [] {:bytes (byte-array [(byte 1) (byte 2) (byte 3)]) @@ -461,7 +462,9 @@ :ratio 22/7 :uuid (java.util.UUID/randomUUID) - :date (java.util.Date.)})) + :date (java.util.Date.) + + :stress-record (->StressRecord "data")})) ;;;; Deprecated API diff --git a/test/taoensso/nippy/tests/main.clj b/test/taoensso/nippy/tests/main.clj index 4a70bcf..4d98a57 100644 --- a/test/taoensso/nippy/tests/main.clj +++ b/test/taoensso/nippy/tests/main.clj @@ -39,18 +39,16 @@ (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 (reflecting) -(defrecord MyRec [data]) -(expect (let [rec (->MyRec "val")] (= rec (thaw (freeze rec))))) -;;; Custom types +;;; Extend to custom Type (defrecord MyType [data]) (nippy/extend-freeze MyType 1 [x s] (.writeUTF s (:data x))) (expect Exception (thaw (freeze (->MyType "val")))) (expect (do (nippy/extend-thaw 1 [s] (->MyType (.readUTF s))) (let [type (->MyType "val")] (= type (thaw (freeze type)))))) -;;; Records (extend) +;;; Extend to custom Record +(defrecord MyRec [data]) (expect (do (nippy/extend-freeze MyRec 2 [x s] (.writeUTF s (str "fast-" (:data x)))) (nippy/extend-thaw 2 [s] (->MyRec (.readUTF s))) (= (->MyRec "fast-val") (thaw (freeze (->MyRec "val"))))))