Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions json/cheshire/clj_easy/tools/json/v0.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
(ns clj-easy.tools.json.v0
(:require [cheshire.core :as cheshire]))
(:require [cheshire.core :as cheshire])
(:import (com.fasterxml.jackson.core.io JsonEOFException)))

(defn read-str
([s] (read-str s nil))
([s {:keys [key-fn]}] (cheshire/parse-string s (or key-fn keyword))))
([s {:keys [key-fn]}]
(try (cheshire/parse-string s (or key-fn keyword))
(catch JsonEOFException e
(throw (ex-info "Incomplete input" {} e))))))

(defn write-str
([s] (write-str s nil))
Expand Down
7 changes: 5 additions & 2 deletions json/data-json/clj_easy/tools/json/v0.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

(defn read-str
([s] (read-str s nil))
([s {:keys [key-fn]}] (json/read-str s :key-fn (or key-fn keyword))))

([s {:keys [key-fn]}]
(try (json/read-str s :key-fn (or key-fn keyword))
(catch Exception e
(throw (ex-info "Incomplete input" {} e))))))

(defn write-str
([s] (write-str s nil))
([s _opts] (json/write-str s)))
5 changes: 4 additions & 1 deletion test/clj_easy/tools/json_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
(is (= [1 2 3] (json/read-str "[1, 2, 3]")))
(is (= {:a 1} (json/read-str "{\"a\": 1}")))
(is (= {:a 1} (json/read-str "{\"a\": 1}" {:key-fn keyword})))
(is (= {"a" 1} (json/read-str "{\"a\": 1}" {:key-fn str}))))
(is (= {"a" 1} (json/read-str "{\"a\": 1}" {:key-fn str})))
(is (thrown-with-msg? clojure.lang.ExceptionInfo
#"Incomplete input"
(json/read-str "{\"a\": 1" {:key-fn str}))))
(testing "write json"
(is (= [1 2 3] (json/read-str (json/write-str [1 2 3]))))
(is (= {:a 1} (json/read-str (json/write-str {:a 1}))))))