Skip to content

Commit a710cad

Browse files
committed
Use data.json
1 parent 9686f07 commit a710cad

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

frameworks/ring-http-exchange/project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[org.clojars.jj/tassu "1.0.4"]
1010
[org.clojars.jj/async-boa-sql "1.0.11"]
1111
[org.clojars.jj/vertx-pg-client-async-boa-adapter "1.0.1"]
12-
[metosin/jsonista "1.0.0"]
12+
[org.clojure/data.json "2.5.2"]
1313
[org.clojars.jj/majavat "2.0.2"]
1414
[io.github.robaho/httpserver "1.0.29"]
1515
[org.clojure/core.cache "1.2.263"]]

frameworks/ring-http-exchange/src/ring/core.clj

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[jj.sql.async-boa :as boa]
88
[jj.sql.boa.query.vertx-pg :as vertx-adapter]
99
[jj.tassu :refer [GET POST PUT async-route]]
10-
[jsonista.core :as json]
10+
[clojure.data.json :as json]
1111
[ring-http-exchange.core :as server]
1212
[ring-http-exchange.ssl :as ssl])
1313
(:import (io.vertx.core Vertx)
@@ -84,7 +84,7 @@
8484

8585
(defn- load-json [path]
8686
(when (.exists (io/file path))
87-
(json/read-value (slurp path) json/keyword-keys-object-mapper)))
87+
(json/read-str (slurp path) :key-fn keyword)))
8888

8989
(defn- process-item [item ^long m]
9090
(assoc item :total (* (:price item) (:quantity item) m)))
@@ -124,7 +124,7 @@
124124
(.toByteArray baos)))
125125

126126
(defn- json-response [data]
127-
{:status 200 :headers json-headers :body (json/write-value-as-string data)})
127+
{:status 200 :headers json-headers :body (json/write-str data)})
128128

129129
(defn- text-response [s]
130130
{:status 200 :headers text-headers :body (str s)})
@@ -148,7 +148,7 @@
148148
:price (:price row)
149149
:quantity (:quantity row)
150150
:active (:active row)
151-
:tags (json/read-value (str (:tags row)))
151+
:tags (json/read-str (str (:tags row)))
152152
:rating {:score (:rating_score row) :count (:rating_count row)}})
153153

154154
(defn- pem->keystore [^String cert-path ^String key-path]
@@ -233,8 +233,7 @@
233233
params (parse-qs (:query-string req))
234234
m (safe-parse-long (get params param-m) 1)
235235
items (map #(process-item % m) (subvec dataset 0 n))
236-
body-bytes (json/write-value-as-bytes
237-
{:items items :count (clojure.core/count items)})]
236+
body-bytes (.getBytes (json/write-str {:items items :count (clojure.core/count items)}) "UTF-8")]
238237
(respond
239238
(if (accepts-gzip? (:headers req))
240239
{:status 200 :headers json-gzip-headers :body (gzip-bytes body-bytes)}
@@ -245,16 +244,24 @@
245244
(respond (text-response (.transferTo in (OutputStream/nullOutputStream))))))
246245

247246
(defn- handle-pg [pg-pool req respond _raise]
248-
(let [params (parse-qs (:query-string req))
249-
min-p (safe-parse-double (get params param-min) 10.0)
250-
max-p (safe-parse-double (get params param-max) 50.0)
251-
limit (safe-parse-long (get params param-limit) 50)]
252-
(pg-query pg-pool {:min min-p :max max-p :limit limit}
253-
(fn [rows]
254-
(let [items (map transform-pg-row rows)]
255-
(respond (json-response {:items items :count (count items)}))))
256-
(fn [_]
257-
(respond (json-response {:items [] :count 0}))))))
247+
(if (nil? pg-pool)
248+
(respond (json-response {:items [] :count 0}))
249+
(let [params (parse-qs (:query-string req))
250+
min-p (safe-parse-long (get params param-min) 10)
251+
max-p (safe-parse-long (get params param-max) 50)
252+
limit (safe-parse-long (get params param-limit) 50)]
253+
(try
254+
(pg-query pg-pool {:min min-p :max max-p :limit limit}
255+
(fn [rows]
256+
(try
257+
(let [items (mapv transform-pg-row rows)]
258+
(respond (json-response {:items items :count (count items)})))
259+
(catch Throwable _
260+
(respond (json-response {:items [] :count 0})))))
261+
(fn [_]
262+
(respond (json-response {:items [] :count 0}))))
263+
(catch Throwable _
264+
(respond (json-response {:items [] :count 0})))))))
258265

259266
(defn- handle-fortunes [pg-pool respond raise]
260267
(fortunes-query
@@ -289,7 +296,7 @@
289296
:price (long (:price row))
290297
:quantity (long (:quantity row))
291298
:active (:active row)
292-
:tags (json/read-value (str (:tags row)))
299+
:tags (json/read-str (str (:tags row)))
293300
:rating {:score (long (:rating_score row)) :count (long (:rating_count row))}})
294301

295302
(defn- handle-crud-list [pg-pool req respond raise]
@@ -316,14 +323,14 @@
316323
(crud-read-query pg-pool {:id id}
317324
(fn [rows]
318325
(if-let [row (first rows)]
319-
(let [json-str (json/write-value-as-string (transform-crud-row row))]
326+
(let [json-str (json/write-str (transform-crud-row row))]
320327
(crud-cache-set id json-str)
321328
(respond {:status 200 :headers crud-miss-headers :body json-str}))
322329
(respond {:status 404 :headers json-headers :body not-found-body})))
323330
raise)))))
324331

325332
(defn- handle-crud-create [pg-pool req respond raise]
326-
(let [body (json/read-value (:body req) json/keyword-keys-object-mapper)
333+
(let [body (json/read-str (slurp (:body req)) :key-fn keyword)
327334
id (:id body)
328335
nm (or (:name body) "New Product")
329336
category (or (:category body) "test")
@@ -333,7 +340,7 @@
333340
(fn [rows]
334341
(respond {:status 201
335342
:headers json-headers
336-
:body (json/write-value-as-string
343+
:body (json/write-str
337344
{:id (:id (first rows))
338345
:name nm
339346
:category category
@@ -345,7 +352,7 @@
345352
(let [id (safe-parse-long (get-in req [:params :id]) nil)]
346353
(if (nil? id)
347354
(respond {:status 404 :headers json-headers :body not-found-body})
348-
(let [body (json/read-value (:body req) json/keyword-keys-object-mapper)
355+
(let [body (json/read-str (slurp (:body req)) :key-fn keyword)
349356
nm (or (:name body) "Updated")
350357
price (or (:price body) 0)
351358
quantity (or (:quantity body) 0)]
@@ -356,7 +363,7 @@
356363
(crud-cache-evict id)
357364
(respond {:status 200
358365
:headers json-headers
359-
:body (json/write-value-as-string
366+
:body (json/write-str
360367
{:id id
361368
:name nm
362369
:price price

0 commit comments

Comments
 (0)