What is happening when I get this error in Clojure?
java.lang.RuntimeException: No reader function for tag db/id
This error message is related to a feature introduced in Clojure 1.7, tagged literals. Tagged literals are a simple means for extending what data types can be represented as literals in Clojure code or EDN data.
Clojure ships with readers for two tagged literals, #inst
and
#uuid
allowing literal representations of java.util.Date
and
java.util.UUID
. Support for more tags can be added by including
data_readers.clj
files on the classpath as described in the
documentation.
Some common causes of this error:
#db/id
literals:readers
*data-readers*
.One common cause of this error is trying to use the EDN reader
without passing the :readers
option. From the doc string for
clojure.edn/read
:
: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.
As its name implies, default-data-readers
is only the map of data
readers that ship with Clojure itself. That is, it does not include
any readers defined in the data_readers.clj
files of any libraries
you are using (like, say, Datomic). The solution in this case is to
pass the current set of loaded data readers, found in the var
*data-readers*
.
(clojure.edn/read {:readers *data-readers*} <your-read-source>)
Or, even better, explicitly specify the mapping of the data readers you want to use. For instance, if you want to be able to use only the datomic data readers:
(require 'datomic.db
'datomic.function
'datomic.codec)
(edn/read {:readers {'db/id datomic.db/id-literal
'db/fn datomic.function/construct
'base64 datomic.codec/base-64-literal}}
<your-read-source-here>)
And of course, since these are just clojure maps we're dealing with,
we could also include the default-data-readers
by merging the maps:
(require 'datomic.db
'datomic.function
'datomic.codec)
(edn/read {:readers (merge
default-data-readers
{'db/id datomic.db/id-literal
'db/fn datomic.function/construct
'base64 datomic.codec/base-64-literal})}
<your-read-source-here>)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With