From ea62e3ec4c3a3b977b672ed19cc5cba999f1f700 Mon Sep 17 00:00:00 2001 From: Stuart Sierra Date: Sat, 8 Jan 2011 16:45:03 -0500 Subject: Handle Ratios by converting to doubles --- modules/json/src/main/clojure/clojure/contrib/json.clj | 7 ++++++- modules/json/src/test/clojure/clojure/contrib/test_json.clj | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) 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)))) -- cgit v1.2.3-18-g5258