forked from phmarek/yason
/
encode.lisp
266 lines (228 loc) · 8.79 KB
/
encode.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
;; This file is part of yason, a Common Lisp JSON parser/encoder
;;
;; Copyright (c) 2008 Hans Hübner
;; All rights reserved.
;;
;; Please see the file LICENSE in the distribution.
(in-package :yason)
(defvar *json-output*)
(defgeneric encode (object &optional stream)
(:documentation "Encode OBJECT to STREAM in JSON format. May be
specialized by applications to perform specific rendering. STREAM
defaults to *STANDARD-OUTPUT*."))
;; from alexandria
(defun plist-hash-table (plist &rest hash-table-initargs)
"Returns a hash table containing the keys and values of the property list
PLIST. Hash table is initialized using the HASH-TABLE-INITARGS."
(let ((table (apply #'make-hash-table hash-table-initargs)))
(do ((tail plist (cddr tail)))
((not tail))
(setf (gethash (car tail) table) (cadr tail)))
table))
(defparameter *char-replacements*
(plist-hash-table
'(#\\ "\\\\"
#\" "\\\""
#\/ "\\/"
#\Backspace "\\b"
#\Page "\\f"
#\Newline "\\n"
#\Return "\\r"
#\Tab "\\t")))
(defmethod encode ((string string) &optional (stream *standard-output*))
(write-char #\" stream)
(dotimes (i (length string))
(let* ((char (aref string i))
(replacement (gethash char *char-replacements*)))
(if replacement
(write-string replacement stream)
(write-char char stream))))
(write-char #\" stream)
string)
(defmethod encode ((object rational) &optional (stream *standard-output*))
(encode (float object) stream)
object)
(defmethod encode ((object double-float) &optional (stream *standard-output*))
(encode (coerce object 'single-float) stream)
object)
(defmethod encode ((object float) &optional (stream *standard-output*))
(princ object stream)
object)
(defmethod encode ((object integer) &optional (stream *standard-output*))
(princ object stream))
(defun encode-key/value (key value stream)
(encode key stream)
(write-char #\: stream)
(encode value stream))
(defmethod encode ((object hash-table) &optional (stream *standard-output*))
(write-char #\{ stream)
(let (printed)
(maphash (lambda (key value)
(if printed
(write-char #\, stream)
(setf printed t))
(encode-key/value key value stream))
object))
(write-char #\} stream)
object)
(defmethod encode ((object vector) &optional (stream *standard-output*))
(write-char #\[ stream)
(let (printed)
(loop
for value across object
do
(when printed
(write-char #\, stream))
(setf printed t)
(encode value stream)))
(write-char #\] stream)
object)
(defmethod encode ((object list) &optional (stream *standard-output*))
(write-char #\[ stream)
(let (printed)
(dolist (value object)
(if printed
(write-char #\, stream)
(setf printed t))
(encode value stream)))
(write-char #\] stream)
object)
(defun encode-symbol/value (symbol value stream)
(let ((string (symbol-name symbol)))
(encode-key/value string value stream)))
(defun encode-alist (object &optional (stream *standard-output*))
(loop initially (write-char #\{ stream)
with printed = nil
for (key . value) in object
do (if printed
(write-char #\, stream)
(setf printed t))
(encode-symbol/value key value stream)
finally (write-char #\} stream)
(return object)))
(defun encode-plist (object &optional (stream *standard-output*))
(loop initially (write-char #\{ stream)
with printed = nil
for (key value . rest) on object by #'cddr
do (if printed
(write-char #\, stream)
(setf printed t))
(encode-symbol/value key value stream)
finally (write-char #\} stream)
(return object)))
(defmethod encode ((object (eql 'true)) &optional (stream *standard-output*))
(write-string "true" stream)
object)
(defmethod encode ((object (eql 'false)) &optional (stream *standard-output*))
(write-string "false" stream)
object)
(defmethod encode ((object (eql 'null)) &optional (stream *standard-output*))
(write-string "null" stream)
object)
(defmethod encode ((object (eql t)) &optional (stream *standard-output*))
(write-string "true" stream)
object)
(defmethod encode ((object (eql nil)) &optional (stream *standard-output*))
(write-string "null" stream)
object)
(defclass json-output-stream ()
((output-stream :reader output-stream
:initarg :output-stream)
(stack :accessor stack
:initform nil))
(:documentation "Objects of this class capture the state of a JSON stream encoder."))
(defun next-aggregate-element ()
(if (car (stack *json-output*))
(write-char (car (stack *json-output*)) (output-stream *json-output*))
(setf (car (stack *json-output*)) #\,)))
(defmacro with-output ((stream) &body body)
"Set up a JSON streaming encoder context on STREAM, then evaluate BODY."
`(let ((*json-output* (make-instance 'json-output-stream :output-stream ,stream)))
,@body))
(defmacro with-output-to-string* (() &body body)
"Set up a JSON streaming encoder context, then evaluate BODY.
Return a string with the generated JSON output."
`(with-output-to-string (s)
(with-output (s)
,@body)))
(define-condition no-json-output-context (error)
()
(:report "No JSON output context is active")
(:documentation "This condition is signalled when one of the stream
encoding function is used outside the dynamic context of a
WITH-OUTPUT or WITH-OUTPUT-TO-STRING* body."))
(defmacro with-aggregate ((begin-char end-char) &body body)
`(progn
(unless (boundp '*json-output*)
(error 'no-json-output-context))
(when (stack *json-output*)
(next-aggregate-element))
(write-char ,begin-char (output-stream *json-output*))
(push nil (stack *json-output*))
(prog1
(progn ,@body)
(pop (stack *json-output*))
(write-char ,end-char (output-stream *json-output*)))))
(defmacro with-array (() &body body)
"Open a JSON array, then run BODY. Inside the body,
ENCODE-ARRAY-ELEMENT must be called to encode elements to the opened
array. Must be called within an existing JSON encoder context, see
WITH-OUTPUT and WITH-OUTPUT-TO-STRING*."
`(with-aggregate (#\[ #\]) ,@body))
(defmacro with-object (() &body body)
"Open a JSON object, then run BODY. Inside the body,
ENCODE-OBJECT-ELEMENT or WITH-OBJECT-ELEMENT must be called to encode
elements to the object. Must be called within an existing JSON
encoder context, see WITH-OUTPUT and WITH-OUTPUT-TO-STRING*."
`(with-aggregate (#\{ #\}) ,@body))
(defun encode-array-element (object)
"Encode OBJECT as next array element to the last JSON array opened
with WITH-ARRAY in the dynamic context. OBJECT is encoded using the
ENCODE generic function, so it must be of a type for which an ENCODE
method is defined."
(next-aggregate-element)
(encode object (output-stream *json-output*)))
(defun encode-array-elements (&rest objects)
"Encode OBJECTS, a list of JSON encodeable object, as array elements."
(dolist (object objects)
(encode-array-element object)))
(defun encode-object-element (key value)
"Encode KEY and VALUE as object element to the last JSON object
opened with WITH-OBJECT in the dynamic context. KEY and VALUE are
encoded using the ENCODE generic function, so they both must be of a
type for which an ENCODE method is defined."
(next-aggregate-element)
(encode-key/value key value (output-stream *json-output*))
value)
(defun encode-object-elements (&rest elements)
"Encode plist ELEMENTS as object elements."
(loop for (key value) on elements by #'cddr
do (encode-object-element key value)))
(defmacro with-object-element ((key) &body body)
"Open a new encoding context to encode a JSON object element. KEY
is the key of the element. The value will be whatever BODY
serializes to the current JSON output context using one of the
stream encoding functions. This can be used to stream out nested
object structures."
`(progn
(next-aggregate-element)
(encode ,key (output-stream *json-output*))
(setf (car (stack *json-output*)) #\:)
(unwind-protect
(progn ,@body)
(setf (car (stack *json-output*)) #\,))))
(defgeneric encode-slots (object)
(:method-combination progn)
(:documentation
"Generic function to encode objects. Every class in a hierarchy
implements a method for ENCODE-OBJECT that serializes its slots.
It is a PROGN generic function so that for a given instance, all
slots are serialized by invoking the ENCODE-OBJECT method for all
classes that it inherits from."))
(defgeneric encode-object (object)
(:documentation
"Encode OBJECT, presumably a CLOS object as a JSON object, invoking
the ENCODE-SLOTS method as appropriate.")
(:method (object)
(with-object ()
(json:encode-slots object))))