Skip to content

Commit

Permalink
faster slow-write-string
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Dec 21, 2023
1 parent cc154d5 commit d694575
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ Change Log
* Add [DJSON-46]: In `read`, add `:extra-data-fn` that can be provided to cause an eof check after value is read
* Add [DJSON-54]: In `write`, add custom fallback fn for writing unknown types
* Perf [DJSON-61]: Faster string writing when string is "simple"
* Perf: Faster string writing when string is not simple
* Perf: Faster `read-str`
* Release [2.4.0] on 2021-Jul-12
* Fix [DJSON-52]: Remove Classloader workaround to support Clojure 1.2.x and below
* Fix [DJSON-53]: Move deprecated API functions from compat ns into main ns
Expand Down
11 changes: 7 additions & 4 deletions src/main/clojure/clojure/data/json.clj
Original file line number Diff line number Diff line change
Expand Up @@ -562,25 +562,28 @@
shorts))

(defn- slow-write-string [^CharSequence s ^Appendable out options]
(let [decoder codepoint-decoder]
(let [decoder codepoint-decoder
slash (get options :escape-slash)
escape-js-separators (get options :escape-js-separators)
escape-unicode (get options :escape-unicode)]
(dotimes [i (.length s)]
(let [cp (int (.charAt s i))]
(if (< cp 128)
(case (aget decoder cp)
0 (.append out (char cp))
1 (do (.append out (char (codepoint \\))) (.append out (char cp)))
2 (.append out (if (get options :escape-slash) "\\/" "/"))
2 (.append out (if slash "\\/" "/"))
3 (.append out "\\b")
4 (.append out "\\f")
5 (.append out "\\n")
6 (.append out "\\r")
7 (.append out "\\t")
8 (->hex-string out cp))
(codepoint-case cp
:js-separators (if (get options :escape-js-separators)
:js-separators (if escape-js-separators
(->hex-string out cp)
(.append out (char cp)))
(if (get options :escape-unicode)
(if escape-unicode
(->hex-string out cp) ; Hexadecimal-escaped
(.append out (char cp)))))))))

Expand Down

0 comments on commit d694575

Please sign in to comment.