-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpp.rkt
163 lines (143 loc) · 5.7 KB
/
pp.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
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
#lang racket/base
(require racket/contract)
(require (only-in (file "./json.rkt")
count-properties
object-properties
property-value))
(require (only-in racket/port
with-output-to-string))
(require json)
(require (only-in racket/list
take
last
rest
first
empty?))
(require (only-in racket/string
string-split
string-trim))
(module+ test
(require rackunit))
(define spaces-per-indentation-level 2)
(define (make-pad indentation-level)
(make-string (* indentation-level spaces-per-indentation-level)
#\space))
(define (indent-line str indentation)
(format "~a~a" (make-pad indentation) str))
(define (indent-lines/list lines indentation)
(cond ((empty? lines)
(make-pad indentation))
((empty? (rest lines))
(indent-line (first lines) indentation))
(else
(format "~a~%~a"
(indent-line (first lines) indentation)
(indent-lines/list (rest lines) indentation)))))
(module+ test
(check-equal? "" (indent-lines/list (list) 0))
(check-equal? "a" (indent-lines/list (list "a") 0))
(check-equal? " a" (indent-lines/list (list "a") 1))
(check-equal? "hi\nthere" (indent-lines/list (list "hi" "there") 0))
(check-equal? " ahlan\n thabatat" (indent-lines/list (list "ahlan" "thabatat") 2)))
(define (explode-lines str)
(string-split str "\n"))
(define (indent-lines/str str indentation)
(indent-lines/list (explode-lines str)
indentation))
(define/contract (json-pretty-print js)
(-> jsexpr? string?)
(define (pp x level)
(define (indent str)
(indent-lines/str str level))
(let ([pad (make-pad level)])
(cond ((eq? 'null x)
"null")
((real? x)
(format "~a" x))
((boolean? x)
(if x "true" "false"))
((string? x) x
(format "~s" x))
((list? x)
(let ([num-items (length x)]
[items x])
(if (= num-items 0)
"[]"
(with-output-to-string
(lambda ()
(display "[")
(newline)
;; all elements except the final one
(for ([item (take items (- num-items 1))])
(display (indent-lines/str (pp item 0) 1))
(display ",")
(newline))
;; last item
(display (indent-lines/str (pp (last items) 0) 1))
(newline)
(display "]"))))))
((hash? x)
(let ([num-props (count-properties x)])
(if (= num-props 0)
"{}"
(with-output-to-string
(lambda ()
(display "{")
(newline)
(let ([pad (make-pad (+ level 1))]
[props (object-properties x)])
;; all but final property
(for ([prop (take props (- num-props 1))])
(display (indent-line (format "\"~s\": " prop)
1))
(display (string-trim (indent-lines/str (pp (property-value x prop) 0) 1)
" "
#:left? #t
#:right? #f
#:repeat? #t))
(display ",")
(newline))
(let ([final (last props)])
(display (indent-line (format "\"~s\": " final)
1))
(display (string-trim (indent-lines/str (pp (property-value x final) 0) 1)
" "
#:left? #t
#:right? #f
#:repeat? #t))))
(newline)
(display "}")))))))))
(pp js 0))
(provide json-pretty-print)
(module+ test
(check-equal? (json-pretty-print 'null)
"null")
(check-equal? (json-pretty-print -5)
"-5")
(check-equal? (json-pretty-print "hello!")
"\"hello!\"")
(check-equal? (json-pretty-print #f)
"false")
(check-equal? (json-pretty-print (list))
"[]")
(check-equal? (json-pretty-print (make-string 1 #\nul))
"\"\\u0000\"")
(check-equal? (json-pretty-print "üff då phô")
"\"üff då phô\"")
(check-equal? (json-pretty-print (hasheq))
"{}")
(check-equal? (json-pretty-print (list #t "jim bob" (list)))
"[
true,
\"jim bob\",
[]
]")
(let ([obj (hasheq 'some "pig" 'pig 'null)])
(let ([rendered (json-pretty-print obj)])
(check-true (or (string=? rendered "{\n \"some\": \"pig\",\n \"pig\": null\n}")
(string=? rendered "{\n \"pig\": null,\n \"some\": \"pig\"\n}")) rendered)))
(let ([doc (hasheq 'dependencies (hasheq 'foo (list "bar")))])
(check-equal? (json-pretty-print doc)
"{\n \"dependencies\": {\n \"foo\": [\n \"bar\"\n ]\n }\n}"))
(check-equal? (json-pretty-print (list 5 (list #t) 'null))
"[\n 5,\n [\n true\n ],\n null\n]"))