Permalink
Browse files

Handle Ratios by converting to doubles

  • Loading branch information...
stuartsierra committed Jan 8, 2011
1 parent 1068a72 commit ea62e3ec4c3a3b977b672ed19cc5cba999f1f700
@@ -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
@@ -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))))

0 comments on commit ea62e3e

Please sign in to comment.