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

Add custom serialization support #20

Closed ptaoussanis closed 11 years ago

ptaoussanis commented 11 years ago

Thanks to James Reeves for this suggestion:

(thaw bytes {:readers {Vector3D read-vector} :writers {Vector3D write-vector})

It might result in a prefix for the data like 80 \V \e \c \t \o \r \3 \D \

(defn read-vector [^DataInputStream s]
  (Vector3D. (.readDouble s) (.readDouble s) (.readDouble s)))

(defn write-vector [^DataOutputStream s ^Vector3D v]
   (.writeDouble s (.getX v))
   (.writeDouble s (.getY v))
   (.writeDouble s (.getZ v)))
ptaoussanis commented 11 years ago

Update: an alternative would be to reserve some part of the type-id space (-128 to -1 say) for custom types, and suggest that folks extend their custom types to the current Freezable protocol.

Then thaw-from-stream could be made extensible - perhaps with a {<custom-id> <fn-of-stream>} map passable to thaw/thaw-from-stream.

ptaoussanis commented 11 years ago

As of v2.1.0 currently on the dev branch:

(defrecord MyType [data])

(nippy/extend-freeze MyType 1 ; A unique type id ∈[1, 128]
  [x data-output-steam]
  (.writeUTF data-output-stream (:data x)))

(nippy/extend-thaw 1 ; Same type id
  [data-input-stream]
  (->MyType (.readUTF data-input-stream)))

(nippy/thaw (nippy/freeze (->MyType "Joe"))) => #taoensso.nippy.MyType{:data "Joe"}

This approach makes some tradeoffs.

PROS

CONS

Any other thoughts?