This is my attempt:
(defn record? [a]
(and (map? a) (not (instance? clojure.lang.APersistentMap a))))
(defmethod print-dup clojure.lang.IPersistentMap [m, ^Writer w]
(if (record? m)
(do (.write w "#=(")
(print-dup (class m) w) (.write w ". ")
(doall (map #(do (print-dup % w) (.write w " ")) (map second (seq
m))))
(.write w ")")
)
(do
(#'clojure.core/print-meta m w)
(.write w "#=(")
(.write w (.getName (class m)))
(.write w "/create ")
(#'clojure.core/print-map m print-dup w)
(.write w ")"))))
;;used from
http://groups.google.com/group/clojure/browse_thread/thread/cb5246d07142a3dc?fwc=2&pli=1
(defn frm-save
"Save a clojure form to file."
[file form]
(with-open [w (java.io.FileWriter.
(if (instance? File file) file (File. file)))]
(binding [*out* w *print-dup* true] (prn form))))
(defn frm-load
"Load a clojure form from file."
[file]
(with-open [r (java.io.PushbackReader.
(java.io.FileReader. (if (instance? File file) file (File.
file))))]
(let [rec (read r)]
rec)))
However, it appears that the reader cant read classes
For example
(defrecord Foo [a])
(frm-save "/home/seth/Desktop/test" (Foo. 2))
(frm-load "/home/seth/Desktop/test")
Another problem is that it saves it as the ns it is in, for example my-
ns.Foo, and when you reload it it wont find my-ns.Foo unless you have
required it. But, editing the test file to just output
#=(#=foo. 2 3 )
and then loading that throws a class not found exception.
Any general way to print-dup these records?