Skip to content

Instantly share code, notes, and snippets.

@st
Last active August 16, 2017 16:10
Show Gist options
  • Save st/db8b11f5bff4cbff81b3 to your computer and use it in GitHub Desktop.
Save st/db8b11f5bff4cbff81b3 to your computer and use it in GitHub Desktop.
useful for interop with "java" maps
(defn clojurify
[exp]
(cond
(instance? java.util.Map exp) (into {} (for [[k v] exp] [(keyword k) v]))
(instance? java.util.List exp) (into [] exp)
:else exp))
(defn walk-to-clojure-map
[java-map]
(clojure.walk/prewalk clojurify java-map))
(deftest walk-to-clojure-map-should-recursively-keywordize-a-java-map
(is (=
{:A 42 :c {:B [{:Z 111} {:e 1515}]}}
(walk-to-clojure-map
(java.util.HashMap. {"A" 42 "c" {"B" (java.util.ArrayList. [(java.util.HashMap. {"Z" 111}) {"e" 1515}])}})))))
@mpenet
Copy link

mpenet commented Apr 8, 2015

you can replace the for with clojure.walk/keywordize-keys

@st
Copy link
Author

st commented Apr 8, 2015

Hi Max,
started with your idea, but couldn't make it in one pass.
I mean, one traversal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment