Skip to content

Commit

Permalink
Unify bb and clj behavior in cheshire and support :key-fn in read
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jan 30, 2024
1 parent 8fdc95b commit 6f281a2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
18 changes: 0 additions & 18 deletions src/babashka/json/internal/cheshire.bb

This file was deleted.

21 changes: 5 additions & 16 deletions src/babashka/json/internal/cheshire.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
(ns babashka.json.internal.cheshire
(:require [cheshire.core :as cheshire]
[cheshire.factory :as factory]
[cheshire.parse :as cheshire-parse])
(:refer-clojure :exclude [read])
(:import [com.fasterxml.jackson.core JsonFactory]
[java.io Reader]))
(:require [cheshire.core :as cheshire])
(:refer-clojure :exclude [read]))

(defn read-str
([s] (read-str s nil))
Expand All @@ -14,16 +10,9 @@
([s] (write-str s nil))
([s _opts] (cheshire/generate-string s)))

(defn ->json-reader
([rdr] (->json-reader rdr nil))
([rdr _opts]
(.createParser ^JsonFactory (or factory/*json-factory*
factory/json-factory)
^Reader rdr)))

(defn read
([reader] (read reader nil))
([reader _opts]
(cheshire-parse/parse-strict reader true nil nil)))
([reader {:keys [key-fn]}]
(cheshire/parse-stream reader (or key-fn keyword))))

(def fns ['cheshire/cheshire read-str write-str ->json-reader read])
(def fns ['cheshire/cheshire read-str write-str identity read])
8 changes: 5 additions & 3 deletions test/babashka/json_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
(is (= [1 2 3] (json/read-str (json/write-str [1 2 3]))))
(is (= {:a 1} (json/read-str (json/write-str {:a 1})))))
(testing "read json"
(let [rdr (json/->json-reader (java.io.StringReader. "{\"a\": 1} {\"b\": 2}"))]
(is (= {:a 1} (json/read rdr)))
#_(is (= {:b 2} (json/read rdr))))))
(let [rdr (json/->json-reader (java.io.StringReader. "{\"a\": 1}"))]
(is (= {:a 1} (json/read rdr))))
(testing "without keywords"
(let [rdr (json/->json-reader (java.io.StringReader. "{\"a\": 1}"))]
(is (= {"a" 1} (json/read rdr {:key-fn str})))))))

(deftest provider-test
(let [prop (some-> (System/getProperty "babashka.json.provider") not-empty symbol)]
Expand Down

0 comments on commit 6f281a2

Please sign in to comment.