Skip to content

Instantly share code, notes, and snippets.

@joinr
Created July 10, 2024 06:18
Show Gist options
  • Save joinr/7b77830017d342b832b70a581462dcdd to your computer and use it in GitHub Desktop.
Save joinr/7b77830017d342b832b70a581462dcdd to your computer and use it in GitHub Desktop.
accurate serialization of false values with nippy on top of java.io.Serializable
(ns demo
(:require [taoensso.nippy :as nippy])
(:import [java.io ObjectOutputStream ObjectInputStream]))
;;https://gist.github.com/orendon/e38ac86dcd4c64cadad8fd5c749452b7
(defn serialize
"Serializes value, returns a byte array"
[v]
(let [buff (java.io.ByteArrayOutputStream. 1024)]
(with-open [dos (java.io.ObjectOutputStream. buff)]
(.writeObject dos v))
(.toByteArray buff)))
(defn deserialize
"Accepts a byte array, returns deserialized value"
[bytes]
(with-open [dis (java.io.ObjectInputStream.
(java.io.ByteArrayInputStream. bytes))]
(.readObject dis)))
;;our strategy will be to reify a
(defn pack [this]
{:contents (nippy/freeze this)})
(defn unpack [this]
(nippy/thaw (this :contents)))
(defn round-trip-pack [in]
(->> in pack serialize deserialize unpack))
(defn round-trip-jis [in]
(->> in serialize deserialize))
(comment
(def res (pack [true false]))
(def bins (serialize res))
(def returned (deserialize bins))
(def final (unpack returned))
(->> final (filter identity))
(->> (round-trip-jis [false false]) (filter identity))
;;(false false)
(->> (round-trip-pack [false false]) (filter identity))
;;()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment