taoensso / nippy

The fastest serialization library for Clojure
https://www.taoensso.com/nippy
Eclipse Public License 1.0
1.04k stars 60 forks source link

Suggestion: convenience functions for spitting and slurping nippy #91

Closed Engelberg closed 7 years ago

Engelberg commented 7 years ago

99% of my use cases for nippy, I end up using the following functions:

(defn spit-nippy [filename data]
  (with-open [out (clojure.java.io/output-stream (clojure.java.io/file filename))]
    (.write out (nippy/freeze data))))

(defn slurp-nippy [filename]
  (let [^java.io.File file (clojure.java.io/file filename),
        result             (byte-array (.length file))]
    (with-open [in (DataInputStream. (clojure.java.io/input-stream file))]
      (.readFully in result))
    (nippy/thaw result)))

If others frequently use nippy the way I do, as a really convenient way to serialize and deserialize data to/from a file, I'd recommend including these functions directly in the nippy namespace, so people don't have to keep reinventing the wheel.

Probably to make it more general purpose, it would be good if it optionally took extra compression/encryption options like freeze and thaw, although I haven't needed to do that myself.

ptaoussanis commented 7 years ago

Hi Mark, thanks for pinging about this- great suggestion. Will add something next time I'm on Nippy.

Cheers! :-)

ptaoussanis commented 7 years ago

About to upload a new release with these utils, thanks again Mark - cheers! :-)