Move SSD JSON text parsing to typed Kotlin

ober

d90146248b0d9d981bcc42399ad02206aad4d4e5

diff --git a/.build.yml b/.build.yml
index dd3e6ae..9dc5a6a 100644
--- a/.build.yml
+++ b/.build.yml
@@ -5,7 +5,7 @@ packages:
   - make=4.4.1-r4
 sources:
   # Build dependency: full immutable commit, mirrored in dependencies.lock.json.
-  - "https://git.sr.ht/~lisp/jerboa#a56576481905e9152869948b3fd80f9274e8a5d3"
+  - "https://git.sr.ht/~lisp/jerboa#fccc76e702602c0538c28935ef7b5e228947bded"
   # The second source is the build subject selected by the SourceHut submitter.
   - https://git.sr.ht/~lisp/jerboa-android
 tasks:
@@ -14,6 +14,6 @@ tasks:
       test "$(apk info -v chez-scheme)" = chez-scheme-10.3.0-r2
       test "$(apk info -v git)" = git-2.54.0-r0
       test "$(apk info -v make)" = make-4.4.1-r4
-      test "$(git -C ../jerboa rev-parse HEAD)" = a56576481905e9152869948b3fd80f9274e8a5d3
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 2e6a1b52a9d086817471e35e4b45f64c43f2862c
+      test "$(git -C ../jerboa rev-parse HEAD)" = fccc76e702602c0538c28935ef7b5e228947bded
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 041beddb9852658fbd7865cc1073272583513867
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 2ecaa1e..1e6fd49 100644
--- a/dependencies.lock.json
+++ b/dependencies.lock.json
@@ -11,8 +11,8 @@
   "generator_runtime": {
     "name": "jerboa",
     "repository": "https://git.sr.ht/~lisp/jerboa",
-    "commit": "a56576481905e9152869948b3fd80f9274e8a5d3",
-    "tree": "2e6a1b52a9d086817471e35e4b45f64c43f2862c"
+    "commit": "fccc76e702602c0538c28935ef7b5e228947bded",
+    "tree": "041beddb9852658fbd7865cc1073272583513867"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 21668b4..200f26f 100755
--- a/scripts/verify-supply-chain.sh
+++ b/scripts/verify-supply-chain.sh
@@ -3,8 +3,8 @@ set -eu
 
 repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
 lock="$repo/dependencies.lock.json"
-jerboa_commit=a56576481905e9152869948b3fd80f9274e8a5d3 # gitsafe:ignore
-jerboa_tree=2e6a1b52a9d086817471e35e4b45f64c43f2862c # gitsafe:ignore
+jerboa_commit=fccc76e702602c0538c28935ef7b5e228947bded # gitsafe:ignore
+jerboa_tree=041beddb9852658fbd7865cc1073272583513867 # gitsafe:ignore
 gradle_sha=20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78
 jdk_macos_sha=8fa1eff40bb637a33613b2ccb8b12c70dc3661cc22cf8e784943715769a05336
 jdk_linux_sha=d8afc263758141a66e0e3aafc321e783f7016696f4eaea067d340a269037d331
diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 710fbb1..fd0c9ec 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -1593,7 +1593,8 @@
     (typed-kotlin-file "com/sfb/ssdreview/TruthJson.kt"
       (kotlin-imports (org json JSONArray) (org json JSONObject))
       (typed-library (com sfb ssdreview)
-        (export truthJsonArrayOrEmpty truthJsonObjectOrEmpty
+        (export jsonObjectFromText
+                truthJsonArrayOrEmpty truthJsonObjectOrEmpty
                 truthGroupCount nullableTruthGroupCount truthSsdAreaRect
                 truthSourceKey truthSessionIdOr
                 imageWidth imageHeight)
@@ -1601,6 +1602,8 @@
         (type JSONObject)
         (type FloatArray)
         (type Int32)
+        (def (jsonObjectFromText (text : String)) : JSONObject
+          (json-object-from-string text))
         (def (truthJsonArrayOrEmpty (raw : (Nullable JSONArray))) : JSONArray
           (if (nullable-null? raw)
               (json-array-empty)
@@ -5973,7 +5976,7 @@
        "            loadError = error"
        "            return null"
        "        }"
-       "        val root = try { JSONObject(jsonText) } catch (_: Exception) { return null }"
+       "        val root = try { jsonObjectFromText(jsonText) } catch (_: Exception) { return null }"
        "        if (!nativeDetectionRootOk(root)) {"
        "            loadError = IllegalStateException(nativeDetectionRootError(root))"
        "            return null"
@@ -6585,7 +6588,7 @@
        "    fun loadTruth(sourceKey: String): JSONObject? {"
        "        val file = truthPath(sourceKey)"
        "        if (!file.exists()) return null"
-       "        return JSONObject(readLocalText(file))"
+       "        return jsonObjectFromText(readLocalText(file))"
        "    }"
        ""
        "    fun loadTruth(session: SsdSession): JSONObject? {"
@@ -6594,7 +6597,7 @@
        "            truthIndexEntry(truthPath(session.sourceKey)),"
        "            session"
        "        )"
-       "        return best?.let { JSONObject(readLocalText(it._file)) }"
+       "        return best?.let { jsonObjectFromText(readLocalText(it._file)) }"
        "    }"
        ""
        "    @Synchronized"
@@ -6616,7 +6619,7 @@
        "    private fun truthIndexEntry(file: File): TruthIndexEntry? {"
        "        if (!file.exists() || !file.isFile) return null"
        "        return try {"
-       "            val truth = JSONObject(readLocalText(file))"
+       "            val truth = jsonObjectFromText(readLocalText(file))"
        "            truthIndexEntryFromJson(file, truth, truthTime(truth) ?: file.lastModified())"
        "        } catch (_: Exception) {"
        "            null"
@@ -6893,7 +6896,7 @@
        "                return 0"
        "            }"
        "            val response = readResponseText(connection)"
-       "            val payload = JSONObject(response)"
+       "            val payload = jsonObjectFromText(response)"
        "            val compactTruth = remoteTruthPayloadTruth(payload)"
        "            if (compactTruth != null) {"
        "                val modified = remoteTruthPayloadMatchModified(payload)"
@@ -6956,7 +6959,7 @@
        "        val dest = truthPath(key)"
        "        val text = truth.toString(2)"
        "        val shouldWrite = if (replaceIfMoreComplete && dest.exists()) {"
-       "            val local = JSONObject(readLocalText(dest))"
+       "            val local = jsonObjectFromText(readLocalText(dest))"
        "            truthGroupCount(truth) >= truthGroupCount(local) || shouldReplaceLocal(dest, text, modified)"
        "        } else {"
        "            shouldReplaceLocal(dest, text, modified)"
@@ -6971,7 +6974,7 @@
        "        val file = File(learnedDir, \"group_examples.json\")"
        "        if (!file.exists()) rebuildLearnedExamples()"
        "        if (!file.exists()) return 0"
-       "        val examples = learnedExamplesFromDocument(JSONObject(readLocalText(file)))"
+       "        val examples = learnedExamplesFromDocument(jsonObjectFromText(readLocalText(file)))"
        "        return applyLearnedGuessesFromExamples(session, examples)"
        "    }"
        ""
@@ -7099,10 +7102,10 @@
        "            text.lineSequence().map { it.trim() }.filter { it.isNotBlank() }.forEach { line ->"
        "                lines += 1"
        "                if (lines > MAX_JSONL_LINES) throw IllegalStateException(\"JSONL line-count limit exceeded\")"
-       "                JSONObject(line)"
+       "                jsonObjectFromText(line)"
        "            }"
        "        } else {"
-       "            JSONObject(text)"
+       "            jsonObjectFromText(text)"
        "        }"
        "    }"
        ""
@@ -7153,7 +7156,7 @@
        "                throw IllegalStateException(\"HTTP $code\")"
        "            }"
        "                val response = readResponseText(connection)"
-       "                return remoteDumpImportedCount(JSONObject(response))"
+       "                return remoteDumpImportedCount(jsonObjectFromText(response))"
        "            } finally {"
        "                connection.disconnect()"
        "            }"
@@ -7184,7 +7187,7 @@
        "            ?: emptyList()"
        "        fileLoop@ for (file in files) {"
        "            val truth = try {"
-       "                JSONObject(readLocalText(file))"
+       "                jsonObjectFromText(readLocalText(file))"
        "            } catch (_: Exception) {"
        "                continue"
        "            }"
@@ -7336,7 +7339,7 @@
        "        jsonValueTime(truth.opt(\"generated_at\") ?: truth.opt(\"created_at\"))"
        ""
        "    private fun jsonTime(text: String): Long? = try {"
-       "        val json = JSONObject(text)"
+       "        val json = jsonObjectFromText(text)"
        "        truthTime(json)"
        "    } catch (_: Exception) {"
        "        null"