Created
November 9, 2008 15:48
-
-
Save cmiles74/23299 to your computer and use it in GitHub Desktop.
Clojure code for transforming sequences into JSON data
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
; | |
; Provides functions for transforming data into JSON objects. | |
; | |
; This is Clojure code. | |
; | |
(defn data-to-json [list-this] | |
"Transforms a list into a string of JSON data" | |
(cond | |
; handle sequences and vectors | |
(or (seq? list-this) (vector? list-this)) | |
(apply str | |
(concat "[" | |
(interpose ", " | |
(map data-to-json list-this)) "]")) | |
; handle maps | |
(map? list-this) | |
(apply str | |
(concat "{" | |
(interpose ", " | |
(map #(str "\"" (name (first %)) "\": " | |
(data-to-json (second %))) list-this)) "}")) | |
; handle atoms | |
:else (str "\"" list-this "\""))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment