Create a gist now

Instantly share code, notes, and snippets.

Representing [Clojure] code in 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