summaryrefslogtreecommitdiff
path: root/lib/zambyte/meta/sagittarius.scm
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2023-02-12 22:46:48 -0500
committerRobby Zambito <contact@robbyzambito.me>2023-02-12 22:46:48 -0500
commitb1294088059fe22d1a05d0f9637fdd598795d8ea (patch)
tree2031c4c455d3e26c4ab985eae678de7abc1a0042 /lib/zambyte/meta/sagittarius.scm
parent91adcf023b0c2684991161661032e1aae3cd6b0c (diff)
Add json-object-contains-key?, json-list-length, and json-key-not-found.
Update README accordingly and with add examples.
Diffstat (limited to 'lib/zambyte/meta/sagittarius.scm')
-rw-r--r--lib/zambyte/meta/sagittarius.scm35
1 files changed, 22 insertions, 13 deletions
diff --git a/lib/zambyte/meta/sagittarius.scm b/lib/zambyte/meta/sagittarius.scm
index 3b5a741..97deb0b 100644
--- a/lib/zambyte/meta/sagittarius.scm
+++ b/lib/zambyte/meta/sagittarius.scm
@@ -2,17 +2,6 @@
(srfi 1)
(srfi 2))
(begin
- (define (string->json str)
- (call-with-port (open-input-string str)
- (lambda (port)
- (json-read port))))
-
- (define (json->string json)
- (call-with-port (open-output-string)
- (lambda (port)
- (json-write json port)
- (get-output-string port))))
-
(define (json-object . pairs)
(if (eq? (*json-map-type*) 'vector)
(apply vector pairs)
@@ -38,13 +27,33 @@
(define (json-null? obj)
(eq? obj json-null))
+ (define (json-object-contains-key? obj key)
+ (and (assoc key (json-object->alist obj)) #t))
+
+ (define (string->json str)
+ (call-with-port (open-input-string str)
+ (lambda (port)
+ (json-read port))))
+
+ (define (json->string json)
+ (call-with-port (open-output-string)
+ (lambda (port)
+ (json-write json port)
+ (get-output-string port))))
+
(define (json-object-ref json key)
- (and-let* ((pair (assoc key (json-object->alist json))))
- (cdr pair)))
+ (or (and-let* ((pair (assoc key (json-object->alist json))))
+ (cdr pair))
+ ((json-key-not-found))))
(define (json-list-ref json index)
(list-ref (json-list->list json) index))
+ (define (json-list-length json)
+ (if (eq? (*json-map-type*) 'vector)
+ (length json)
+ (vector-length json)))
+
(define (json-object->alist json)
(if (eq? (*json-map-type*) 'vector)
(vector->list json)