Skip to content
Browse files

DJSON-7: Omit comma after key/value pairs removed by :value-fn

Signed-off-by: Stuart Sierra <mail@stuartsierra.com>
  • Loading branch information...
1 parent d3cce5b commit 20dac1124fbeb0723cd6d349adfd4421f7241f53 @jafingerhut jafingerhut committed with stuartsierra
Showing with 12 additions and 5 deletions.
  1. +5 −4 src/main/clojure/clojure/data/json.clj
  2. +7 −1 src/test/clojure/clojure/data/json_test.clj
View
9 src/main/clojure/clojure/data/json.clj
@@ -311,16 +311,17 @@
(when (seq m)
(let [[k v] (first x)
out-key (*key-fn* k)
- out-value (*value-fn* k v)]
+ out-value (*value-fn* k v)
+ nxt (next x)]
(when-not (string? out-key)
(throw (Exception. "JSON object keys must be strings")))
(when-not (= *value-fn* out-value)
(write-string out-key out)
(.print out \:)
- (-write out-value out)))
- (let [nxt (next x)]
+ (-write out-value out)
+ (when (seq nxt)
+ (.print out \,)))
(when (seq nxt)
- (.print out \,)
(recur nxt)))))
(.print out \}))
View
8 src/test/clojure/clojure/data/json_test.clj
@@ -110,7 +110,13 @@
:value-fn (fn thisfn [k v]
(if (= :date k)
thisfn
- v))))))
+ v)))))
+ (is (= "{\"c\":1,\"e\":2}"
+ (json/write-str {:a nil, :b nil, :c 1, :d nil, :e 2, :f nil}
+ :value-fn (fn remove-nils [k v]
+ (if (nil? v)
+ remove-nils
+ v))))))
(declare pass1-string)

0 comments on commit 20dac11

Please sign in to comment.
Something went wrong with that request. Please try again.