Last active
June 23, 2016 08:07
-
-
Save ThomasDeutsch/667dc44cb301e646de76d92f54f921d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; i used the new-js->clj3 function from Darrick Wiebe | |
;; https://gist.github.com/pangloss/591d77231fda460c2fbe | |
;; i have added a :ns parameter | |
(let [data | |
"[{ \"web-app\": { \"servlet\": [ { \"servlet-name\": \"cofaxCDS\", \"servlet-class\": \"org.cofax.cds.CDSServlet\", \"init-param\": { \"configGlossary:installationAt\": \"Philadelphia, PA\", \"configGlossary:adminEmail\": \"ksm@pobox.com\", \"configGlossary:poweredBy\": \"Cofax\", \"configGlossary:poweredByIcon\": \"/images/cofax.gif\", \"configGlossary:staticPath\": \"/content/static\", \"templateProcessorClass\": \"org.cofax.WysiwygTemplate\", \"templateLoaderClass\": \"org.cofax.FilesTemplateLoader\", \"templatePath\": \"templates\" } }, { \"servlet-name\": \"cofaxAdmin\", \"servlet-class\": \"org.cofax.cds.AdminServlet\" }, { \"servlet-name\": \"cofaxTools\", \"servlet-class\": \"org.cofax.cms.CofaxToolsServlet\", \"init-param\": { \"templatePath\": \"toolstemplates/\", \"log\": 1, \"logLocation\": \"/usr/local/tomcat/logs/CofaxTools.log\", \"logMaxSize\": \"\", \"dataLog\": 1, \"dataLogLocation\": \"/usr/local/tomcat/logs/dataLog.log\", \"dataLogMaxSize\": \"\", \"removePageCache\": \"/content/admin/remove?cache=pages&id=\", \"removeTemplateCache\": \"/content/admin/remove?cache=templates&id=\", \"fileTransferFolder\": \"/usr/local/tomcat/webapps/content/fileTransferFolder\", \"lookInContext\": 1, \"adminGroupID\": 4, \"betaServer\": true } } ], \"servlet-mapping\": { \"cofaxCDS\": \"/\", \"cofaxEmail\": \"/cofaxutil/aemail/*\", \"cofaxAdmin\": \"/admin/*\", \"fileServlet\": \"/static/*\", \"cofaxTools\": \"/tools/*\" }, \"taglib\": { \"taglib-uri\": \"cofax.tld\", \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\" } } }, | |
{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"} ] } }} ]" | |
] | |
(def js-data (js/JSON.parse data))) | |
(defn new-js->clj4 | |
"Recursively transforms JavaScript arrays into ClojureScript | |
vectors, and JavaScript objects into ClojureScript maps. With | |
option ':keywordize-keys true' will convert object fields from | |
strings to keywords." | |
([x] (new-js->clj4 x {:keywordize-keys false | |
:ns nil})) | |
([x & opts] | |
(cond | |
(satisfies? IEncodeClojure x) | |
(-js->clj x (apply array-map opts)) | |
(seq opts) | |
(let [{:keys [keywordize-keys ns]} opts | |
keyfn (if keywordize-keys | |
(if ns | |
(partial keyword ns) | |
keyword) | |
str) | |
f (fn thisfn [x] | |
(cond | |
(seq? x) | |
(doall (map thisfn x)) | |
(coll? x) | |
(into (empty x) (map thisfn) x) | |
(array? x) | |
(persistent! | |
(reduce #(conj! %1 (thisfn %2)) | |
(transient []) x)) | |
(identical? (type x) js/Object) | |
(persistent! | |
(reduce (fn [r k] (assoc! r (keyfn k) (thisfn (aget x k)))) | |
(transient {}) (js-keys x))) | |
:else x))] | |
(f x))))) | |
(time | |
(dotimes [_ 1000] | |
(new-js->clj4 js-data :keywordize-keys true))) | |
(time | |
(dotimes [_ 1000] | |
(js->clj js-data :keywordize-keys true))) | |
(time | |
(dotimes [_ 1000] | |
(new-js->clj4 js-data :keywordize-keys true :ns "NS"))) | |
(println "TEST-with-namespace: " (new-js->clj4 js-data :keywordize-keys true :ns "my-ns")) | |
(println "TEST-without-namespace: " (new-js->clj4 js-data :keywordize-keys true)) | |
(println "same?" (= (new-js->clj4 js-data :keywordize-keys true) | |
(js->clj js-data :keywordize-keys true))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment