Move SSD group JSON reader to typed Kotlin

ober

9648cb9be8e9a520db9d7d1bbf8a266d7093fe9a

diff --git a/.build.yml b/.build.yml
index fafc0bb..89b2965 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#b0c22096435f1f3be933323d52e5a207896a3bc9"
+  - "https://git.sr.ht/~lisp/jerboa#3a359bfff280c0eee5fc85c4c6a24fc9fc669d58"
   # 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)" = b0c22096435f1f3be933323d52e5a207896a3bc9
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = c76a4d830a2b9af4382621128ea0e80d85dab859
+      test "$(git -C ../jerboa rev-parse HEAD)" = 3a359bfff280c0eee5fc85c4c6a24fc9fc669d58
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = a7ab43d1351a122fa69915cba29dc6cfdc4da9fd
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 21b6a38..9f9bb25 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": "b0c22096435f1f3be933323d52e5a207896a3bc9",
-    "tree": "c76a4d830a2b9af4382621128ea0e80d85dab859"
+    "commit": "3a359bfff280c0eee5fc85c4c6a24fc9fc669d58",
+    "tree": "a7ab43d1351a122fa69915cba29dc6cfdc4da9fd"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 785aa6c..8a2f641 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=b0c22096435f1f3be933323d52e5a207896a3bc9 # gitsafe:ignore
-jerboa_tree=c76a4d830a2b9af4382621128ea0e80d85dab859 # gitsafe:ignore
+jerboa_commit=3a359bfff280c0eee5fc85c4c6a24fc9fc669d58 # gitsafe:ignore
+jerboa_tree=a7ab43d1351a122fa69915cba29dc6cfdc4da9fd # 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 233f225..c8dae6a 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -818,12 +818,17 @@
     (typed-kotlin-file "com/sfb/ssdreview/SsdGroupJson.kt"
       (kotlin-imports (org json JSONArray) (org json JSONObject))
       (typed-library (com sfb ssdreview)
-        (export ssdGroupToTruthJson)
+        (export ssdGroupToTruthJson ssdGroupFromTruthJson)
         (type JSONObject)
         (type JSONArray)
         (type SsdCell)
         (type SsdGroup)
         (type Int32)
+        (def (jsonArrayOrEmpty (json : JSONObject) (key : String)) : JSONArray
+          (let ((items (json-object-opt-json-array json key)))
+            (if (nullable-null? items)
+                (json-array-empty)
+                (nullable-get items))))
         (def (ssdGroupToTruthJson (group : SsdGroup)
                                   (cellsById : (Map String SsdCell))) : JSONObject
           (let ((ids (json-array-empty)))
@@ -859,7 +864,40 @@
                       (begin
                         (json-object-put-string! json "firing_arc" (SsdGroup-firingArc group))
                         (int32 0)))
-                  json)))))))
+                  json)))))
+        (def (ssdGroupFromTruthJson (json : JSONObject)) : (Pair SsdGroup (MutableList SsdCell))
+          (let ((cells (mutable-list-empty SsdCell)))
+            (let ((ids (mutable-list-empty String)))
+              (let ((cellArray (jsonArrayOrEmpty json "cells")))
+                (begin
+                  (for/fold ((ignored (int32 0)))
+                            ((i (in-range (int32 0) (json-array-length cellArray))))
+                    (let ((cell (ssdCellFromJson (json-array-get-json-object cellArray i))))
+                      (begin
+                        (mutable-list-add! cells cell)
+                        (mutable-list-add! ids (SsdCell-id cell))
+                        ignored)))
+                  (let ((bboxArray (jsonArrayOrEmpty json "bbox")))
+                    (let ((bbox
+                            (float-array
+                              (json-array-opt-float32 bboxArray (int32 0) (float32 0.0))
+                              (json-array-opt-float32 bboxArray (int32 1) (float32 0.0))
+                              (json-array-opt-float32 bboxArray (int32 2) (float32 0.0))
+                              (json-array-opt-float32 bboxArray (int32 3) (float32 0.0)))))
+                      (let ((group
+                              (make-SsdGroup
+                                (json-object-opt-string-default json "source_group_id"
+                                  (json-object-opt-string-default json "id" "g0000"))
+                                (json-object-opt-string json "label")
+                                (json-object-opt-string json "box_type_id")
+                                (json-object-opt-string-default json "status" "reviewed")
+                                (json-object-opt-int32-default json "count" (list-size cells))
+                                bbox
+                                ids
+                                (json-object-opt-string-default json "firing_arc"
+                                  (json-object-opt-string json "arc"))
+                                (json-object-opt-string json "notes"))))
+                        (pair group cells)))))))))))
 
     (typed-kotlin-file "com/sfb/ssdreview/Guess.kt"
       (typed-library (com sfb ssdreview)
@@ -3617,31 +3655,6 @@
        "import kotlin.math.max"
        "import kotlin.math.min"
        ""
-       "fun ssdGroupFromTruthJson(json: JSONObject): Pair<SsdGroup, List<SsdCell>> {"
-       "    val cells = mutableListOf<SsdCell>()"
-       "    val ids = mutableListOf<String>()"
-       "    val cellArray = json.optJSONArray(\"cells\") ?: JSONArray()"
-       "    for (i in 0 until cellArray.length()) {"
-       "        val cell = ssdCellFromJson(cellArray.getJSONObject(i))"
-       "        cells.add(cell)"
-       "        ids.add(cell.id)"
-       "    }"
-       "    val bboxArray = json.optJSONArray(\"bbox\") ?: JSONArray()"
-       "    val bbox = FloatArray(4) { index -> bboxArray.optDouble(index, 0.0).toFloat() }"
-       "    val group = SsdGroup("
-       "        id = json.optString(\"source_group_id\", json.optString(\"id\", \"g0000\")),"
-       "        label = json.optString(\"label\"),"
-       "        boxTypeId = json.optString(\"box_type_id\"),"
-       "        status = json.optString(\"status\", \"reviewed\"),"
-       "        count = json.optInt(\"count\", cells.size),"
-       "        bbox = bbox,"
-       "        cellIds = ids,"
-       "        firingArc = json.optString(\"firing_arc\", json.optString(\"arc\")),"
-       "        notes = json.optString(\"notes\")"
-       "    )"
-       "    return group to cells"
-       "}"
-       ""
        "fun ssdSessionCellsById(session: SsdSession): Map<String, SsdCell> = session.cells.associateBy { it.id }"
        ""
        "fun ssdSessionToTruthJson(session: SsdSession): JSONObject {"