diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2011-01-08 16:45:03 -0500 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2011-01-08 16:45:03 -0500 |
commit | ea62e3ec4c3a3b977b672ed19cc5cba999f1f700 (patch) | |
tree | a3e6e3c220b4da196e31b5a742eae9fa0e04cd1a | |
parent | 1068a72b5a1d47c98c4b8b13c4ce1904f6a755d8 (diff) |
Handle Ratios by converting to doubles
-rw-r--r-- | modules/json/src/main/clojure/clojure/contrib/json.clj | 7 | ||||
-rw-r--r-- | modules/json/src/test/clojure/clojure/contrib/test_json.clj | 3 |
2 files changed, 9 insertions, 1 deletions
diff --git a/modules/json/src/main/clojure/clojure/contrib/json.clj b/modules/json/src/main/clojure/clojure/contrib/json.clj index bcb59e2b..9ff79ca1 100644 --- a/modules/json/src/main/clojure/clojure/contrib/json.clj +++ b/modules/json/src/main/clojure/clojure/contrib/json.clj @@ -278,7 +278,10 @@ (if (.isArray (class x)) (write-json (seq x) out escape-unicode?) (throw (Exception. (str "Don't know how to write JSON of " (class x)))))) - + +(defn- write-json-ratio [x out escape-unicode?] + (write-json (double x) out escape-unicode?)) + (extend nil Write-JSON {:write-json write-json-null}) (extend clojure.lang.Named Write-JSON @@ -291,6 +294,8 @@ {:write-json write-json-bignum}) (extend java.math.BigDecimal Write-JSON {:write-json write-json-bignum}) +(extend clojure.lang.Ratio Write-JSON + {:write-json write-json-ratio}) (extend java.lang.CharSequence Write-JSON {:write-json write-json-string}) (extend java.util.Map Write-JSON diff --git a/modules/json/src/test/clojure/clojure/contrib/test_json.clj b/modules/json/src/test/clojure/clojure/contrib/test_json.clj index 33a7a0f2..df3409ec 100644 --- a/modules/json/src/test/clojure/clojure/contrib/test_json.clj +++ b/modules/json/src/test/clojure/clojure/contrib/test_json.clj @@ -150,6 +150,9 @@ (deftest can-print-json-null (is (= "null" (json-str nil)))) +(deftest can-print-ratios-as-doubles + (is (= "0.75" (json-str 3/4)))) + (deftest can-print-json-arrays (is (= "[1,2,3]" (json-str [1 2 3]))) (is (= "[1,2,3]" (json-str (list 1 2 3)))) |