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

How can I freeze an unknown data structure, ignoring all errors ? #142

Closed behrica closed 2 years ago

behrica commented 2 years ago

Related to https://github.com/ptaoussanis/nippy/issues/141, but more focussed question.

Is there a way to freeze an arbitrary data structure, while ignoring all errors due to non freezable types ? "ignoring" could mean putting nil or leaving out or replace by "dummy".

ptaoussanis commented 2 years ago

Hi Carsten,

Yes, you have two options:

  1. Set *freeze-fallback* to the keyword value :write-unfreezable.
  2. Set *freeze-fallback* to the function value taoensso.nippy/write-unfreezable.

There's an important difference between these:

  1. Will try to freeze an object using Java Serializable (which can be error-prone). If that fails, it'll try using Clojure's reader (which can be slow, and sometimes also error-prone). If that fails, it'll use taoensso.nippy/write-unfreezable to essentially serialize an error message (always works, but of limited value if you want to be able to reconstruct the frozen object).

  2. Will just immediately resort to using taoensso.nippy/write-unfreezable - i.e. will always work and always be fast, but you'll basically just be serializing an error message.

Put it differently:

behrica commented 2 years ago

thanks, very helpfull