-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSObj.scm
179 lines (171 loc) · 4.92 KB
/
SObj.scm
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
;;;;;;;;;;;;;;;;;;;;;
; SObj for scheme ;
; @author Quanyec ;
;;;;;;;;;;;;;;;;;;;;;
;;; SObj object and list tags ;;;
(define SOBJ-TAG '*obj)
(define LIST-TAG '*list)
;;; basic procedures ;;;
(define first car)
(define second cadr)
(define key-value-pair?
(lambda (lat)
(and (= 2 (length lat))
(atom? (first lat))
(atom? (second lat)))))
(define apair?
(lambda (lat)
(and (pair? lat) (<= 2 (length lat)))))
(define *list?
(lambda (sobj)
(and (apair? sobj) (eq? LIST-TAG (first sobj)))))
(define *sobj?
(lambda (sobj)
(and (apair? sobj) (eq? SOBJ-TAG (first sobj)))))
(define read-from-string
(lambda (s)
(with-input-from-string s read)))
(define ->JSON-value
(lambda (e)
(cond
[(string? e) (string-append "\"" e "\"")]
[(symbol? e) (->JSON-value (symbol->string e))]
[(number? e) (number->string e)]
[(char? e) (string e)]
[(boolean? e) (if e "true" "false")]
[(null? e) "null"]
[else (error '->JSON-value (format "~a do not convert to string" e))])))
;;;;;;;;;;;;;;;;;;;;;
;;; SObj Actions
;;;;;;;;;;;;;;;;;;;;;
;;; sfind - find an key from SObj
(define sfind
(lambda (sobj k)
(cond
[(null? sobj) '()]
[else
(let* ([fp (first sobj)]
[v (second fp)])
(cond
[(eq? (first fp) k) v]
[else (sfind (cdr sobj) k)]))])))
;;; sobj-ref
(define sobj-ref
(lambda (lat a)
(cond
[(string? lat)
(sobj-ref (read-from-string lat) a)]
[(*sobj? lat)
(sfind (cdr lat) a)]
[(*list? lat)
(list-ref (cdr lat) a)]
[else (error 'sobj-ref "Invalid SObj syntax")])))
;;;;;;;;;;;;;;;;;;;;;;
;;; HASH TABLE THINGS
;;;;;;;;;;;;;;;;;;;;;;
(define sobj->hashtable-list
(lambda (lat)
(cond
[(null? lat) '()]
[(atom? lat) lat]
[(*sobj? (car lat))
(cons (sobj->hashtable (car lat))
(sobj->hashtable-list (cdr lat)))]
[(*list? (car lat))
(cons (sobj->hashtable-list (cdar lat))
(sobj->hashtable-list (cdr lat)))]
[else
(cons (sobj->hashtable-list (car lat))
(sobj->hashtable-list (cdr lat)))])))
;;; sobj->hashtable
(define sobj->hashtable
(case-lambda
[(lat) (sobj->hashtable (make-eq-hashtable) lat)]
[(ht lat)
(cond
[(null? lat) ht]
[(*sobj? lat)
(sobj->hashtable ht (cdr lat))]
[(*list? lat)
(sobj->hashtable-list (cdr lat))]
[else
(let ([kvp (first lat)])
(cond
[(key-value-pair? kvp)
(put-hash-table! ht (first kvp) (second kvp))
(sobj->hashtable ht (cdr lat))]
[else
(put-hash-table! ht (first kvp)
(sobj->hashtable (second kvp)))
(sobj->hashtable ht (cdr lat))]))])]))
;;; s-ref -- SObj's hashtable reference
(define s-ref
(lambda (ht-sobj k)
(hashtable-ref ht-sobj k '())))
(define hashtable->sobj
(lambda (ht)
(letrec ([->list
(lambda (lat)
(cond
[(null? lat) '()]
[(atom? lat) lat]
[(hashtable? (car lat))
(cons (hashtable->sobj (car lat))
(->list (cdr lat)))]
[(list? (car lat))
(cons (cons LIST-TAG (car lat))
(->list (cdr lat)))]
[else
(cons (->list (car lat))
(->list (cdr lat)))]))])
(cons SOBJ-TAG
(hash-table-map ht
(lambda (k v)
(cond
[(hashtable? v)
(cons k
(cons (hashtable->sobj v) '()))]
[(list? v)
(cons k
(cons (cons LIST-TAG (->list v))
'()))]
[else (cons k (cons v '()))])))))))
;;;;;;;;;;;;;;;;;;;;;
;;; JSON THINGS
;;;;;;;;;;;;;;;;;;;;;
(define sobj->JSON-list
(lambda (lat)
(cond
[(null? lat) "]"]
[(or (*sobj? (first lat))
(*list? (first lat)))
(string-append (sobj->JSON (first lat))
(if (null? (cdr lat)) "]"
(string-append "," (sobj->JSON-list (cdr lat)))))]
[else
(string-append (->JSON-value (first lat))
(if (null? (cdr lat)) "]"
(string-append "," (sobj->JSON-list (cdr lat)))))])))
;;; sobj->JSON
(define sobj->JSON
(lambda (lat)
(cond
[(null? lat) "{}"]
[(or (*sobj? lat) (*list? lat))
(let loop ([lat lat])
(cond
;;[(null? lat) "}"]
[(*sobj? lat)
(string-append "{" (loop (cdr lat)))]
[(*list? lat)
(string-append "[" (sobj->JSON-list (cdr lat)))]
[else
(let* ([fp (first lat)]
[k (first fp)]
[v (second fp)])
(if (null? (cdr lat))
(string-append (->JSON-value k) ":" (sobj->JSON v) "}")
(string-append (->JSON-value k) ":" (sobj->JSON v) ","
(loop (cdr lat)))))]))]
[(atom? lat) (->JSON-value lat)]
[else (error 'sobj-ref "Invalid SObj syntax")])))