diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-05-14 18:32:22 +0000 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-05-14 18:32:22 +0000 |
commit | f79532a56bc284f76b65a0fe7a9b4f5b602b658d (patch) | |
tree | e53e935c3d413490e1aba77d203fdeadfdc359a1 /src/clojure/contrib/json | |
parent | b1ce8f040a1d593ce37689f7082aae02158f7ded (diff) |
json/read.clj: added *json-keyword-keys* to get keywords in maps
Bind *json-keyword-keys* to true to convert map keys to keywords.
Diffstat (limited to 'src/clojure/contrib/json')
-rw-r--r-- | src/clojure/contrib/json/read.clj | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/clojure/contrib/json/read.clj b/src/clojure/contrib/json/read.clj index a76ff5f8..d652106e 100644 --- a/src/clojure/contrib/json/read.clj +++ b/src/clojure/contrib/json/read.clj @@ -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. @@ -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 @@ -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 |