Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: json-strict support #175

Merged
merged 3 commits into from
Jan 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ The client transparently accepts and decompresses the `gzip` and

;; Coerce as json
(client/get "http://site.com/foo.json" {:as :json})
(client/get "http://site.com/foo.json" {:as :json-strict})
(client/get "http://site.com/foo.json" {:as :json-string-keys})

;; Coerce as a clojure datastructure
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[commons-codec "1.9"]
[commons-io "2.4"]
[slingshot "0.10.3"]
[cheshire "5.3.0"]
[cheshire "5.3.1"]
[crouton "0.1.1"]
[org.clojure/tools.reader "0.8.3"]]
:profiles {:dev {:dependencies [[org.clojure/clojure "1.5.1"]
Expand Down
30 changes: 21 additions & 9 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode")) args))

(defn ^:dynamic json-decode-strict
"Resolve and apply cheshire's json decoding dynamically (with lazy parsing disabled)."
[& args]
{:pre [json-enabled?]}
(apply (ns-resolve (symbol "cheshire.core") (symbol "decode-strict")) args))

(defn update [m k f & args]
(assoc m k (apply f (m k) args)))

Expand Down Expand Up @@ -273,29 +279,34 @@
(assoc resp :body (java.io.ByteArrayInputStream. body)))))

(defn coerce-json-body
[{:keys [coerce]} {:keys [body status] :as resp} keyword? & [charset]]
[{:keys [coerce]} {:keys [body status] :as resp} keyword? strict? & [charset]]
(let [^String charset (or charset "UTF-8")
body (util/force-byte-array body)]
body (util/force-byte-array body)
decode-func (if strict? json-decode-strict json-decode)]
(if json-enabled?
(cond
(= coerce :always)
(assoc resp :body (json-decode (String. #^"[B" body charset) keyword?))
(assoc resp :body (decode-func (String. #^"[B" body charset) keyword?))

(and (unexceptional-status? status)
(or (nil? coerce) (= coerce :unexceptional)))
(assoc resp :body (json-decode (String. #^"[B" body charset) keyword?))
(assoc resp :body (decode-func (String. #^"[B" body charset) keyword?))

(and (not (unexceptional-status? status)) (= coerce :exceptional))
(assoc resp :body (json-decode (String. #^"[B" body charset) keyword?))
(assoc resp :body (decode-func (String. #^"[B" body charset) keyword?))

:else (assoc resp :body (String. #^"[B" body charset)))
(assoc resp :body (String. #^"[B" body charset)))))

(defmethod coerce-response-body :json [req resp]
(coerce-json-body req resp true))
(coerce-json-body req resp true false))

;; XXX Will we have a use case where we want strict JSON with string keys?
(defmethod coerce-response-body :json-strict [req resp]
(coerce-json-body req resp true true))

(defmethod coerce-response-body :json-string-keys [req resp]
(coerce-json-body req resp false))
(coerce-json-body req resp false false))

(defmethod coerce-response-body :clojure [_ {:keys [body] :as resp}]
(let [body (util/force-byte-array body)]
Expand Down Expand Up @@ -325,8 +336,9 @@
(do
(if-let [charset (second (re-find #"charset=(.*)"
(str typestring)))]
(coerce-json-body req resp true charset)
(coerce-json-body req resp true "UTF-8")))
;; Defaulting to lazy parsing w/ symbol keys.
(coerce-json-body req resp true false charset)
(coerce-json-body req resp true false "UTF-8")))

:else
(coerce-response-body {:as :default} resp))))
Expand Down
9 changes: 9 additions & 0 deletions test/clj_http/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
[:get "/json"]
{:status 200 :body "{\"foo\":\"bar\"}"
:headers {"content-type" "application/json"}}
[:get "/json-array"]
{:status 200 :body "[\"foo\", \"bar\"]"
:headers {"content-type" "application/json"}}
[:get "/json-bad"]
{:status 400 :body "{\"foo\":\"bar\"}"}
[:get "/redirect"]
Expand Down Expand Up @@ -328,6 +331,7 @@
(deftest ^{:integration true} t-json-output-coercion
(run-server)
(let [resp (client/get (localhost "/json") {:as :json})
resp-array (client/get (localhost "/json-array") {:as :json-strict})
resp-str (client/get (localhost "/json")
{:as :json :coerce :exceptional})
resp-auto (client/get (localhost "/json") {:as :auto})
Expand All @@ -341,11 +345,16 @@
:coerce :unexceptional})]
(is (= 200
(:status resp)
(:status resp-array)
(:status resp-str)
(:status resp-auto)))
(is (= {:foo "bar"}
(:body resp)
(:body resp-auto)))
(is (= ["foo", "bar"]
(:body resp-array)))
;; '("foo" "bar") and ["foo" "bar"] compare as equal with =.
(is (vector? (:body resp-array)))
(is (= "{\"foo\":\"bar\"}" (:body resp-str)))
(is (= 400
(:status bad-resp)
Expand Down