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

strange freezable? result #120

Closed aiba closed 3 years ago

aiba commented 4 years ago

This call returns nil:

(nippy/freezable? {:a nil} {:allow-clojure-reader? true})

That seems surprising, as nippy seems totally able to freeze {:a nil}.

Using [com.taoensso/nippy "2.14.0"], clojure 1.10.1, AdoptOpenJDK "12.0.1+12" on macos

ptaoussanis commented 4 years ago

Hi Aaron,

So the problem is just that nippy/freezable? isn't currently exhaustive, and actually doesn't include a check for nil?. Was about to add it (it's trivial to add), but in hindsight realise the whole current approach to freezable? is a bit dubious.

Would like the opportunity to consider alternative approaches.

Could you describe your use-case? Might be able to suggest a workaround in the meantime.

aiba commented 4 years ago

Could you describe your use-case?

I need to freeze clojure.lang.ExceptionInfo, but sometimes the ex-data is unfreezable. So I am sanitizing it by doing a postwalk on the ex-data, checking for nippy/freezable? and converting anything not freezable into a string.

I tried binding nippy/*freeze-fallback*, but that seems to call the fallback function on any ExceptionInfo, even if it is freezable:

(defn safe-freeze [x]
  (binding [nippy/*freeze-fallback* (fn [out x]
                                      (nippy/freeze-to-out!
                                       out (str "unfreezable:" x)))]
    (nippy/freeze x)))

(-> (ex-info "foo" {:a 1}) (safe-freeze) (nippy/thaw))    ;; fallback called
(-> (ex-info "foo" {:a 1}) (nippy/freeze) (nippy/thaw))  ;; seems to work fine
ptaoussanis commented 3 years ago

Hi Aaron, fixed in v2.15.0 - sorry for the delay! Cheers :-)