Serialize zipper?

repl> (-> root zip/down zip/right)
[{:answer-keys [5 6], :id 3} {:l [{:id 2, :answer-keys []}], :pnodes [{:answer-keys [2 3 4], :id 1}], :ppath nil, :r ({:answer-keys [7], :id 4})}]

I see this data when I print the zipper on repl. I think this could be all the data I need to serialize the zipper? Is it possible to de-serialize a zipper from the provided data?

I am looking for something like the zip / serialize and zip / deserialize functions presented below.

(def s (zip/serialize (-> root zip/down zip/right))) ;; s is a string
(def d (zip/deserialize s)) ;; d is a zipper location
;;And I can go on using the deserialized zipper d without any difficulty.

Does anyone know how to do this?

+3
source share
2 answers

lightning magic is a data structure that represents everything you need to create arbitrarily modified versions of tree structures. zippers print and read just fine , because they are the right values and do not require any state

"" pr-str "deserialize" read

:

user> (zip/vector-zip [[1 [2]][3][4]])
[[[1 [2]] [3] [4]] nil]
user> (def s (zip/vector-zip [[1 [2]][3][4]]))
#'user/s
user> s
[[[1 [2]] [3] [4]] nil]

:

user> (def serialized-s (pr-str (zip/next s)))
#'user/serialized-s
user> serialized-s
"[[1 [2]] {:l [], :pnodes [[[1 [2]] [3] [4]]], :ppath nil, :r ([3] [4])}]"

:

user> (def deserialized-s (read-string "[[1 [2]] {:l [], :pnodes [[[1 [2]] [3] [4]]], :ppath nil, :r ([3] [4])}]"))
#'user/deserialized-s

- :

user> (zip/root deserialized-s)
[[1 [2]] [3] [4]]
+4

:

, , clojure.zip , , , , branch?/children/make-node. -

(defn serialize [zipper]
  (binding [*print-meta* true]
    (pr-str zipper)))

(def deserialize read-string)
+3

All Articles