Skip to content

Commit

Permalink
Stop piping json if value is inputstream
Browse files Browse the repository at this point in the history
  • Loading branch information
kauwai committed Oct 7, 2024
1 parent fc4e967 commit 75d2033
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion http/json-codec/project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject house.jux/http.json-codec "2024.07.15"
(defproject house.jux/http.json-codec "2024.10.07"

:description "Ring-style wrapper to transparently encode and decode JSON to/from Clojure values."
:url "https://github.com/klauswuestefeld/simple-clj/tree/master/http/json-codec"
Expand Down
27 changes: 17 additions & 10 deletions http/json-codec/src/house/jux__/http/json_codec__.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@
(:import
[java.io BufferedReader BufferedWriter InputStream InputStreamReader OutputStreamWriter PipedInputStream PipedOutputStream]))

(defn- input-stream? [value]
(instance? InputStream value))

(defn- pipe-json [value]
(let [input (PipedInputStream. (* 1024 32)) ; 32k buffer
output (-> input PipedOutputStream. OutputStreamWriter. BufferedWriter.)]
(future
(try
(json/generate-stream value output)
(finally (.close output))))
input))

(defn- pipe-json-if-necessary [value]
(if (nil? value)
"null"
(let [input (PipedInputStream. (* 1024 32)) ; 32k buffer
output (-> input PipedOutputStream. OutputStreamWriter. BufferedWriter.)]
(future
(try
(json/generate-stream value output)
(finally (.close output))))
input)))
(cond
(nil? value) "null"
(input-stream? value) value
:else (pipe-json value)))

(defn- decode [input]
(-> input InputStreamReader. BufferedReader. (json/parse-stream keyword)))

(defn- decode-if-necessary [body]
(cond-> body
(instance? InputStream body) decode))
(input-stream? body) decode))

(defn wrap [delegate]
(fn [request]
Expand Down

0 comments on commit 75d2033

Please sign in to comment.