summaryrefslogtreecommitdiff
path: root/lib/zambyte/meta/json.sld
blob: 0313516a2bb48a08ab4bfe36631f17fc55c54e7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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."))))