From 814574303561075e6f0ce24a1d12af221024915f Mon Sep 17 00:00:00 2001 From: Theodor Meresescu <48867560+theodor-meresescu@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:52:19 +0100 Subject: [PATCH] Leave only one json library in deps (jsonista or data.json) (#279) * 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 --- deps.edn | 4 +-- examples/acl/test/comments_test.clj | 6 ++-- examples/acl/test/helpers.clj | 8 ++--- examples/acl/test/post_helpers.clj | 8 ++--- examples/acl/test/posts_test.clj | 6 ++-- examples/acl/test/users_test.clj | 4 +-- examples/jwt/test/integration_test.clj | 36 +++++++++---------- .../src/backend/app/controllers/login.clj | 6 ++-- src/xiana/sse.clj | 6 ++-- src/xiana/websockets.clj | 4 +-- test/xiana/web_socket/router_test.clj | 4 +-- 11 files changed, 46 insertions(+), 46 deletions(-) diff --git a/deps.edn b/deps.edn index 58dcb4ae..ea703fe5 100644 --- a/deps.edn +++ b/deps.edn @@ -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"} @@ -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 diff --git a/examples/acl/test/comments_test.clj b/examples/acl/test/comments_test.clj index 2ed8a694..bded1c42 100644 --- a/examples/acl/test/comments_test.clj +++ b/examples/acl/test/comments_test.clj @@ -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]])) @@ -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) @@ -36,7 +36,7 @@ [response] (-> response :body - (read-str :key-fn keyword) + (j/read-value j/keyword-keys-object-mapper) :data :comments)) diff --git a/examples/acl/test/helpers.clj b/examples/acl/test/helpers.clj index 41b49d48..b6cfb172 100644 --- a/examples/acl/test/helpers.clj +++ b/examples/acl/test/helpers.clj @@ -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") @@ -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] @@ -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)})) diff --git a/examples/acl/test/post_helpers.clj b/examples/acl/test/post_helpers.clj index 8fd10b51..fe66b651 100644 --- a/examples/acl/test/post_helpers.clj +++ b/examples/acl/test/post_helpers.clj @@ -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 [] @@ -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))) @@ -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)) diff --git a/examples/acl/test/posts_test.clj b/examples/acl/test/posts_test.clj index a95a66d7..2cd9eb1d 100644 --- a/examples/acl/test/posts_test.clj +++ b/examples/acl/test/posts_test.clj @@ -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 @@ -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 @@ -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 @@ -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)] diff --git a/examples/acl/test/users_test.clj b/examples/acl/test/users_test.clj index 9afe61a8..74ed3ded 100644 --- a/examples/acl/test/users_test.clj +++ b/examples/acl/test/users_test.clj @@ -1,7 +1,6 @@ (ns users-test (:require [acl-fixture] - [clojure.data.json :as json] [clojure.test :refer [deftest is use-fixtures]] [helpers :refer [delete put @@ -9,6 +8,7 @@ post test_member test_admin]] + [jsonista.core :as j] [post-helpers])) (use-fixtures :once acl-fixture/std-system-fixture) @@ -17,7 +17,7 @@ [response] (-> response :body - (json/read-str :key-fn keyword) + (j/read-value j/keyword-keys-object-mapper) :data :users)) diff --git a/examples/jwt/test/integration_test.clj b/examples/jwt/test/integration_test.clj index 6afca459..cdfd2cf7 100644 --- a/examples/jwt/test/integration_test.clj +++ b/examples/jwt/test/integration_test.clj @@ -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) @@ -19,10 +19,10 @@ (-> (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 @@ -30,7 +30,7 @@ :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))))) @@ -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" @@ -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])))))) diff --git a/examples/sessions/src/backend/app/controllers/login.clj b/examples/sessions/src/backend/app/controllers/login.clj index 4778f798..08537e6c 100644 --- a/examples/sessions/src/backend/app/controllers/login.clj +++ b/examples/sessions/src/backend/app/controllers/login.clj @@ -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])) @@ -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) @@ -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)))) diff --git a/src/xiana/sse.clj b/src/xiana/sse.clj index 722e7fc6..562586f3 100644 --- a/src/xiana/sse.clj +++ b/src/xiana/sse.clj @@ -1,7 +1,7 @@ (ns xiana.sse (:require [clojure.core.async :as async :refer (message [data] - (str "data: " (json/write-str data) EOL EOL)) + (str "data: " (j/write-value-as-string data) EOL EOL)) (defn- clients->channels [clients] @@ -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))})) diff --git a/src/xiana/websockets.clj b/src/xiana/websockets.clj index e4c07b5e..afec8408 100644 --- a/src/xiana/websockets.clj +++ b/src/xiana/websockets.clj @@ -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] @@ -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] diff --git a/test/xiana/web_socket/router_test.clj b/test/xiana/web_socket/router_test.clj index 4a020313..84d7a508 100644 --- a/test/xiana/web_socket/router_test.clj +++ b/test/xiana/web_socket/router_test.clj @@ -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]])) @@ -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