Skip to content

Instantly share code, notes, and snippets.

@mpenet
Created November 18, 2012 22:06
Show Gist options
  • Save mpenet/4107823 to your computer and use it in GitHub Desktop.
Save mpenet/4107823 to your computer and use it in GitHub Desktop.
(defprotocol JSCodec
(clj->js [x])
(key->js [x])
(js->clj [x][x options]))
(extend-protocol JSCodec
default
(key->js [k]
(if (coll? k)
(prn-str k)
(clj->js k)))
(clj->js [x]
(cond
(keyword? x) (name x)
(symbol? x) (str x)
(map? x) (let [m (js-obj)]
(doseq [[k v] x]
(aset m (key->js k) (clj->js v)))
m)
(coll? x) (apply array (map clj->js x))
:else x))
(js->clj
([x options]
(let [{:keys [keywordize-keys]} options
keyfn (if keywordize-keys keyword str)
f (fn thisfn [x]
(cond
(seq? x) (doall (map thisfn x))
(coll? x) (into (empty x) (map thisfn x))
(goog.isArray x) (vec (map thisfn x))
(identical? (type x) js/Object) (into {} (for [k (js-keys x)]
[(keyfn k)
(thisfn (aget x k))]))
:else x))]
(f x)))
([x] (js->clj x {:keywordize-keys false})))
nil
(clj->js [x] nil))
@mpenet
Copy link
Author

mpenet commented Nov 18, 2012

There is probably some overhead using a protocol (just a guess) but this would make life easier when dealing with nested structures with custom types (either clojure or js instances depending on the fn).

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