You're viewing version 1.2.0 of *print-dup*. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

*print-dup*

clojure.core

When set to logical true, objects will be printed in a way that preserves
their type when read in later.

Defaults to false.

1 Example top

  • ;; `*print-dup*` is very handy when we want to write clojure code/data
    ;; to a file to read it in later.
    
    (defn serialize
      "Print a data structure to a file so that we may read it in later."
      [data-structure #^String filename]
      (with-out-writer
        (java.io.File. filename)
        (binding [*print-dup* true] (prn data-structure))))
    
    
    ;; This allows us to then read in the structure at a later time, like so:
    (defn deserialize [filename]
      (with-open [r (PushbackReader. (FileReader. filename))]
        (read r)))
    
    
    (def *configuration-options* (deserialize "config.clj"))
    
    user=> *configuration-options*
    {:name "Fred", :age "23"}
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Comments top

2 comment(s) for *print-dup*.

It is sometimes preferable (depending on the size of the structure you're serializing) to wrap the (with-out-writer ...) inside a (dorun (with-out-writer ...) nil) in order to suppress the output at your REPL.

Note, I'm making changes to deserialize, there are a few typos.

Log in to add a comment.