aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-05-12 20:02:25 +0000
committerStuart Sierra <mail@stuartsierra.com>2009-05-12 20:02:25 +0000
commitf7b98d0d5e85bf2dbd9d1601d2ff7b5990700a98 (patch)
tree1c424da07ea992fe995fae4eedf5bb322016ca72
parentf6266fbcd45ce1695244b8b01fcb55df2c741b2d (diff)
json/write.clj: throw Exception if map key is nil
-rw-r--r--src/clojure/contrib/json/write.clj29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/clojure/contrib/json/write.clj b/src/clojure/contrib/json/write.clj
index 5395c57e..77f04cf5 100644
--- a/src/clojure/contrib/json/write.clj
+++ b/src/clojure/contrib/json/write.clj
@@ -79,26 +79,29 @@ Within strings, all non-ASCII characters are hexadecimal escaped.
(defmethod print-json ::array [s]
(print \[)
(loop [x s]
- (when (> (count x) 0)
- (let [fst (first x)]
+ (when (seq x)
+ (let [fst (first x)
+ nxt (next x)]
(print-json fst)
- (let [nxt (next x)]
- (when (> (count nxt) 0)
- (print \,)
- (recur nxt))))))
+ (when (seq nxt)
+ (print \,)
+ (recur nxt)))))
(print \]))
(defmethod print-json ::object [m]
(print \{)
(loop [x m]
- (when (first x)
+ (when (seq m)
(let [[k v] (first x)]
+ (when (nil? k)
+ (throw (Exception. "JSON object keys cannot be nil/null")))
(print-json (j/as-str k))
(print \:)
(print-json v))
- (when (next x)
- (print \,)
- (recur (next x)))))
+ (let [nxt (next x)]
+ (when (seq nxt)
+ (print \,)
+ (recur nxt)))))
(print \}))
(defmethod print-json java.lang.CharSequence [s]
@@ -169,3 +172,9 @@ Within strings, all non-ASCII characters are hexadecimal escaped.
(deftest- can-print-empty-objects
(is (= "{}" (json-str {}))))
+
+(deftest- accept-sequence-of-nils
+ (is (= "[null,null,null]" (json-str [nil nil nil]))))
+
+(deftest- error-on-nil-keys
+ (is (thrown? Exception (json-str {nil 1}))))