clojureedn

Clojure edn read auto namespaced keywords


Is there a way to instruct clojure.core.edn/read how to resolve auto namespaced keywords ?

  (edn/read-string "{:not-namespaced \"ko\" }") ;;=> {:not-namespaced "ko"}
  (edn/read-string "{:namespaced/ok \"ko\" }")  ;;=> #:namespaced{:ok "ko"}
  (edn/read-string "{::namespaced \"ko\" }")    ;;=> Unhandled java.lang.RuntimeException Invalid token: ::namespaced autonamespaced does not work

The last exception makes sense, since "A keyword cannot begin with ::".

I could use load-file with this simple example, however I also need the extensibility of edn (read custom tags).

Having a parameter to instruct how to resolve namespaces would make my config files (coerced with clojure.spec) much more readable.


Solution

  • So in my case it turn out the simplest solution is to use clojure.spec with un-namespaced keywords in my config file (I did not know it could do that).

    Using the core readers functions would have worked too.

    Quote from Alex Miller on slack:

    Yes, you'll need full namespaces in edn config files You could also write specs on unqualified keys with req-un and opt-un

    Either the normal core reader fns or clojure.edn reader fns should be able to read tagged literals though with # You just need to bind around the call to set up the readers