What about using protocols for this job?
(defprotocol ConvertibleToClojure
(->clj [o]))
(extend-protocol ConvertibleToClojure
java.util.Map
(->clj [o] (let [entries (.entrySet o)]
(reduce (fn [m [^String k v]]
(assoc m (keyword k) (->clj v)))
{} entries)))
java.util.List
(->clj [o] (vec (map ->clj o)))
java.lang.Object
(->clj [o] o)
nil
(->clj [_] nil))
(defn as-clj-map
[m]
(->clj m))
Let me know if this works for you & meets your performance requirements.
Regards,
BG
--
Baishampayan Ghose
b.ghose at gmail.com
Glad that it worked for you, Jestan. Protocols are usually the right
approach for these kind of tasks.
Why convert at all? Java maps can be used quite conveniently in Clojure.
If it's because you need the map to be persistent, you might consider
converting each map only when the user attempts to conj or assoc,
rather than doing all the work up front. Since those aren't (yet!)
protocols, I supposed you'd have to either use a non-standard conj and
assoc, or create a wrapper around the Java Maps. Of course there are
complications with all three of these solutions, so you'll have to
choose carefully based on your needs.
--Chouser