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

Prefer APersistentMap over IPersistentMap #24

Closed weavejester closed 11 years ago

weavejester commented 11 years ago

The IPersistentMap interface is implemented by records as well as normal map structures, so when serializing records Nippy incorrectly encodes them as maps.

user=> (require '[taoensso.nippy :as nippy])
nil
user=> (defrecord Foo [x])
user.Foo
user=> (nippy/thaw (nippy/freeze (Foo. 1)))
{:x 1}

The APersistentMap abstract class, on the other hand, is implemented by all map types except for records:

user=> (instance? clojure.lang.APersistentMap (hash-map))
true
user=> (instance? clojure.lang.APersistentMap (array-map))
true
user=> (instance? clojure.lang.APersistentMap (sorted-map))
true
user=> (instance? clojure.lang.APersistentMap (Foo. 1))
false

For this reason it's better for Nippy to dispatch its freezer off of APersistentMap instead of IPersistentMap.