Skip to content
Browse files

json: factor out remaining printer functions as defns

  • Loading branch information...
1 parent e65fe64 commit 724e7678339a8772bde8677214920c939d16a48f @stuartsierra stuartsierra committed Feb 10, 2010
Showing with 17 additions and 9 deletions.
  1. +17 −9 src/main/clojure/clojure/contrib/json.clj
26 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."}
- (:require [ :as j])
+ (:use [clojure.contrib.pprint :only (write formatter-out)]
+ [ :only (as-str)])
(:import ( 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."

0 comments on commit 724e767

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