Navigation Menu

Skip to content

Commit

Permalink
json/read.clj: added *json-keyword-keys* to get keywords in maps
Browse files Browse the repository at this point in the history
Bind *json-keyword-keys* to true to convert map keys to keywords.
  • Loading branch information
Stuart Sierra committed May 14, 2009
1 parent b1ce8f0 commit f79532a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/clojure/contrib/json/read.clj
Expand Up @@ -52,6 +52,10 @@

(declare read-json)

(def #^{:doc "If true, JSON object keys will be converted to keywords
instead of strings. Defaults to false. There are no checks that the strings form valid
keywords."} *json-keyword-keys* false)

(defn- read-json-array [#^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; opening bracket.
Expand Down Expand Up @@ -90,7 +94,9 @@
(if (string? element)
(recur (.read stream) element result)
(throw (Exception. "JSON error (non-string key in object)")))
(recur (.read stream) nil (assoc result key element)))))))))
(recur (.read stream) nil
(assoc result (if *json-keyword-keys* (keyword key) key)
element)))))))))

(defn- read-json-hex-character [#^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
Expand Down Expand Up @@ -254,6 +260,11 @@
(deftest- disallows-unclosed-objects
(is (thrown? Exception (read-json-string "{\"a\":1, "))))

(deftest- can-get-keyword-keys
(is (= {:a [1 2 {:b [3 "four"]} 5.5]}
(binding [*json-keyword-keys* true]
(read-json-string "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}")))))

(declare *pass1-string*)

(deftest- pass1-test
Expand Down

0 comments on commit f79532a

Please sign in to comment.