Skip to content

Commit dab0edb

Browse files
authored
Merge pull request #48 from BuddhiLW/t300/cljw-json-delegate
t300/cljw-json-delegate
2 parents 3ee4ee5 + 637b375 commit dab0edb

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎src/lang/clj/cljw/json.clj‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@
88
[clojure.walk]))
99

1010
;; Re-exports (so `(require '[cljw.json :as json])` gives the data.json surface).
11-
(def write-str clojure.data.json/write-str)
12-
(def read-str clojure.data.json/read-str)
11+
;; Delegating defns, not `(def write-str clojure.data.json/write-str)`: a
12+
;; def-alias captures the fn VALUE at load time, so a later redefinition or a
13+
;; `with-redefs` of the data.json var would not reach these, and the alias var
14+
;; would carry no :doc / :arglists.
15+
(defn write-str
16+
"Converts x to a JSON-formatted string. Delegates to
17+
clojure.data.json/write-str at call time; options are its key-value pairs
18+
(:key-fn, :value-fn, :escape-unicode, :escape-slash,
19+
:escape-js-separators)."
20+
[x & options]
21+
(apply clojure.data.json/write-str x options))
22+
23+
(defn read-str
24+
"Reads one JSON value from the string s. Delegates to
25+
clojure.data.json/read-str at call time; options are its key-value pairs."
26+
[s & options]
27+
(apply clojure.data.json/read-str s options))
1328

1429
(defn encode
1530
"A cljw value -> a JSON string. (Alias of write-str, the natural map->JSON

‎test/e2e/phase16_cljw_json_fs.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ assert_eq 'json_decode_kw' "$(run '(cljw.json/decode "{\"name\":\"x\",\"nested\"
3030
assert_eq 'json_decode_get' "$(run '(:code (cljw.json/decode "{\"code\":\"(+ 1 2)\"}"))')" '"(+ 1 2)"'
3131
assert_eq 'json_decode_strict' "$(run '(get (cljw.json/decode-strict "{\"a\":1}") "a")')" '1'
3232
assert_eq 'json_roundtrip' "$(run '(cljw.json/decode (cljw.json/encode {:x [1 2 3]}))')" '{:x [1 2 3]}'
33+
# write-str / read-str delegate at call time (not a def-alias): with-redefs of
34+
# the data.json var reaches them, options pass through, and the vars document.
35+
assert_eq 'json_write_str_redefs' "$(run '(with-redefs [clojure.data.json/write-str (fn [& _] "X")] (cljw.json/write-str {:a 1}))')" '"X"'
36+
assert_eq 'json_read_str_opts' "$(run '(cljw.json/read-str "{\"a\":1}" :key-fn keyword)')" '{:a 1}'
37+
assert_eq 'json_write_str_arglists' "$(run '(:arglists (meta (var cljw.json/write-str)))')" '([x & options])'
38+
assert_eq 'json_read_str_doc' "$(run '(string? (:doc (meta (var cljw.json/read-str))))')" 'true'
3339

3440
# --- cljw.fs ---
3541
assert_eq 'fs_exists' "$(run "(cljw.fs/exists? \"$TMP/ten.txt\")")" 'true'

0 commit comments

Comments
 (0)