diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-05-14 18:17:59 +0000 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-05-14 18:17:59 +0000 |
commit | b1ce8f040a1d593ce37689f7082aae02158f7ded (patch) | |
tree | f11c25c3ab38b435ea2b75926b7e39c589193de1 | |
parent | c375d4481973dde6111c879e0543df8637da71af (diff) |
json/write.clj: use StringBuilder when escaping strings for faster output
-rw-r--r-- | src/clojure/contrib/json/write.clj | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/src/clojure/contrib/json/write.clj b/src/clojure/contrib/json/write.clj index c7b985b7..9db4a979 100644 --- a/src/clojure/contrib/json/write.clj +++ b/src/clojure/contrib/json/write.clj @@ -113,25 +113,27 @@ Within strings, all non-ASCII characters are hexadecimal escaped. (print \})) (defmethod print-json java.lang.CharSequence [#^CharSequence s] - (print \") - (dotimes [i (count s)] - (let [cp (Character/codePointAt s i)] - (cond - ;; Handle printable JSON escapes before ASCII - (= cp 34) (print "\\\"") - (= cp 92) (print "\\\\") - (= cp 47) (print "\\/") - ;; Print simple ASCII characters - (< 31 cp 127) (print (.charAt s i)) - ;; Handle non-printable JSON escapes - (= cp 8) (print "\\b") - (= cp 12) (print "\\f") - (= cp 10) (print "\\n") - (= cp 13) (print "\\r") - (= cp 9) (print "\\t") - ;; Any other character is printed as Hexadecimal escape - :else (printf "\\u%04x" cp)))) - (print \")) + (let [sb (StringBuilder. (count s))] + (.append sb \") + (dotimes [i (count s)] + (let [cp (Character/codePointAt s i)] + (cond + ;; Handle printable JSON escapes before ASCII + (= cp 34) (.append sb "\\\"") + (= cp 92) (.append sb "\\\\") + (= cp 47) (.append sb "\\/") + ;; Print simple ASCII characters + (< 31 cp 127) (.append sb (.charAt s i)) + ;; Handle non-printable JSON escapes + (= cp 8) (.append sb "\\b") + (= cp 12) (.append sb "\\f") + (= cp 10) (.append sb "\\n") + (= cp 13) (.append sb "\\r") + (= cp 9) (.append sb "\\t") + ;; Any other character is Hexadecimal-escaped + :else (.append sb (format "\\u%04x" cp))))) + (.append sb \") + (print (str sb)))) (defn json-str "Converts x to a JSON-formatted string." |