-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-json.scm
172 lines (156 loc) · 5.99 KB
/
format-json.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
;;;
;;; format-json.scm - JSON output formater
;;;
;;; Copyright (c) 2009-2012 Jens Thiele <karme@karme.de>
;;; based on:
;;; json.scm - JSON (RFC4627) Parser
;;;
;;; Copyright (c) 2006 Rui Ueyama (rui314@gmail.com)
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; 2. Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in the
;;; documentation and/or other materials provided with the distribution.
;;;
;;; 3. Neither the name of the authors nor the names of its contributors
;;; may be used to endorse or promote products derived from this
;;; software without specific prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
;;; http://www.ietf.org/rfc/rfc4627.txt
(define-module format-json
(use srfi-13)
(use srfi-14)
(use srfi-43)
(use util.list)
(use gauche.collection)
(use gauche.sequence)
(export ->jsonf
format-number-fixed-point
print-fixed-point))
(select-module format-json)
;;;============================================================
;;; Writer
;;;
(define (print-value obj format)
(cond ((eq? obj 'false) (display "false"))
((eq? obj 'null) (display "null"))
((eq? obj 'true) (display "true"))
((pair? obj) (print-object obj format)) ;; todo: empty list?
((vector? obj) (print-array obj format))
((number? obj) ((ref format 'number print-number) obj))
((string? obj) (print-string obj))
(else (error "format-json expects list or vector, but got:" obj))))
(define (print-object obj format)
(display "{")
(fold (lambda (attr comma)
(display comma)
(print-string (x->string (car attr)))
(display ":")
(print-value (cdr attr) format)
",")
"" obj)
(display "}"))
(define (print-array obj format)
(display "[")
(vector-for-each (lambda (i val)
(unless (zero? i) (display ","))
(print-value val format))
obj)
(display "]"))
(define (print-number num)
(cond [(or (not (real? num)) (not (finite? num)))
(error "real number expected, but got" num)]
[(and (rational? num) (not (integer? num)))
(write (exact->inexact num))]
[else (write num)]))
;; uh - quite ugly - and likely does not conform to spec
(define (print-string str)
(display
(string-append
"\""
(string-join
(map
(lambda(c)
(if (char-set-contains? char-set:ascii c)
(subseq (write-to-string (string c)) 1 -1)
(format "\\u~4,'0x" (char->ucs c))))
str)
"")
"\"")))
(define (->jsonf x . args)
(let-optionals* args ((format '()))
(let ((f (alist->hash-table format)))
(with-output-to-string
(lambda ()
(cond ((pair? x) (print-object x f))
((vector? x) (print-array x f))
(else (error "format-json expects list or vector, but got" x))))))))
(define (fixed-point precision)
(let ((p (expt 10 precision)))
(lambda(x)
(receive (q r) #?=(quotient&remainder #?=(round->exact (* x p)) p)
((if (negative? x) - +)
(+ (abs q) (/ (abs r) p)))))))
;; todo:
;; - did not expect it to be that complicated
;; - handle nan/infinity?
(define (format-number-fixed-point precision)
(let ((p (expt 10 precision)))
(lambda(x)
;; note: round in scheme behaves strange:
;; If fractional part of X is exactly 0.5, ROUND returns the closest even integer.
(receive (q r) (quotient&remainder (round->exact (* x p)) p)
(string-append
(if (negative? x) "-" "")
(if (zero? r)
;; todo: it is probably a bad idea to skip the point (exact vs inexact)
(number->string (abs q))
(string-join (list (number->string (abs q))
(string-trim-right (string-pad (number->string (abs r)) precision #\0) #\0)) ".")))))))
(define (string->fixed-point s)
(let* ((qr (string-split s "."))
(q (string->number (car qr)))
(r (if (null? (cdr qr)) "0" (cadr qr))))
(when (string-null? s)
(error "empty string not allowed"))
((if (char=? (ref s 0) #\-) - +)
q
(/ (string->number r) (expt 10 (string-length r))))))
(define (test-format-number-fixed-point)
(let ((f (format-number-fixed-point 4)))
(for-each
(lambda(x)
(let* ((n #?=(string->fixed-point #?=(f x)))
(e #?=(abs (- n x))))
(when (> e (/ 1 (expt 10 4)))
(error "test failed: " x (exact->inexact e)))))
#?=((lambda(l)
(append l (map - l)))
`(10 ,(+ 10000000000000 1/3) 10000.3299 1003.3 1E100 0 0.00005
0.1 0.02 999999999999999999.00005)))))
(define (print-fixed-point precision)
(let ((f (format-number-fixed-point precision)))
(lambda(x)
(display (f x)))))
(define (main args)
#?=(->jsonf #(0 0 0))
#?=(->jsonf #(0 0 0) `((number . ,(print-fixed-point 4)))))
(provide "format-json")