commit ce6f3e4dc81f678cc9d9f2b685a62a2a375e2a5c
parent 8fe0c561b5318c2cd4b34a48b9f84748bf3bf9d8
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Fri, 12 Jan 2024 19:22:04 +0000
JSON strings are escaped
Diffstat:
1 file changed, 45 insertions(+), 3 deletions(-)
diff --git a/src/pref-matrix.scm b/src/pref-matrix.scm
@@ -163,12 +163,54 @@
;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data File Generation
+(define json-escape-map
+ '(("\x00" . "\\u0000")
+ ("\x01" . "\\u0001")
+ ("\x02" . "\\u0002")
+ ("\x03" . "\\u0003")
+ ("\x04" . "\\u0004")
+ ("\x05" . "\\u0005")
+ ("\x06" . "\\u0006")
+ ("\a" . "\\u0007")
+ ("\b" . "\\b")
+ ("\t" . "\\t")
+ ("\n" . "\\n")
+ ("\v" . "\\u000b")
+ ("\f" . "\\f")
+ ("\r" . "\\r")
+ ("\x0e" . "\\u000e")
+ ("\x0f" . "\\u000f")
+ ("\x10" . "\\u0010")
+ ("\x11" . "\\u0011")
+ ("\x12" . "\\u0012")
+ ("\x13" . "\\u0013")
+ ("\x14" . "\\u0014")
+ ("\x15" . "\\u0015")
+ ("\x16" . "\\u0016")
+ ("\x17" . "\\u0017")
+ ("\x18" . "\\u0018")
+ ("\x19" . "\\u0019")
+ ("\x1a" . "\\u001a")
+ ("\x1b" . "\\u001b")
+ ("\x1c" . "\\u001c")
+ ("\x1d" . "\\u001d")
+ ("\x1e" . "\\u001e")
+ ("\x1f" . "\\u001f")
+ ("\"" . "\\\"")
+ ("\\" . "\\\\")))
+
+(define (json-escape raw-str)
+ (string-translate* raw-str json-escape-map))
+
(define (subject-json name)
(string-append
"{"
(string-intersperse
(map (lambda (row)
- (string-append "\"" (car row) "\":" (number->string (cadr row))))
+ (string-append "\""
+ (json-escape (car row))
+ "\":"
+ (number->string (cadr row))))
(subject-pref name 0 -1))
",")
"}"))
@@ -176,12 +218,12 @@
(define (all-json)
(string-append
"[[\""
- (string-intersperse (object-list) "\",\"")
+ (string-intersperse (map json-escape (object-list)) "\",\"")
"\"],{"
(string-intersperse
(map
(lambda (name)
- (string-append "\"" name "\":" (subject-json name)))
+ (string-append "\"" (json-escape name) "\":" (subject-json name)))
(subject-list))
",")
"}]"))