From 762bc940460a28b2085f7b0d73c2603df5c9414e Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 2 Mar 2014 09:08:29 -0500 Subject: [PATCH] add java-to-clojure conversions --- src/clj_jtwig/convert.clj | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/clj_jtwig/convert.clj diff --git a/src/clj_jtwig/convert.clj b/src/clj_jtwig/convert.clj new file mode 100644 index 0000000..b5ab2d5 --- /dev/null +++ b/src/clj_jtwig/convert.clj @@ -0,0 +1,51 @@ +(ns clj-jtwig.convert) + +(defprotocol JavaToClojure + (convert [x])) + +(extend-protocol JavaToClojure + java.util.Collection + (convert [x] + (map convert x)) + + java.util.Map + (convert [x] + (->> x + (.entrySet) + (reduce + (fn [m [k v]] + ; TODO: perhaps we should be doing (keyword k) instead? i don't like that it technically is not an + ; exact conversion if we do it that way though, even if it is more idiomatic for clojure ... + (assoc m k (convert v))) + {}))) + + java.lang.Number + (convert [x] + x) + + java.lang.Boolean + (convert [x] + x) + + java.lang.Character + (convert [x] + x) + + java.lang.String + (convert [x] + x) + + java.lang.Object + (convert [x] + (-> x + (bean) ; TODO: this is definitely not the fastest method ... + (dissoc :class))) + + nil + (convert [_] + nil)) + +(defn java->clojure + "converts a java value to an equivalent value using one of the clojure data types" + [x] + (convert x))