From 8bec62767d47f6dcabd2f28d9413358683bd24bf Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 24 Jul 2012 14:04:13 +0700 Subject: [PATCH] BREAKING (minor): Change `freeze-to-bytes`, `thaw-from-bytes` API when not using defaults. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Breaks only for users that were overriding `freeze-to-bytes` or `thaw-from-bytes` defaults to disable compression. Changed to map-based config to allow extra control (like :read-eval? as suggested by Luc Préfontaine). This change will also allow further additions in future without the need to break the API again. --- src/taoensso/nippy.clj | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 65d0701..272a026 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -161,13 +161,13 @@ (defn freeze-to-bytes "Serializes x to a byte array and returns the array." - (^bytes [x] (freeze-to-bytes x true)) - (^bytes [x compress?] - (let [ba (ByteArrayOutputStream.) - stream (DataOutputStream. ba)] - (freeze-to-stream! stream x) - (let [ba (.toByteArray ba)] - (if compress? (Snappy/compress ba) ba))))) + ^bytes [x & {:keys [compress?] + :or {compress? true}}] + (let [ba (ByteArrayOutputStream.) + stream (DataOutputStream. ba)] + (freeze-to-stream! stream x) + (let [ba (.toByteArray ba)] + (if compress? (Snappy/compress ba) ba)))) ;;;; Thawing @@ -226,8 +226,8 @@ ;; TODO Scheduled for Carmine version 1.0.0 ;; (defn thaw-from-stream! ;; "Deserializes an object from given input stream." -;; [data-input-stream] -;; (binding [*read-eval* false] ; For `read-string` injection safety - NB!!! +;; [data-input-stream read-eval?] +;; (binding [*read-eval* read-eval?] ;; (let [schema-header (thaw-from-stream!* data-input-stream)] ;; (thaw-from-stream!* data-input-stream)))) @@ -236,8 +236,8 @@ ;; Carmine < 0.8.3 and haven't yet migrated their databases. (defn thaw-from-stream! "Deserializes an object from given input stream." - [data-input-stream] - (binding [*read-eval* false] ; For `read-string` injection safety - NB!!! + [data-input-stream read-eval?] + (binding [*read-eval* read-eval?] (let [maybe-schema-header (thaw-from-stream!* data-input-stream)] (if (and (string? maybe-schema-header) (.startsWith ^String maybe-schema-header "\u0000~")) @@ -246,12 +246,13 @@ (defn thaw-from-bytes "Deserializes an object from given byte array." - ([ba] (thaw-from-bytes ba true)) - ([ba compressed?] - (->> (if compressed? (Snappy/uncompress ba) ba) - (ByteArrayInputStream.) - (DataInputStream.) - (thaw-from-stream!)))) + [ba & {:keys [read-eval? compressed?] + :or {read-eval? false ; For `read-string` injection safety - NB!!! + compressed? true}}] + (-> (if compressed? (Snappy/uncompress ba) ba) + (ByteArrayInputStream.) + (DataInputStream.) + (thaw-from-stream! read-eval?))) (def stress-data "Reference data used for tests & benchmarks."