Skip to content

Instantly share code, notes, and snippets.

@bis83
Last active August 29, 2015 14:27
Show Gist options
  • Save bis83/f474ad42b5639821be1a to your computer and use it in GitHub Desktop.
Save bis83/f474ad42b5639821be1a to your computer and use it in GitHub Desktop.
tiny s-expression -> json converter
(use srfi-13 ports)
(define (s->json s)
(cond
((and (pair? s) (string? (car s)))
(sprintf "~a:~a"
(s->json (car s))
(s->json (cdr s))))
((pair? s)
(sprintf "{~a}"
(string-join (map s->json s) ",")))
((vector? s)
(sprintf "[~a]"
(string-join (map s->json (vector->list s)) ",")))
((string? s)
(with-output-to-string (cut write s)))
((number? s)
(number->string s))
(else (error s->json s))))
(print
(s->json
`(("name"
("foo" . #(1 2 3))
("bar" . "hige"))
("data" . "qwerty"))))
; ==> {"name":{"foo":[1,2,3],"bar":"hige"},"data":"qwerty"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment