What is the most idiomatic way to write a data structure to disk in Clojure, so I can read it back with edn/read? I tried the following, as recommended in the Clojure cookbook:
(with-open [w (clojure.java.io/writer "data.clj")]
(binding [*out* w]
(pr large-data-structure)))
However, this will only write the first 100 items, followed by "...". I tried (prn (doall large-data-structure))
as well, yielding the same result.
I've managed to do it by writing line by line with (doseq [i large-data-structure] (pr i))
, but then I have to manually add the parens at the beginning and end of the sequence to get the desired result.
You can control the number of items in a collection that are printed via *print-length*
Consider using spit instead of manually opening the writer and pr-str instead of manually binding to*out*
.Edit from comment:
Note:
*print-length*
has a root binding ofnil
so you should not need to bind it in the example above. I would check the current binding at the time of your originalpr
call.