Skip to content

Instantly share code, notes, and snippets.

@spacepluk
Last active December 23, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacepluk/f682f387fb3cd93f4d72 to your computer and use it in GitHub Desktop.
Save spacepluk/f682f387fb3cd93f4d72 to your computer and use it in GitHub Desktop.
(defn java->clj
"Recursively transforms Java arrays into Clojure vectors, and Java Maps into
Clojure persistent maps. With option ':keywordize-keys true' will convert
keys from strings to keywords."
([x] (java->clj x :keywordize-keys false))
([x & opts]
(let [{:keys [keywordize-keys]} opts
keyfn (if keywordize-keys keyword identity)
f (fn thisfn [x]
(cond
(seq? x)
(doall (map thisfn x))
(coll? x)
(into (empty x) (map thisfn x))
(instance? java.util.List x)
(vec (map thisfn x))
(instance? java.util.Map x)
(into {} (for [[k v] x] [(keyfn k) (thisfn v)]))
:else x))]
(f x))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment