Idiomdrottning’s homepage

Call-tables and Medea

Here’s a simple way to write JSON from call-tables. It’s very unoptimal for now since it uses an intermediate representation instead of writing directly:

(import brev-separate medea srfi-69)

(json-unparsers
 `((,symbol? . ,(o write symbol->string))
   (,call-table*?
    . ,(fn (write-json
            ((over (cons (car x)
                         (list->vector (cdr x))))
             (hash-table->alist (x))))))
   (,call-table? . ,(fn (write-json
                         (hash-table->alist (x)))))
   ,@(json-unparsers)))

It makes writing them straight-forward:

(define fruit-color
  (ctq* apple red pear (green yellow)))

(write-json fruit-color)