diff --git a/.github/workflows/doc-build.yml b/.github/workflows/doc-build.yml
index e379e5d..07265e0 100644
--- a/.github/workflows/doc-build.yml
+++ b/.github/workflows/doc-build.yml
@@ -1,5 +1,8 @@
name: Build API Docs
+permissions:
+ contents: write
+
on:
workflow_dispatch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index e2718bd..286cf95 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -1,5 +1,8 @@
name: Release on demand
+permissions:
+ contents: write
+
on:
workflow_dispatch:
inputs:
diff --git a/.github/workflows/snapshot.yml b/.github/workflows/snapshot.yml
index 2472957..9fdad8c 100644
--- a/.github/workflows/snapshot.yml
+++ b/.github/workflows/snapshot.yml
@@ -1,5 +1,8 @@
name: Snapshot on demand
+permissions:
+ contents: read
+
on: [workflow_dispatch]
jobs:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 1fa127c..2cc441a 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -1,5 +1,8 @@
name: Test
+permissions:
+ contents: read
+
on: [push]
jobs:
diff --git a/README.md b/README.md
index 2e9135d..653f3e2 100644
--- a/README.md
+++ b/README.md
@@ -13,23 +13,23 @@ Releases and Dependency Information
This project follows the version scheme MAJOR.MINOR.PATCH where each component provides some relative indication of the size of the change, but does not follow semantic versioning. In general, all changes endeavor to be non-breaking (by moving to new names rather than by breaking existing names).
-Latest stable release is [2.4.0]
+Latest stable release is [2.5.2]
[CLI/`deps.edn`](https://clojure.org/reference/deps_and_cli) dependency information:
```clojure
-org.clojure/data.json {:mvn/version "2.4.0"}
+org.clojure/data.json {:mvn/version "2.5.2"}
```
[Leiningen] dependency information:
- [org.clojure/data.json "2.4.0"]
+ [org.clojure/data.json "2.5.2"]
[Maven] dependency information:
org.clojure
data.json
- 2.4.0
+ 2.5.2
[Leiningen]: https://leiningen.org/
@@ -150,12 +150,21 @@ Developer Information
Change Log
----------------------------------------
-* Next
+* Release [2.5.2] on 2026-Jan-02
+ * Update to latest parent pom and Clojure 1.11.4
+ * Fix: [DJSON-56] During `read`, better error messages for chars < 32
+* Release [2.5.1] on 2024-Nov-26
+ * Fix: `read` of JSON number followed by EOF can break subsequent read on supplier PBR from seeing EOF
+ * Fix: `read` docstring updated to specify minimum buffer size when PushbackReader supplied (64)
+* Release [2.5.0] on 2023-Dec-21
* Fix [DJSON-50]: `read` can take a PushbackReader for repeated read use case
* Fix `write` docstring to add `:indent` option added in [DJSON-18]
+ * Fix [DJSON-57]: Throw better exception when EOF encountered while reading array or object
* 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
@@ -191,6 +200,7 @@ Change Log
* Perf [DJSON-35]: Replace PrintWriter with more generic Appendable, reduce wrapping
* Perf [DJSON-34]: More efficient writing for common path
* Perf [DJSON-32]: Use option map instead of dynamic variables (affects read+write)
+ * NOTE: Includes a breaking change in the internal JSONWriter protocol method signature
* Perf [DJSON-33]: Improve speed of reading JSON strings
* Fix [DJSON-30]: Fix bad test
* Release [1.1.0] on 2021-Mar-5
@@ -244,6 +254,8 @@ Change Log
* Source-compatible with clojure.contrib.json, except for the name change.
[DJSON-61]: https://clojure.atlassian.net/browse/DJSON-61
+[DJSON-57]: https://clojure.atlassian.net/browse/DJSON-57
+[DJSON-56]: https://clojure.atlassian.net/browse/DJSON-56
[DJSON-54]: https://clojure.atlassian.net/browse/DJSON-54
[DJSON-53]: https://clojure.atlassian.net/browse/DJSON-53
[DJSON-52]: https://clojure.atlassian.net/browse/DJSON-52
@@ -251,6 +263,7 @@ Change Log
[DJSON-50]: https://clojure.atlassian.net/browse/DJSON-50
[DJSON-48]: https://clojure.atlassian.net/browse/DJSON-48
[DJSON-47]: https://clojure.atlassian.net/browse/DJSON-47
+[DJSON-46]: https://clojure.atlassian.net/browse/DJSON-46
[DJSON-45]: https://clojure.atlassian.net/browse/DJSON-45
[DJSON-43]: https://clojure.atlassian.net/browse/DJSON-43
[DJSON-41]: https://clojure.atlassian.net/browse/DJSON-41
@@ -277,14 +290,17 @@ Change Log
[DJSON-7]: https://clojure.atlassian.net/browse/DJSON-7
[DJSON-1]: https://clojure.atlassian.net/browse/DJSON-1
-[2.4.0]: https://github.com/clojure/data.json/tree/data.json-2.4.0
-[2.3.1]: https://github.com/clojure/data.json/tree/data.json-2.3.1
-[2.3.0]: https://github.com/clojure/data.json/tree/data.json-2.3.0
-[2.2.3]: https://github.com/clojure/data.json/tree/data.json-2.2.3
-[2.2.2]: https://github.com/clojure/data.json/tree/data.json-2.2.2
-[2.2.1]: https://github.com/clojure/data.json/tree/data.json-2.2.1
-[2.2.0]: https://github.com/clojure/data.json/tree/data.json-2.2.0
-[2.1.1]: https://github.com/clojure/data.json/tree/data.json-2.1.1
+[2.5.2]: https://github.com/clojure/data.json/tree/v2.5.2
+[2.5.1]: https://github.com/clojure/data.json/tree/v2.5.1
+[2.5.0]: https://github.com/clojure/data.json/tree/v2.5.0
+[2.4.0]: https://github.com/clojure/data.json/tree/v2.4.0
+[2.3.1]: https://github.com/clojure/data.json/tree/v2.3.1
+[2.3.0]: https://github.com/clojure/data.json/tree/v2.3.0
+[2.2.3]: https://github.com/clojure/data.json/tree/v2.2.3
+[2.2.2]: https://github.com/clojure/data.json/tree/v2.2.2
+[2.2.1]: https://github.com/clojure/data.json/tree/v2.2.1
+[2.2.0]: https://github.com/clojure/data.json/tree/v2.2.0
+[2.1.1]: https://github.com/clojure/data.json/tree/v2.1.1
[2.1.0]: https://github.com/clojure/data.json/tree/data.json-2.1.0
[2.0.2]: https://github.com/clojure/data.json/tree/data.json-2.0.2
[2.0.1]: https://github.com/clojure/data.json/tree/data.json-2.0.1
diff --git a/deps.edn b/deps.edn
index eff4f54..1fafc86 100644
--- a/deps.edn
+++ b/deps.edn
@@ -1,2 +1,2 @@
{:paths ["src/main/clojure"]
- :deps {org.clojure/clojure {:mvn/version "1.9.0"}}}
+ :deps {org.clojure/clojure {:mvn/version "1.11.4"}}}
diff --git a/pom.xml b/pom.xml
index 3cdaccf..7af80aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
4.0.0
data.json
- 2.4.1-SNAPSHOT
+ 2.5.3-SNAPSHOT
data.json
Generating/parsing JSON from/to Clojure data structures
https://github.com/clojure/data.json
@@ -19,7 +19,7 @@
org.clojure
pom.contrib
- 1.1.0
+ 1.4.0
@@ -30,14 +30,14 @@
- 1.9.0
+ 1.11.4
org.clojure
test.check
- 1.1.0
+ 1.1.3
test
@@ -71,7 +71,7 @@
maven-jar-plugin
- 3.0.2
+ 3.5.0
default-jar
@@ -85,11 +85,24 @@
+
+ javadoc-jar
+ package
+
+ jar
+
+
+ javadoc
+
+ **/*.html
+
+
+
maven-assembly-plugin
- 3.0.0
+ 3.8.0
aot-jar
diff --git a/project.clj b/project.clj
index b367048..17f63e4 100644
--- a/project.clj
+++ b/project.clj
@@ -1,20 +1,19 @@
;; NOTE: Used only for perf testing - this project is built with Maven (see pom.xml)
(defproject clojure.data.json "1.1.1-SNAPSHOT"
- :dependencies [[org.clojure/clojure "1.10.3"]]
+ :dependencies [[org.clojure/clojure "1.12.4"]]
:source-paths ["src/main/clojure"]
:java-source-paths ["src/main/java"]
:java-test-paths ["src/test/java"]
:test-paths ["src/test/clojure" "src/test/clojure-perf"]
- :profiles {:dev {:dependencies [[com.clojure-goes-fast/clj-async-profiler "0.5.0"]
- [com.clojure-goes-fast/clj-java-decompiler "0.3.0"]
- [org.clojure/test.check "1.1.0"]
+ :profiles {:dev {:dependencies [[com.clojure-goes-fast/clj-async-profiler "1.6.2"]
+ [com.clojure-goes-fast/clj-java-decompiler "0.3.7"]
+ [org.clojure/test.check "1.1.3"]
[criterium/criterium "0.4.6"]
- [metosin/jsonista "0.3.1"]
- [cheshire/cheshire "5.10.0"]
- [org.openjdk.jmh/jmh-core "1.28"]
- [jmh-clojure "0.4.0"]
+ [metosin/jsonista "0.3.13"]
+ [cheshire/cheshire "6.1.0"]
+ [org.openjdk.jmh/jmh-core "1.37"]
+ [jmh-clojure "0.4.1"]
[com.jsoniter/jsoniter "0.9.23"]]
:resource-paths ["dev-resources"]
:global-vars {*warn-on-reflection* true}}}
- ;;:plugins [[lein-nodisassemble "0.1.3"]]
:jvm-opts ["-Djdk.attach.allowAttachSelf=true"])
diff --git a/src/main/clojure/clojure/data/json.clj b/src/main/clojure/clojure/data/json.clj
index 22b454b..8afcd14 100644
--- a/src/main/clojure/clojure/data/json.clj
+++ b/src/main/clojure/clojure/data/json.clj
@@ -33,6 +33,7 @@
(readChars [_ buffer start bufflen]
(.read rdr ^chars buffer start bufflen))
(unreadChar [_ c]
+ ;; ASSERT: c should never be -1 (EOF)
(.unread rdr c))
(unreadChars [_ buffer start bufflen]
(.unread rdr buffer start bufflen))
@@ -63,6 +64,7 @@
(.getChars ^String s p end ^chars buffer start)))
(if (pos? n) n -1)))
(unreadChar [_ _c]
+ ;; ASSERT: c should never be -1 (EOF)
(set! pos (unchecked-dec pos))
nil)
(unreadChars [_ _buffer _start bufflen]
@@ -234,9 +236,11 @@
:whitespace
(do (.unreadChar stream c)
false)
- (\, \] \} -1)
+ (\, \] \})
(do (.unreadChar stream c)
false)
+ -1
+ false
(throw (Exception. "JSON error (invalid number literal)")))
;; previous character is a "0"
:frac-point
@@ -251,9 +255,11 @@
:whitespace
(do (.unreadChar stream c)
false)
- (\, \] \} -1)
+ (\, \] \})
(do (.unreadChar stream c)
false)
+ -1
+ false
;; Disallow zero-padded numbers or invalid characters
(throw (Exception. "JSON error (invalid number literal)")))
;; previous character is a "."
@@ -276,9 +282,11 @@
:whitespace
(do (.unreadChar stream c)
true)
- (\, \] \} -1)
+ (\, \] \})
(do (.unreadChar stream c)
true)
+ -1
+ true
(throw (Exception. "JSON error (invalid number literal)")))
;; previous character is a "e" or "E"
:exp-symbol
@@ -306,9 +314,11 @@
:whitespace
(do (.unreadChar stream c)
true)
- (\, \] \} -1)
+ (\, \] \})
(do (.unreadChar stream c)
true)
+ -1
+ true
(throw (Exception. "JSON error (invalid number literal)"))))))]
(if decimal?
(read-decimal (str buffer) bigdec?)
@@ -320,11 +330,14 @@
(int c)
(codepoint-case (int c)
:whitespace (recur (.readChar stream))
- -1 -1))))
+ c))))
(defn invalid-array-exception []
(Exception. "JSON error (invalid array)"))
+(defn- eof-array-exception []
+ (EOFException. "JSON error (EOF in array)"))
+
(defn- read-array* [^InternalPBR stream options]
;; Handles all array values after the first.
(loop [result (transient [])]
@@ -332,6 +345,7 @@
(codepoint-case (int (next-token stream))
\] (persistent! r)
\, (recur r)
+ -1 (throw (eof-array-exception))
(throw (invalid-array-exception))))))
(defn- read-array [^InternalPBR stream options]
@@ -342,19 +356,35 @@
(codepoint-case c
\] []
\, (throw (invalid-array-exception))
+ -1 (throw (eof-array-exception))
(do (.unreadChar stream c)
(read-array* stream options)))))
+(defn- object-colon-exception []
+ (Exception. "JSON error (missing `:` in object)"))
+
+(defn- eof-object-exception []
+ (EOFException. "JSON error (EOF in object)"))
+
+(defn- invalid-key-exception [c]
+ (if (= c -1)
+ (throw (eof-object-exception))
+ (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`")))))
+
+(comment
+ (compile 'clojure.data.json)
+ )
+
(defn- read-key [^InternalPBR stream]
(let [c (int (next-token stream))]
(if (= c (codepoint \"))
(let [key (read-quoted-string stream)]
(if (= (codepoint \:) (int (next-token stream)))
key
- (throw (Exception. "JSON error (missing `:` in object)"))))
+ (throw (object-colon-exception))))
(if (= c (codepoint \}))
nil
- (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`")))))))
+ (invalid-key-exception c)))))
(defn- read-object [^InternalPBR stream options]
;; Expects to be called with the head of the stream AFTER the
@@ -374,6 +404,7 @@
(codepoint-case (int (next-token stream))
\, (recur r)
\} (persistent! r)
+ -1 (throw (eof-object-exception))
(throw (Exception. "JSON error (missing entry in object)"))))
(let [r (persistent! result)]
(if (empty? r)
@@ -462,7 +493,8 @@
"Reads a single item of JSON data from a java.io.Reader.
If you wish to repeatedly read items from the same reader, you must
- supply a PushbackReader and reuse it on subsequent calls.
+ supply a PushbackReader with buffer size >= 64, and reuse it on
+ subsequent calls.
Options are key-value pairs, valid options are:
@@ -562,14 +594,17 @@
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")
@@ -577,10 +612,10 @@
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)))))))))
diff --git a/src/test/clojure/clojure/data/json_test.clj b/src/test/clojure/clojure/data/json_test.clj
index 8eb0354..aa6d79c 100644
--- a/src/test/clojure/clojure/data/json_test.clj
+++ b/src/test/clojure/clojure/data/json_test.clj
@@ -3,24 +3,48 @@
[clojure.test :refer :all]
[clojure.string :as str]))
+(defn pbr
+ ([s]
+ (pbr s 64))
+ ([s size]
+ (if (< size 64)
+ (throw (RuntimeException. "Size must be >= 64"))
+ (java.io.PushbackReader. (java.io.StringReader. s) size))))
+
(deftest read-from-pushback-reader
- (let [s (java.io.PushbackReader. (java.io.StringReader. "42"))]
- (is (= 42 (json/read s)))))
+ (is (= 42 (json/read (pbr "42"))))
+ (is (= ["abc" "def"] (json/read (pbr "[\"abc\", \"def\"]")))))
-;; DJSON-50 - pass PBR to safely do reapeated read
+;; DJSON-50 - pass PBR to safely do repeated read
(deftest read-multiple
(let [st "{\"foo\":\"some string\"}{\"foo\":\"another string\"}"
- srdr (java.io.StringReader. st)
- pbr (java.io.PushbackReader. srdr 64)]
+ pbr (pbr st)]
(is (= {"foo" "some string"} (json/read pbr)))
(is (= {"foo" "another string"} (json/read pbr))))
(let [st "{\"foo\":\"some string\"}{\"foo\":\"another long ......................................................... string\"}"
- srdr (java.io.StringReader. st)
- pbr (java.io.PushbackReader. srdr 64)]
+ pbr (pbr st)]
(is (= {"foo" "some string"} (json/read pbr)))
(is (= {"foo" "another long ......................................................... string"} (json/read pbr)))))
+(defn read-then-eof [s]
+ (let [r (pbr s)
+ val (json/read r :eof-error? false :eof-value :EOF)]
+ (is (= :EOF (json/read r :eof-error? false :eof-value :EOF)))
+ val))
+
+(deftest read-multiple-eof
+ (are [expected s] (= expected (read-then-eof s))
+ 1.2 "1.2"
+ 0 "0"
+ 1 "1"
+ 1.0 "1.0"
+ "abc" "\"abc\""
+ "\u2202" "\"\u2202\""
+ [] "[]"
+ [1 2] "[1, 2]")
+ )
+
(deftest read-from-reader
(let [s (java.io.StringReader. "42")]
(is (= 42 (json/read s)))))
@@ -407,6 +431,18 @@
(is (thrown? java.io.EOFException
(json/read-str "\"\\"))))
+(deftest throws-eof-in-arrays
+ (is (thrown? java.io.EOFException
+ (json/read-str "[1,")))
+ (is (thrown? java.io.EOFException
+ (json/read-str "[1,2,"))))
+
+(deftest throws-eof-in-objects
+ (is (thrown? java.io.EOFException
+ (json/read-str "{")))
+ (is (thrown? java.io.EOFException
+ (json/read-str "{\"\":1,"))))
+
(deftest accept-eof
(is (= ::eof (json/read-str "" :eof-error? false :eof-value ::eof))))