Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
[Clojure] code-to-json
; Context:
(defn escape-string [x]
(clojure.string/replace x #"^[':\\]" "\\\\$0"))
(defn code-to-json [x]
(condp #(%1 %2) x
number? x
symbol? (str \' (name x))
keyword? (str \: (name x))
string? (escape-string x)
list? (into [] (cons "list" (map code-to-json x)))
vector? (into [] (cons "vector" (map code-to-json x)))
set? (into [] (cons "set" (map code-to-json x)))
map? (into {} (map #(mapv code-to-json %) x))
(throw (Exception. (format "Unsupported type: %s" (type x))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.