Permalink
Browse files

Rename 'str' to 'write-str'

* Having a function named 'str' was just too confusing
  • Loading branch information...
1 parent 3c568a9 commit 51ee78711ba411dd2efed78f9bb3b1ad0b64fa3b @stuartsierra stuartsierra committed Oct 5, 2012
Showing with 38 additions and 38 deletions.
  1. +13 −13 src/main/clojure/clojure/data/json.clj
  2. +25 −25 src/test/clojure/clojure/data/json_test.clj
@@ -10,7 +10,7 @@
:doc "JavaScript Object Notation (JSON) parser/generator.
See http://www.json.org/"}
clojure.data.json
- (:refer-clojure :exclude (read str))
+ (:refer-clojure :exclude (read))
(:require [clojure.pprint :as pprint])
(:import (java.io PrintWriter PushbackReader StringWriter
Writer StringReader EOFException)))
@@ -27,7 +27,7 @@
(name x)
(nil? x)
(throw (Exception. "JSON object properties may not be nil"))
- :else (clojure.core/str x)))
+ :else (str x)))
(defn- default-value-fn [k v] v)
@@ -110,7 +110,7 @@
(when (or (neg? a) (neg? b) (neg? c) (neg? d))
(throw (EOFException.
"JSON error (end-of-file inside Unicode character escape)")))
- (let [s (clojure.core/str (char a) (char b) (char c) (char d))]
+ (let [s (str (char a) (char b) (char c) (char d))]
(char (Integer/parseInt s 16)))))
(defn- read-escaped-char [^PushbackReader stream]
@@ -135,7 +135,7 @@
(when (neg? c)
(throw (EOFException. "JSON error (end-of-file inside array)")))
(codepoint-case c
- \" (clojure.core/str buffer)
+ \" (str buffer)
\\ (do (.append buffer (read-escaped-char stream))
(recur))
(do (.append buffer (char c))
@@ -167,8 +167,8 @@
(do (.unread stream c)
decimal?))))]
(if decimal?
- (read-decimal (clojure.core/str buffer))
- (read-integer (clojure.core/str buffer)))))
+ (read-decimal (str buffer))
+ (read-integer (str buffer)))))
(defn- -read
[^PushbackReader stream eof-error? eof-value]
@@ -219,7 +219,7 @@
\[ (read-array stream)
(throw (Exception.
- (clojure.core/str "JSON error (unexpected character): " (char c)))))))))
+ (str "JSON error (unexpected character): " (char c)))))))))
(defn read
"Reads a single item of JSON data from a java.io.Reader. Options are
@@ -303,7 +303,7 @@
(.append sb (format "\\u%04x" cp)) ; Hexadecimal-escaped
(.appendCodePoint sb cp)))))
(.append sb \")
- (.print out (clojure.core/str sb))))
+ (.print out (str sb))))
(defn- write-object [m ^PrintWriter out]
(.print out \{)
@@ -337,7 +337,7 @@
(.print out \]))
(defn- write-bignum [x ^PrintWriter out]
- (.print out (clojure.core/str x)))
+ (.print out (str x)))
(defn- write-plain [x ^PrintWriter out]
(.print out x))
@@ -351,7 +351,7 @@
(defn- write-generic [x out]
(if (.isArray (class x))
(-write (seq x) out)
- (throw (Exception. (clojure.core/str "Don't know how to write JSON of " (class x))))))
+ (throw (Exception. (str "Don't know how to write JSON of " (class x))))))
(defn- write-ratio [x out]
(-write (double x) out))
@@ -422,12 +422,12 @@
*value-fn* value-fn]
(-write x (PrintWriter. writer)))))
-(defn str
+(defn write-str
"Converts x to a JSON-formatted string. Options are the same as
write."
[x & options]
(let [sw (StringWriter.)]
- (apply write-json x sw options)
+ (apply write x sw options)
(.toString sw)))
;;; JSON PRETTY-PRINTER
@@ -456,7 +456,7 @@
(defn pprint
"Pretty-prints JSON representation of x to *out*. Options are the
- same as for write-json except :value-fn, which is not supported."
+ same as for write except :value-fn, which is not supported."
[x & options]
(let [{:keys [escape-unicode escape-slash key-fn]
:or {escape-unicode true
@@ -33,9 +33,9 @@
(deftest handles-unicode-outside-bmp
(is (= "\"smiling face: \uD83D\uDE03\""
- (json/str "smiling face: \uD83D\uDE03" :escape-unicode false)))
+ (json/write-str "smiling face: \uD83D\uDE03" :escape-unicode false)))
(is (= "\"smiling face: \\ud83d\\ude03\""
- (json/str "smiling face: \uD83D\uDE03" :escape-unicode true))))
+ (json/write-str "smiling face: \uD83D\uDE03" :escape-unicode true))))
(deftest handles-escaped-whitespace
(is (= "foo\nbar" (json/read-str "\"foo\\nbar\"")))
@@ -173,55 +173,55 @@
(deftest can-print-json-strings
- (is (= "\"Hello, World!\"" (json/str "Hello, World!")))
- (is (= "\"\\\"Embedded\\\" Quotes\"" (json/str "\"Embedded\" Quotes"))))
+ (is (= "\"Hello, World!\"" (json/write-str "Hello, World!")))
+ (is (= "\"\\\"Embedded\\\" Quotes\"" (json/write-str "\"Embedded\" Quotes"))))
(deftest can-print-unicode
- (is (= "\"\\u1234\\u4567\"" (json/str "\u1234\u4567"))))
+ (is (= "\"\\u1234\\u4567\"" (json/write-str "\u1234\u4567"))))
(deftest can-print-nonescaped-unicode
- (is (= "\"\u1234\u4567\"" (json/str "\u1234\u4567" :escape-unicode false))))
+ (is (= "\"\u1234\u4567\"" (json/write-str "\u1234\u4567" :escape-unicode false))))
(deftest can-print-json-null
- (is (= "null" (json/str nil))))
+ (is (= "null" (json/write-str nil))))
(deftest can-print-ratios-as-doubles
- (is (= "0.75" (json/str 3/4))))
+ (is (= "0.75" (json/write-str 3/4))))
(deftest can-print-bigints
- (is (= "12345678901234567890" (json/str 12345678901234567890))))
+ (is (= "12345678901234567890" (json/write-str 12345678901234567890))))
(deftest can-print-json-arrays
- (is (= "[1,2,3]" (json/str [1 2 3])))
- (is (= "[1,2,3]" (json/str (list 1 2 3))))
- (is (= "[1,2,3]" (json/str (sorted-set 1 2 3))))
- (is (= "[1,2,3]" (json/str (seq [1 2 3])))))
+ (is (= "[1,2,3]" (json/write-str [1 2 3])))
+ (is (= "[1,2,3]" (json/write-str (list 1 2 3))))
+ (is (= "[1,2,3]" (json/write-str (sorted-set 1 2 3))))
+ (is (= "[1,2,3]" (json/write-str (seq [1 2 3])))))
(deftest can-print-java-arrays
- (is (= "[1,2,3]" (json/str (into-array [1 2 3])))))
+ (is (= "[1,2,3]" (json/write-str (into-array [1 2 3])))))
(deftest can-print-empty-arrays
- (is (= "[]" (json/str [])))
- (is (= "[]" (json/str (list))))
- (is (= "[]" (json/str #{}))))
+ (is (= "[]" (json/write-str [])))
+ (is (= "[]" (json/write-str (list))))
+ (is (= "[]" (json/write-str #{}))))
(deftest can-print-json-objects
- (is (= "{\"a\":1,\"b\":2}" (json/str (sorted-map :a 1 :b 2)))))
+ (is (= "{\"a\":1,\"b\":2}" (json/write-str (sorted-map :a 1 :b 2)))))
(deftest object-keys-must-be-strings
- (is (= "{\"1\":1,\"2\":2") (json/str (sorted-map 1 1 2 2))))
+ (is (= "{\"1\":1,\"2\":2") (json/write-str (sorted-map 1 1 2 2))))
(deftest can-print-empty-objects
- (is (= "{}" (json/str {}))))
+ (is (= "{}" (json/write-str {}))))
(deftest accept-sequence-of-nils
- (is (= "[null,null,null]" (json/str [nil nil nil]))))
+ (is (= "[null,null,null]" (json/write-str [nil nil nil]))))
(deftest error-on-nil-keys
- (is (thrown? Exception (json/str {nil 1}))))
+ (is (thrown? Exception (json/write-str {nil 1}))))
(deftest characters-in-symbols-are-escaped
- (is (= "\"foo\\u1b1b\"" (json/str (symbol "foo\u1b1b")))))
+ (is (= "\"foo\\u1b1b\"" (json/write-str (symbol "foo\u1b1b")))))
(deftest default-throws-on-eof
(is (thrown? java.io.EOFException (json/read-str ""))))
@@ -230,7 +230,7 @@
(is (= ::eof (json/read-str "" :eof-error? false :eof-value ::eof))))
(deftest characters-in-map-keys-are-escaped
- (is (= (json/str {"\"" 42}) "{\"\\\"\":42}")))
+ (is (= (json/write-str {"\"" 42}) "{\"\\\"\":42}")))
;;; Pretty-printer
@@ -246,4 +246,4 @@
(time
(dotimes [_ 100]
(assert (= (json/read-str pass1-string false)
- (json/read-str (json/str (json/read-str pass1-string false)) false)))))))
+ (json/read-str (json/write-str (json/read-str pass1-string false)) false)))))))

0 comments on commit 51ee787

Please sign in to comment.