Skip to content

Commit

Permalink
extract sets into def's so they aren't constantly recreated
Browse files Browse the repository at this point in the history
  • Loading branch information
peter royal committed Dec 7, 2010
1 parent e4ea06c commit 7ac5425
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/clojure/clojure/contrib/json.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
(assoc! result (if keywordize? (keyword key) key) (assoc! result (if keywordize? (keyword key) key)
element))))))))) element)))))))))


(def ^{:private true} json-hex-characters #{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 \a \b \c \d \e \f \A \B \C \D \E \F})

(defn- read-json-hex-character [^PushbackReader stream] (defn- read-json-hex-character [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the ;; Expects to be called with the head of the stream AFTER the
;; initial "\u". Reads the next four characters from the stream. ;; initial "\u". Reads the next four characters from the stream.
Expand All @@ -78,17 +80,18 @@
(when (some neg? digits) (when (some neg? digits)
(throw (EOFException. "JSON error (end-of-file inside Unicode character escape)"))) (throw (EOFException. "JSON error (end-of-file inside Unicode character escape)")))
(let [chars (map char digits)] (let [chars (map char digits)]
(when-not (every? #{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 \a \b \c \d \e \f \A \B \C \D \E \F} (when-not (every? json-hex-characters chars)
chars)
(throw (Exception. "JSON error (invalid hex character in Unicode character escape)"))) (throw (Exception. "JSON error (invalid hex character in Unicode character escape)")))
(char (Integer/parseInt (apply str chars) 16))))) (char (Integer/parseInt (apply str chars) 16)))))


(def ^{:private true} json-escape-characters #{\" \\ \/})

(defn- read-json-escaped-character [^PushbackReader stream] (defn- read-json-escaped-character [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the ;; Expects to be called with the head of the stream AFTER the
;; initial backslash. ;; initial backslash.
(let [c (char (.read stream))] (let [c (char (.read stream))]
(cond (cond
(#{\" \\ \/} c) c (json-escape-characters c) c
(= c \b) \backspace (= c \b) \backspace
(= c \f) \formfeed (= c \f) \formfeed
(= c \n) \newline (= c \n) \newline
Expand All @@ -110,6 +113,8 @@
:else (do (.append buffer c) :else (do (.append buffer c)
(recur (.read stream)))))))) (recur (.read stream))))))))


(def ^{:private true} json-numbers-true-false #{\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9})

(defn- read-json-reader (defn- read-json-reader
([^PushbackReader stream keywordize? eof-error? eof-value] ([^PushbackReader stream keywordize? eof-error? eof-value]
(loop [i (.read stream)] (loop [i (.read stream)]
Expand All @@ -124,7 +129,7 @@
(Character/isWhitespace c) (recur (.read stream)) (Character/isWhitespace c) (recur (.read stream))


;; Read numbers, true, and false with Clojure reader ;; Read numbers, true, and false with Clojure reader
(#{\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9} c) (json-numbers-true-false c)
(do (.unread stream i) (do (.unread stream i)
(read stream true nil)) (read stream true nil))


Expand Down

0 comments on commit 7ac5425

Please sign in to comment.