summaryrefslogtreecommitdiff
path: root/lib/zambyte/meta/json.sld
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2023-02-12 01:30:42 -0500
committerRobby Zambito <contact@robbyzambito.me>2023-02-12 01:30:42 -0500
commit91adcf023b0c2684991161661032e1aae3cd6b0c (patch)
tree66b76d890cecb354c497b26355a8a44c73527078 /lib/zambyte/meta/json.sld
Initial commit
Diffstat (limited to 'lib/zambyte/meta/json.sld')
-rw-r--r--lib/zambyte/meta/json.sld57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/zambyte/meta/json.sld b/lib/zambyte/meta/json.sld
new file mode 100644
index 0000000..0313516
--- /dev/null
+++ b/lib/zambyte/meta/json.sld
@@ -0,0 +1,57 @@
+(define-library (zambyte meta json)
+ ;; portable
+ (export json-ref
+ json-value?)
+ ;; non-portable
+ (export string->json
+ json->string
+
+ json-object
+ json-list
+ json-null
+
+ json-object?
+ json-list?
+ json-null?
+
+ json-object-ref
+ json-list-ref
+
+ json-object->alist
+ json-list->list)
+ (import (scheme base))
+ ;; portable
+ (begin
+ (define (json-ref json key . keys)
+ (let ((ref (cond ((json-object? json) json-object-ref)
+ ((json-list? json) json-list-ref)
+ (else (error "json-ref: cannot get reference on json value" json)))))
+ (if (null? keys)
+ (ref json key)
+ (apply json-ref (ref json key) keys))))
+
+ (define (json-value? obj)
+ (or (boolean? obj)
+ (number? obj)
+ (string? obj)
+ (json-null? obj)
+ (json-list? obj)
+ (json-object? obj))))
+ ;; non-portable
+ (cond-expand
+ ((and (library (macduffie json)) (srfi 69))
+ (include-library-declarations "macduffie.scm"))
+ ((and guile (library (json)))
+ (include-library-declarations "guile.scm"))
+ (gauche
+ (include-library-declarations "gauche.scm"))
+ (gerbil
+ (include-library-declarations "gerbil.scm"))
+ (sagittarius
+ (include-library-declarations "sagittarius.scm"))
+ ((library (chibi json))
+ (include-library-declarations "chibi.scm"))
+ ((library (srfi 180))
+ (include-library-declarations "180.scm"))
+ (else
+ (error "No implementation of JSON library available."))))