From bd7af0146c722c29caaf62dbb1dfd4845c6fb255 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 29 Jan 2025 15:20:32 -0600 Subject: [PATCH] Allow non-string json object keys --- d-/json.scm | 11 ++++++----- test.scm | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/d-/json.scm b/d-/json.scm index afcae1d..3acf786 100644 --- a/d-/json.scm +++ b/d-/json.scm @@ -27,8 +27,8 @@ (define-peg-pattern eobj all (and open-cu close-cu)) (define-peg-pattern object all (or eobj (and open-cu - (* (and string (ignore ":") ++ value comma)) - (and string (ignore ":") ++ value) + (* (and value (ignore ":") ++ value comma)) + (and value (ignore ":") ++ value) close-cu))) (define-peg-pattern value body (or number boolean string array object)) @@ -49,9 +49,10 @@ (define (parse-object ss) (if (or (null? ss) (null? (cdr ss))) '() - (acons (string->symbol (parse-value (car ss))) - (parse-value (cadr ss)) - (parse-object (cddr ss))))) + (let ([key (parse-value (car ss))]) + (acons (if (string? key) (string->symbol key) key) + (parse-value (cadr ss)) + (parse-object (cddr ss)))))) (define flatwords '(boolean number string array object)) diff --git a/test.scm b/test.scm index 6a2400b..14fab7f 100644 --- a/test.scm +++ b/test.scm @@ -121,8 +121,9 @@ (assert-equal '() (json "{}")) (assert-equal '((cat . 1) (bat . #t) - (rat . "yessir")) - (json "{\"cat\": 1, \"bat\": true, \"rat\": \"yessir\"}")) + (rat . "yessir") + (4 . "what")) + (json "{\"cat\": 1, \"bat\": true, \"rat\": \"yessir\", 4: \"what\"}")) (assert-equal '((butter . 1) (brownie)) (json "{\"butter\": 1, \"brownie\": {}}"))))