summaryrefslogtreecommitdiff
path: root/lib/zambyte/meta/chibi.scm
blob: 7cc777d89cc2c524b24719df8d92ba846879c05a (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
(import (rename (chibi json) (json->string base:json->string))
	(srfi 1))
(begin
  (define json-object list)
  (define json-list vector)
  (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)
    (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)
    (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 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))