Skip to content

Commit

Permalink
Merge branch 'pr/139'
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Feb 8, 2019
2 parents 3157068 + e19bc5a commit c843836
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/cheshire/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@
^Reader rdr)
key-fn nil array-coerce-fn))))

(defn parse-stream-strict
"Returns the Clojure object corresponding to the given reader, reader must
implement BufferedReader. An optional key-fn argument can be either true (to
coerce keys to keywords),false to leave them as strings, or a function to
provide custom coercion.
The array-coerce-fn is an optional function taking the name of an array field,
and returning the collection to be used for array values.
Does not lazily parse top-level arrays."
([rdr] (parse-stream-strict rdr nil nil))
([rdr key-fn] (parse-stream-strict rdr key-fn nil))
([^BufferedReader rdr key-fn array-coerce-fn]
(when rdr
(parse/parse-strict
(.createParser ^JsonFactory (or factory/*json-factory*
factory/json-factory)
^Reader rdr)
key-fn nil array-coerce-fn))))

(defn parse-smile
"Returns the Clojure object corresponding to the given SMILE-encoded bytes.
An optional key-fn argument can be either true (to coerce keys to keywords),
Expand Down
27 changes: 17 additions & 10 deletions test/cheshire/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,23 @@
(is (= "{\"foo\":\"a\"}" (json/encode {:foo \a}))))

(deftest test-streams
(is (= {"foo" "bar"}
(json/parse-stream
(BufferedReader. (StringReader. "{\"foo\":\"bar\"}\n")))))
(let [sw (StringWriter.)
bw (BufferedWriter. sw)]
(json/generate-stream {"foo" "bar"} bw)
(is (= "{\"foo\":\"bar\"}" (.toString sw))))
(is (= {(keyword "foo baz") "bar"}
(with-open [rdr (StringReader. "{\"foo baz\":\"bar\"}\n")]
(json/parse-stream rdr true)))))
(testing "parse-stream"
(are [parsed parse parsee] (= parsed
(parse (BufferedReader. (StringReader. parsee))))
{"foo" "bar"} json/parse-stream "{\"foo\":\"bar\"}\n"
{"foo" "bar"} json/parse-stream-strict "{\"foo\":\"bar\"}\n")

(are [parsed parse parsee] (= parsed
(with-open [rdr (StringReader. parsee)]
(parse rdr true)))
{(keyword "foo baz") "bar"} json/parse-stream "{\"foo baz\":\"bar\"}\n"
{(keyword "foo baz") "bar"} json/parse-stream-strict "{\"foo baz\":\"bar\"}\n"))

(testing "generate-stream"
(let [sw (StringWriter.)
bw (BufferedWriter. sw)]
(json/generate-stream {"foo" "bar"} bw)
(is (= "{\"foo\":\"bar\"}" (.toString sw))))))

(deftest serial-writing
(is (= "[\"foo\",\"bar\"]"
Expand Down

0 comments on commit c843836

Please sign in to comment.