-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.lisp
88 lines (65 loc) · 2.56 KB
/
output.lisp
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
;;;; json-streams
;;;;
;;;; Copyright (C) 2017 Thomas Bakketun <[email protected]>
;;;;
;;;; This library is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this library. If not, see <http://www.gnu.org/licenses/>.
(in-package #:json-streams)
(defun call-with-json-output (target options function)
(if target
(with-open-json-stream (*json-stream* (apply #'make-json-output-stream target options))
(funcall function))
(with-output-to-string (out)
(with-open-json-stream (*json-stream* (apply #'make-json-output-stream out options))
(funcall function)))))
(defmacro with-json-output ((&optional target &rest options) &body body)
`(call-with-json-output ,target (list ,@options) (lambda () ,@body)))
(defmacro with-json-array (&body body)
`(progn
(json-write :begin-array *json-stream*)
,@body
(json-write :end-array *json-stream*)))
(defmacro with-json-object (&body body)
`(progn
(json-write :begin-object *json-stream*)
,@body
(json-write :end-object *json-stream*)))
(defmacro with-json-member (key &body body)
`(progn
(json-write (json-encode-key ,key *json-stream*) *json-stream*)
,@body))
(defun json-output-member (key value)
(json-write (json-encode-key key *json-stream*) *json-stream*)
(json-output-value value))
(defun json-output-value (value)
(etypecase value
((or string real)
(json-write value *json-stream*))
(sequence
(with-json-array
(map nil #'json-output-value value)))
(hash-table
(with-json-object
(maphash #'json-output-member value)))))
(defun json-output-boolean (value)
(json-write (if value :true :false) *json-stream*))
(defun json-output-null ()
(json-write :null *json-stream*))
(defun json-output-alist (alist)
(with-json-object
(loop for (key . value) in alist do
(json-output-member key value))))
(defun json-output-plist (plist)
(with-json-object
(loop for (key value) on plist by #'cddr do
(json-output-member key value))))