-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rkt
57 lines (45 loc) · 1.34 KB
/
main.rkt
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
#lang racket
(require rackunit)
; ----- JSON Parsing -----
(require json)
; A JSON is one of:
; - String
; - Number
; - Boolean
; - 'null
; - JSONArray
; - JSONObject
;;A JSONArray is a [List-of JSON])
;;A JSONObject is a [List-of JSONPair]
;;A JSONPair is a (list Symbol JSON)
;;A PartialJson is one of:
; - String
; - Number
; - Boolean
; - 'null
; - [List-of PartialJson]
; - [HashMap Symbol PartialJson]
;;parse-hashmaps : PartialJson -> Json
;;Parse the hashmaps into association lists
(define (parse-hashmaps pj)
(cond [(cons? pj) (map parse-hashmaps pj)]
[(hash? pj) (hash-map pj (λ (x y) (list x (parse-hashmaps y))))]
[else pj]))
(check-equal? (parse-hashmaps "hi") "hi")
(check-equal? (parse-hashmaps (list (make-immutable-hasheq (list (cons 'a 1)))
(make-immutable-hasheq (list (cons 'b true)))
(make-immutable-hasheq (list (cons 'c "hello")))))
'(((a 1)) ((b #t)) ((c "hello"))))
;; string->json : RawJsonString -> JSon
;; Parse the string
(define (string->json rjs)
(parse-hashmaps (string->jsexpr rjs)))
; ----- HTTP -----
(require net/url)
; String -> String
(define (get-url str)
(string-trim (bytes->string/utf-8
(port->bytes (get-pure-port (string->url str))))))
; ----- Provides -----
(provide string->json
get-url)