-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Questions about custom types #180
Comments
Hi Stuart! 1. If you specify a custom freeze and later read without a custom thaw, what happens? Is there a default handler mechanism?If you specify a custom freeze implementation with (defrecord MyRec [data])
(extend-freeze MyRec 1 [x out] (.writeUTF out (:data x)))
;; (extend-thaw 1 [in] (MyRec. (.readUTF in))) ; Disabled
(thaw (freeze (MyRec. "Foo"))) Nippy freezes values using this protocol fn. Each implementation writes a type id (byte) to the output stream, and a possible payload (byte array). Freezing a small string will write byte 96 (type id for small strings), and the string's payload (string length, and UTF8 bytes). Nippy then thaws based on the written type id. So if a value was written with a custom type id, the same type id will need a registered implementation on thaw. 2. Given that Nippy delegates (sometimes?) to edn and Java serialization which have their own customization mechanisms, how are customizations prioritized, e.g. what would happen if you wrote an instance that had customized Java serialization, Clojure print, and Nippy freeze?
So whatever method is ultimately used during freezing - will determine the type id written, and so the method that will (and must) be used during thawing. Hope that helps? |
Hi Peter! Wow, thanks for the thoughtful, comprehensive, and fast response! I want to document a pattern for using Nippy such that consumers can never be broken for lack of a custom handler. It seems that this is possible as follows:
Does that suffice to lock all the trapdoors? I presume limiting Nippy in this way has performance (and maybe other?) tradeoffs, but that those tradeoffs are not so severe that they erase the advantages of Nippy over e.g. edn. |
No worries Stuart, you're very welcome! Your proposed pattern sounds generally reasonable to me 👍 Re: your point 2 - both options should work, but the simpler is probably to ensure that
Depending on the context of where/how your pattern may be used, there might be a couple things to keep in mind:
If you are concerned about (2) and are able to share a little more about your context, I might be able to propose some ideas. |
Hello. I am trying to understand Nippy's custom types, and have two questions:
Thanks much!
The text was updated successfully, but these errors were encountered: