aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2010-02-10 15:16:56 -0500
committerStuart Sierra <mail@stuartsierra.com>2010-02-10 15:16:56 -0500
commit724e7678339a8772bde8677214920c939d16a48f (patch)
treeeec2d2cceb50b5112a94baddb2bf3a038f528d4a
parente65fe6462feb668c1ee1d84fde00081f055c788f (diff)
json: factor out remaining printer functions as defns
-rw-r--r--src/main/clojure/clojure/contrib/json.clj26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/main/clojure/clojure/contrib/json.clj b/src/main/clojure/clojure/contrib/json.clj
index d89f0036..fe0529bc 100644
--- a/src/main/clojure/clojure/contrib/json.clj
+++ b/src/main/clojure/clojure/contrib/json.clj
@@ -17,7 +17,8 @@
To write JSON, use json-str, write-json, or write-json.
To read JSON, use read-json."}
clojure.contrib.json
- (:require [clojure.contrib.java :as j])
+ (:use [clojure.contrib.pprint :only (write formatter-out)]
+ [clojure.contrib.java :only (as-str)])
(:import (java.io PrintWriter PushbackReader StringWriter
StringReader Reader EOFException)))
@@ -235,7 +236,7 @@
(when (nil? k)
(throw (Exception. "JSON object keys cannot be nil/null")))
(.print out \")
- (.print out (j/as-str k))
+ (.print out (as-str k))
(.print out \")
(.print out \:)
(write-json v out))
@@ -263,11 +264,21 @@
(defn- write-json-plain [x #^PrintWriter out]
(.print out x))
+(defn- write-json-null [x #^PrintWriter out]
+ (.print out "null"))
+
+(defn- write-json-named [x #^PrintWriter out]
+ (write-json-string (name x) out))
+
+(defn- write-json-generic [x out]
+ (if (.isArray (class x))
+ (write-json (seq x) out)
+ (throw (Exception. "Don't know how to write JSON of " (class x)))))
+
(extend nil Write-JSON
- {:write-json (fn [x #^PrintWriter out] (.print out "null"))})
+ {:write-json write-json-null})
(extend clojure.lang.Named Write-JSON
- {:write-json (fn [x #^PrintWriter out]
- (write-json-string (name x) out))})
+ {:write-json write-json-named})
(extend java.lang.Boolean Write-JSON
{:write-json write-json-plain})
(extend java.lang.Number Write-JSON
@@ -285,10 +296,7 @@
(extend clojure.lang.ISeq Write-JSON
{:write-json write-json-array})
(extend java.lang.Object Write-JSON
- {:write-json (fn [x out]
- (if (.isArray (class x))
- (write-json (seq x) out)
- (throw (Exception. "Don't know how to print JSON of " (class x)))))})
+ {:write-json write-json-generic})
(defn json-str
"Converts x to a JSON-formatted string."