Move SSD group JSON writer to typed Kotlin

ober

8fc428b28225637d9dcc582ff8888c38ff7aa845

diff --git a/.build.yml b/.build.yml
index 03caea2..fafc0bb 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#778d80053579958c2297c6f183517fdf1db94950"
+  - "https://git.sr.ht/~lisp/jerboa#b0c22096435f1f3be933323d52e5a207896a3bc9"
   # 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)" = 778d80053579958c2297c6f183517fdf1db94950
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = d643e4a71c3d517cc14732ff7e7b87fe578b014b
+      test "$(git -C ../jerboa rev-parse HEAD)" = b0c22096435f1f3be933323d52e5a207896a3bc9
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = c76a4d830a2b9af4382621128ea0e80d85dab859
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index cceec91..21b6a38 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": "778d80053579958c2297c6f183517fdf1db94950",
-    "tree": "d643e4a71c3d517cc14732ff7e7b87fe578b014b"
+    "commit": "b0c22096435f1f3be933323d52e5a207896a3bc9",
+    "tree": "c76a4d830a2b9af4382621128ea0e80d85dab859"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 9cdcbdf..785aa6c 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=778d80053579958c2297c6f183517fdf1db94950 # gitsafe:ignore
-jerboa_tree=d643e4a71c3d517cc14732ff7e7b87fe578b014b # gitsafe:ignore
+jerboa_commit=b0c22096435f1f3be933323d52e5a207896a3bc9 # gitsafe:ignore
+jerboa_tree=c76a4d830a2b9af4382621128ea0e80d85dab859 # 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 0275fd9..233f225 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -815,6 +815,52 @@
             (json-object-opt-float32 json "h")
             (json-object-opt-string-default json "detector" "truth")))))
 
+    (typed-kotlin-file "com/sfb/ssdreview/SsdGroupJson.kt"
+      (kotlin-imports (org json JSONArray) (org json JSONObject))
+      (typed-library (com sfb ssdreview)
+        (export ssdGroupToTruthJson)
+        (type JSONObject)
+        (type JSONArray)
+        (type SsdCell)
+        (type SsdGroup)
+        (type Int32)
+        (def (ssdGroupToTruthJson (group : SsdGroup)
+                                  (cellsById : (Map String SsdCell))) : JSONObject
+          (let ((ids (json-array-empty)))
+            (let ((cells (json-array-empty)))
+              (let ((json (json-object-empty)))
+                (begin
+                  (for/fold ((ignored (int32 0)))
+                            ((i (in-range (int32 0) (list-size (SsdGroup-cellIds group)))))
+                    (let ((cellId (list-ref (SsdGroup-cellIds group) i)))
+                      (begin
+                        (json-array-put-string! ids cellId)
+                        (let ((cell (map-ref-or-null cellsById cellId)))
+                          (if (nullable-null? cell)
+                              ignored
+                              (begin
+                                (json-array-put-json-object! cells
+                                  (ssdCellToJson (nullable-get cell)))
+                                ignored))))))
+                  (json-object-put-string! json "source_group_id" (SsdGroup-id group))
+                  (json-object-put-string! json "label" (SsdGroup-label group))
+                  (json-object-put-string! json "box_type_id" (SsdGroup-boxTypeId group))
+                  (json-object-put-string! json "status" (SsdGroup-status group))
+                  (json-object-put-int32! json "count"
+                    (if (> (SsdGroup-count group) (int32 0))
+                        (SsdGroup-count group)
+                        (json-array-length cells)))
+                  (json-object-put-json-array! json "bbox" (floatArrayToJson (SsdGroup-bbox group)))
+                  (json-object-put-json-array! json "cell_ids" ids)
+                  (json-object-put-json-array! json "cells" cells)
+                  (json-object-put-string! json "notes" (SsdGroup-notes group))
+                  (if (string-blank? (SsdGroup-firingArc group))
+                      (int32 0)
+                      (begin
+                        (json-object-put-string! json "firing_arc" (SsdGroup-firingArc group))
+                        (int32 0)))
+                  json)))))))
+
     (typed-kotlin-file "com/sfb/ssdreview/Guess.kt"
       (typed-library (com sfb ssdreview)
         (export make-Guess Guess? Guess-label Guess-boxType Guess-reason)
@@ -3571,30 +3617,6 @@
        "import kotlin.math.max"
        "import kotlin.math.min"
        ""
-       "fun ssdGroupToTruthJson(group: SsdGroup, cellsById: Map<String, SsdCell>): JSONObject {"
-       "    val groupCells = group.cellIds.mapNotNull { cellsById[it] }"
-       "    val ids = JSONArray()"
-       "    group.cellIds.forEach { ids.put(it) }"
-       "    val cells = JSONArray()"
-       "    groupCells.forEach { cells.put(ssdCellToJson(it)) }"
-       "    val box = JSONArray()"
-       "    group.bbox.forEach { box.put(it.toDouble()) }"
-       "    val json = JSONObject()"
-       "        .put(\"source_group_id\", group.id)"
-       "        .put(\"label\", group.label)"
-       "        .put(\"box_type_id\", group.boxTypeId)"
-       "        .put(\"status\", group.status)"
-       "        .put(\"count\", if (group.count > 0) group.count else groupCells.size)"
-       "        .put(\"bbox\", box)"
-       "        .put(\"cell_ids\", ids)"
-       "        .put(\"cells\", cells)"
-       "        .put(\"notes\", group.notes)"
-       "    if (group.firingArc.isNotBlank()) {"
-       "        json.put(\"firing_arc\", group.firingArc)"
-       "    }"
-       "    return json"
-       "}"
-       ""
        "fun ssdGroupFromTruthJson(json: JSONObject): Pair<SsdGroup, List<SsdCell>> {"
        "    val cells = mutableListOf<SsdCell>()"
        "    val ids = mutableListOf<String>()"