Skip to content

Commit

Permalink
Leave only one json library in deps (jsonista or data.json) (#279)
Browse files Browse the repository at this point in the history
* add jsonista

* remove data.json

* add jsonista

* refactor read calls

* remove data.json

* add jsonista

* refactor write calls

* add jsonista

* refactor write calls

* refactor read calls

* remove data.json

* add jsonista

* refactor read calls

* refactor write calls

* add jsonista

* refactor read calls

* remove data.json

* add jsonista

* refactor write calls

* refactor read calls

* remove data.json

* add jsonista

* refactor write calls

* remove data.json

* add jsonista

* refactor write calls

* remove data.json

* add jsonista

* refactor read calls

* remove data.json

* add jsonista

* refactor read calls

* remove data.json

* remove data.json

* run format alias
  • Loading branch information
theodor-meresescu authored Feb 1, 2024
1 parent a469c61 commit 8145743
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/core.async {:mvn/version "1.5.648"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/data.json {:mvn/version "2.4.0"}
org.clojure/data.xml {:mvn/version "0.0.8"}
com.draines/postal {:mvn/version "2.0.5"}
com.flexiana/tiny-rbac {:mvn/version "0.1.1"}
Expand All @@ -29,7 +28,8 @@
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.3"}
ring/ring {:mvn/version "1.10.0"}
javax.servlet/servlet-api {:mvn/version "2.5"}
hiccup/hiccup {:mvn/version "2.0.0-RC2"}}
hiccup/hiccup {:mvn/version "2.0.0-RC2"}
metosin/jsonista {:mvn/version "0.3.8"}}

:aliases
{:dev
Expand Down
6 changes: 3 additions & 3 deletions examples/acl/test/comments_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
(:require
[acl]
[acl-fixture :refer [std-system-fixture]]
[clojure.data.json :refer [read-str]]
[clojure.test :refer [deftest is use-fixtures]]
[helpers :refer [test_member
test_admin]]
[jsonista.core :as j]
[post-helpers :refer [init-db-with-two-posts
all-post-ids]]))

Expand All @@ -19,7 +19,7 @@
:content "Test comment on first post"})
(let [new-posts (-> (helpers/fetch "posts/comments" test_admin)
:body
(read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:data
:posts)]
(is (= 1 (->> (filter #(#{first-id} (:posts/id %)) new-posts)
Expand All @@ -36,7 +36,7 @@
[response]
(-> response
:body
(read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:data
:comments))

Expand Down
8 changes: 4 additions & 4 deletions examples/acl/test/helpers.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns helpers
(:require
[clj-http.client :refer [request]]
[clojure.data.json :refer [write-str]]))
[jsonista.core :as j]))

(def test_member "611d7f8a-456d-4f3c-802d-4d869dcd89bf")
(def test_admin "b651939c-96e6-4fbb-88fb-299e728e21c8")
Expand Down Expand Up @@ -46,14 +46,14 @@
:headers {"Authorization" test_admin
"Content-Type" "application/json;charset=utf-8"}
:url (format "http://localhost:3333/%s" (name uri))
:body (write-str content)}))
:body (j/write-value-as-string content)}))
([uri user content]
(request {:unexceptional-status (constantly true)
:method :put
:headers {"Authorization" user
"Content-Type" "application/json;charset=utf-8"}
:url (format "http://localhost:3333/%s" (name uri))
:body (write-str content)})))
:body (j/write-value-as-string content)})))

(defn post
[uri user id content]
Expand All @@ -63,5 +63,5 @@
"Content-Type" "application/json;charset=utf-8"}
:url (format "http://localhost:3333/%s" (name uri))
:query-params {:id id}
:body (write-str content)}))
:body (j/write-value-as-string content)}))

8 changes: 4 additions & 4 deletions examples/acl/test/post_helpers.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns post-helpers
(:require
[clojure.data.json :refer [read-str]]
[helpers]))
[helpers]
[jsonista.core :as j]))

(defn init-db-with-two-posts
[]
Expand All @@ -12,7 +12,7 @@
(defn post-ids
[body]
(map :posts/id (-> body
(read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:data
:posts)))

Expand All @@ -25,6 +25,6 @@
(defn update-count
[body]
(-> body
(read-str :key-fn clojure.core/keyword)
(j/read-value j/keyword-keys-object-mapper)
(get-in [:data :posts])
count))
6 changes: 3 additions & 3 deletions examples/acl/test/posts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[acl]
[acl-fixture]
[clj-http.client :as http]
[clojure.data.json :as json]
[clojure.test :refer [deftest is use-fixtures]]
[helpers :refer [delete
put
Expand All @@ -12,6 +11,7 @@
test_member
test_admin
test_staff]]
[jsonista.core :as j]
[post-helpers :refer [post-ids
update-count
init-db-with-two-posts
Expand Down Expand Up @@ -151,7 +151,7 @@
:headers {"Authorization" test_admin
"Content-Type" "application/json;charset=utf-8"}
:unexceptional-status (constantly true)
:body (json/write-str {:ids (butlast ids)})
:body (j/write-value-as-string {:ids (butlast ids)})
:method :post}
http/request
:body
Expand All @@ -168,7 +168,7 @@
:content "test comment on test post"})
result (-> (fetch "posts/comments" test_member)
:body
(json/read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:data
:posts
first)]
Expand Down
4 changes: 2 additions & 2 deletions examples/acl/test/users_test.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns users-test
(:require
[acl-fixture]
[clojure.data.json :as json]
[clojure.test :refer [deftest is use-fixtures]]
[helpers :refer [delete
put
fetch
post
test_member
test_admin]]
[jsonista.core :as j]
[post-helpers]))

(use-fixtures :once acl-fixture/std-system-fixture)
Expand All @@ -17,7 +17,7 @@
[response]
(-> response
:body
(json/read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:data
:users))

Expand Down
36 changes: 18 additions & 18 deletions examples/jwt/test/integration_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns integration-test
(:require
[clj-http.client :refer [request]]
[clojure.data.json :as json]
[clojure.test :refer [deftest is use-fixtures]]
[jsonista.core :as j]
[jwt-fixture :refer [std-system-fixture]]))

(use-fixtures :once std-system-fixture)
Expand All @@ -19,18 +19,18 @@
(-> (request {:method :post
:url "http://localhost:3333/login"
:headers {"Content-Type" "application/json;charset=utf-8"}
:body (json/write-str {:email email
:password password})})
:body (j/write-value-as-string {:email email
:password password})})
:body
(json/read-str :key-fn keyword)
(j/read-value j/keyword-keys-object-mapper)
:auth-token))

(deftest unauthorized-secret
(let [response (request {:method :post
:unexceptional-status (constantly true)
:url "http://localhost:3333/secret"
:headers {"Content-Type" "application/json;charset=utf-8"}
:body (json/write-str {:hello "hello"})})]
:body (j/write-value-as-string {:hello "hello"})})]
(is (= 401 (:status response)))
(is (= "Signature could not be verified." (:body response)))))

Expand All @@ -39,23 +39,23 @@

(deftest authorized-secret
(let [auth-token (auth email password)
response (request {:method :post
:unexceptional-status (constantly true)
:url "http://localhost:3333/secret"
:headers (merge {"Content-Type" "application/json;charset=utf-8"}
(bearer auth-token))
:body (json/write-str {:hello "hello"})})]
response (request {:method :post
:unexceptional-status (constantly true)
:url "http://localhost:3333/secret"
:headers (merge {"Content-Type" "application/json;charset=utf-8"}
(bearer auth-token))
:body (j/write-value-as-string {:hello "hello"})})]
(is (= 200 (:status response)))
(is (= "Hello Xiana. request content: {:hello \"hello\"}" (:body response)))))

(deftest refresh-token
(let [auth-token (auth email password)
response (request {:method :post
:unexceptional-status (constantly true)
:url "http://localhost:3333/secret"
:headers (merge {"Content-Type" "application/json;charset=utf-8"}
(bearer auth-token))
:body (json/write-str {:hello "hello"})})
response (request {:method :post
:unexceptional-status (constantly true)
:url "http://localhost:3333/secret"
:headers (merge {"Content-Type" "application/json;charset=utf-8"}
(bearer auth-token))
:body (j/write-value-as-string {:hello "hello"})})
new-token (request {:method :get
:unexceptional-status (constantly true)
:url "http://localhost:3333/token"
Expand All @@ -66,6 +66,6 @@
(is (= 200 (:status new-token)))
(is (map? (xiana.jwt/verify-jwt
:no-claims
(-> new-token :body (json/read-str :key-fn keyword) :auth-token)
(-> new-token :body (j/read-value j/keyword-keys-object-mapper) :auth-token)
(get-in @jwt-fixture/test-system [:xiana/jwt :auth]))))))

6 changes: 3 additions & 3 deletions examples/sessions/src/backend/app/controllers/login.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns app.controllers.login
(:require
[clojure.data.json :as json]
[jsonista.core :as j]
[ring.util.request :refer [body-string]]
[xiana.session :as session]))

Expand All @@ -25,7 +25,7 @@
[{request :request :as state}]
(try (let [rbody (or (some-> request
body-string
(json/read-str :key-fn keyword))
(j/read-value j/keyword-keys-object-mapper))
(throw (ex-message "Missing body")))
user (find-user (:email rbody))
session-id (random-uuid)
Expand All @@ -38,7 +38,7 @@
:response {:status 200
:headers {"Content-Type" "application/json"
"Session-id" (str session-id)}
:body (json/write-str (update session-data :session-id str))}))
:body (j/write-value-as-string (update session-data :session-id str))}))
(assoc state :response {:status 401
:body "Incorrect credentials"})))
(catch Exception _ (missing-credentials state))))
6 changes: 3 additions & 3 deletions src/xiana/sse.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns xiana.sse
(:require
[clojure.core.async :as async :refer (<! go-loop)]
[clojure.data.json :as json]
[jsonista.core :as j]
[ring.adapter.jetty9 :as jetty]
[taoensso.timbre :as log])
(:import
Expand All @@ -15,7 +15,7 @@
(def EOL "\n")

(defn ->message [data]
(str "data: " (json/write-str data) EOL EOL))
(str "data: " (j/write-value-as-string data) EOL EOL))

(defn- clients->channels
[clients]
Expand Down Expand Up @@ -47,7 +47,7 @@
session-id (get-in state [:session-data :session-id])]
{:on-connect (fn [ch]
(swap! clients update session-id (fnil conj #{}) ch)
(jetty/send! ch {:headers headers :body (json/write-str {})}))
(jetty/send! ch {:headers headers :body (j/write-value-as-string {})}))
:on-text (fn [c m] (jetty/send! c m))
:on-close (fn [ch _status _reason] (swap! clients update session-id disj ch))}))

Expand Down
4 changes: 2 additions & 2 deletions src/xiana/websockets.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns xiana.websockets
(:require
[clojure.data.json :refer [read-str]]
[clojure.edn :as edn]
[clojure.string :as str]
[jsonista.core :as j]
[reitit.core :as r]
[ring.adapter.jetty9 :as jetty]
[taoensso.timbre :as log]
Expand All @@ -24,7 +24,7 @@
(defn json->
"JSON to 'uri', converts json string to map, extract :action key"
[j]
(:action (read-str j :key-fn keyword)))
(:action (j/read-value j j/keyword-keys-object-mapper)))

(defn probe->
[e]
Expand Down
4 changes: 2 additions & 2 deletions test/xiana/web_socket/router_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns xiana.web-socket.router-test
(:require
[clojure.data.json :as json]
[clojure.test :refer [deftest is]]
[jsonista.core :as j]
[reitit.core :as r]
[xiana.interceptor :as interceptors]
[xiana.websockets :refer [router]]))
Expand Down Expand Up @@ -29,7 +29,7 @@

(deftest router-test
(let [string-action "/log-string"
json-action (json/write-str {:action :log-json})
json-action (j/write-value-as-string {:action :log-json})
edn-action "{:action :log-edn}"]
(is (= "Log was called via string"
(-> (routing {:request-data
Expand Down

0 comments on commit 8145743

Please sign in to comment.