diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2023-02-16 00:23:58 -0500 | 
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2023-02-16 00:23:58 -0500 | 
| commit | cd7057a886b0ddb2a6c804bd297d07c739456b48 (patch) | |
| tree | e38c1743c7e6f1d72e938e417d36d1e04f829fe4 | |
| parent | 334e09f3388a74b5982138b57abb2021e12cf598 (diff) | |
Add initial implementation of json-records
These behave like normal records, but with an added procedure to convert from a JSON string.
| -rw-r--r-- | lib/zambyte/meta/chibi.scm | 41 | ||||
| -rw-r--r-- | lib/zambyte/meta/json.sld | 46 | 
2 files changed, 78 insertions, 9 deletions
| diff --git a/lib/zambyte/meta/chibi.scm b/lib/zambyte/meta/chibi.scm index 812694f..7cc777d 100644 --- a/lib/zambyte/meta/chibi.scm +++ b/lib/zambyte/meta/chibi.scm @@ -1,28 +1,51 @@ -(import (chibi json) +(import (rename (chibi json) (json->string base:json->string))  	(srfi 1))  (begin    (define json-object list)    (define json-list vector) -  (define json-null (if #f #f)) -  (define json-object? list?) +  (define json-null (string->json "null")) + +  (define (json-object? obj) +    (or (list? obj) +	(json-record? obj))) +    (define json-list? vector?)    (define (json-object-contains-key? obj key) -    (and (assoc key obj) #t)) +    (or (and (list? obj) +	     (assoc key obj) +	     #t) +	(and (json-record? obj) +	     (json-object-contains-key? (json-record->fields obj) key))))    (define (json-null? obj)      (eq? obj json-null))    ;; json->string already defined in chibi json +  (define (json->string json) +    (if (json-record? json) +	(json->string (json-record->fields json)) +	(base:json->string json)))    ;; string->json already defined in chibi json    (define (json-object-ref json key) -    (let ((pair (assoc key json))) -      (if pair -	  (cdr pair) -	  ((json-key-not-found))))) +    (cond +     ((list? json) +      (let ((pair (assoc key json))) +	(if pair +	    (cdr pair) +	    ((json-key-not-found))))) +     ((json-record? json) +      (json-object-ref (json-record->fields json) key)) +     (else (error "json-object-ref: not an object" json))))    (define json-list-ref vector-ref)    (define json-list-length vector-length) -  (define json-object->alist values) + +  (define (json-object->alist json) +    (cond +     ((list? json) json) +     ((json-record? json) (json-record->fields json)) +     (else (error "json-object->alist: not an object" json)))) +    (define json-list->list vector->list)) diff --git a/lib/zambyte/meta/json.sld b/lib/zambyte/meta/json.sld index b6e681d..6d18d88 100644 --- a/lib/zambyte/meta/json.sld +++ b/lib/zambyte/meta/json.sld @@ -1,6 +1,7 @@  (define-library (zambyte meta json)    ;; portable    (export json +	  define-json-record-type  	  json-ref  	  json-value? @@ -41,6 +42,51 @@  	     value  	     (error "json: not a json value" value))))) +    ;; TODO: implement this +    (define (json-object-set! . args) #f) + +    (define-syntax handle-field-declarations +      (syntax-rules () +	((_ (field accessor)) +	 (define (accessor obj) +	   (json-object-ref obj 'field))) +	((_ (field accessor mutator)) +	 (begin +	   (define (accessor obj) +	     (json-object-ref obj 'field)) +	   (define (mutator obj value) +	     (json-object-set! obj 'field value)))))) + +    (define-syntax define-json-record-type +      (syntax-rules () +	((_ type +	    (constructor fields ...) +	    from-json-string +	    predicate +	    field-declarations ...) +	 (begin +	   (define (constructor fields ...) +	     (make-json-record 'type +			       (json-object `(fields . ,fields) ...) +			       from-json-string)) + +	   (define (predicate obj) +	     (and (json-record? obj) +		  (eq? (json-record->name obj) 'type))) + +	   (define (from-json-string str) +	     (let ((json (string->json str))) +	       (constructor (json-ref json 'fields) ...))) + +	   (handle-field-declarations field-declarations) ...)))) + +    (define-record-type <json-record> +      (make-json-record name fields from-json-string) +      json-record? +      (name json-record->name) +      (fields json-record->fields) +      (from-json-string json-record->from-json-string)) +      (define (json-ref json key . keys)        (let ((ref (cond ((json-object? json) json-object-ref)  		       ((json-list? json) json-list-ref) | 
