clojure.edn/read¶
Reads the next object from stream, which must be an instance of java.io.PushbackReader or some derivee. stream defaults to the current value of in.
Reads data in the edn format (subset of Clojure data): http://edn-format.org
opts is a map that can include the following keys: :eof - value to return on end-of-file. When not supplied, eof throws an exception. :readers - a map of tag symbols to data-reader functions to be considered before default-data-readers. When not supplied, only the default-data-readers will be used. :default - A function of two args, that will, if present and no reader is found for a tag, be called with the tag and the value.
Examples¶
Note by rmoehn¶
For everyone struggling to provide custom reader functions to
clojure.edn/read: you cannot do that through data_readers.clj.
data_readers.clj can be used to provide custom reader functions for
clojure.core/read and clojure.core/read-string, but not clojure.edn/read
and clojure.edn/read-string.
You look carefully and you'll find that the official documentation does say so:
if you don't supply a map of :readers, ‘only the default-data-readers will
be used’. But the information from data_readers.clj ends up in
*data-readers* not default-data-readers. However, you'd have to look
very carefully to find this out.
(If you're not interested in silly stories, you can skip the following two paragraphs and continue with the last.)
Now you might think: ‘then I'll just say (edn/read-string {:readers
*data-readers*} <a stream>)’. On the first try this will probably fail, because
you forgot to require the namespaces where the reader functions live. Okay,
you've fixed that………
Browsing from your smartphone now? I know, your home directory just got wiped.
And the external drive with the backups that's always plugged in to your machine
as well. Why's that? Well, the procedures from clojure.edn are supposed to be
safe, to not execute any code, so that you can use them with data from untrusted
sources. If you do want to have code executed (only from trusted sources, of
course), you use the corresponding procedures from clojure.core. Now, there
are people who know about data_readers.clj only being used by
clojure.core/read and clojure.core/read-string, which aren't safe. So they
don't make their reader functions safe either. guten-tag.core/read-tagged-val
is such an example. It calls eval on the data it is passed. That means, if you
use it with clojure.edn/read or clojure.edn/read-string, those will become
as unsafe as their clojure.core counterparts.
Sorry for the long story. The moral is: have a very careful look at the reader
functions you provide to the procedures from clojure.edn. If they execute code
that's passed to them, all safety is lost.
See also¶
Content from the matching ClojureDocs page, with authors credited on each contribution.