ClojureDocs

导航

命名空间

*data-readers*

clojure.core

自 1.4 起提供 (源代码)
    Map from reader tag symbols to data reader Vars.
     When Clojure starts, it searches for files named 'data_readers.clj'
    and 'data_readers.cljc' at the root of the classpath. Each such file
    must contain a literal map of symbols, like this:
         {foo/bar my.project.foo/bar
         foo/baz my.project/baz}
     The first symbol in each pair is a tag that will be recognized by
    the Clojure reader. The second symbol in the pair is the
    fully-qualified name of a Var which will be invoked by the reader to
    parse the form following the tag. For example, given the
    data_readers.clj file above, the Clojure reader would parse this
    form:
         #foo/bar [1 2 3]
     by invoking the Var #'my.project.foo/bar on the vector [1 2 3]. The
    data reader function is invoked on the form AFTER it has been read
    as a normal Clojure data structure by the reader.
     Reader tags without namespace qualifiers are reserved for
    Clojure. Default reader tags are defined in
    clojure.core/default-data-readers but may be overridden in
    data_readers.clj, data_readers.cljc, or by rebinding this Var.
    1 Example
    (import '[java.net URL])
    
    ;; Teaching Clojure how to read URL objects from a string
    
    (binding [*data-readers* {'url #(URL. %)}] 
      (read-string "#url \"file:/etc/hosts\""))
    
    ;; #object[java.net.URL 0x64a8c844 "file:/etc/hosts"]
    See Also
    2 Notes
      By , created 9.8 years ago

      clojure.edn/read and clojure.edn/read-string don't look into *data-readers* in order to find the reader functions for a reader tag. That's why they won't get the mappings from data_readers.clj either. See the note on clojure.edn/read for more information and a warning.

      By , created 8.3 years ago

      Note that if you're using clojure.reader/read instead of clojure.core/read, you have to bind clojure.reader/*data-readers* rather than clojure.core/*data-readers*.