|
25 | 25 | (defparameter *symbol-key-encoder* 'encode-symbol-key-error
|
26 | 26 | "The actual function used to encode a SYMBOL when seen as a key.
|
27 | 27 | You might want ENCODE-SYMBOL-AS-LOWERCASE or
|
| 28 | + ENCODE-SYMBOL-AS-STRING here. |
| 29 | + The function returns just a string - the quotes are added when writing the key.") |
| 30 | + |
| 31 | +(defparameter *symbol-encoder* 'encode-symbol-error |
| 32 | + "The actual function used to encode a SYMBOL. |
| 33 | + You might want ENCODE-SYMBOL-AS-LOWERCASE or |
28 | 34 | ENCODE-SYMBOL-AS-STRING here.")
|
29 | 35 |
|
30 | 36 |
|
|
146 | 152 | (defmethod encode ((object list) &optional (stream *json-output*))
|
147 | 153 | (funcall *list-encoder* object stream))
|
148 | 154 |
|
| 155 | +(defmethod encode ((object symbol) &optional (stream *json-output*)) |
| 156 | + (let ((new (funcall *symbol-encoder* object))) |
| 157 | + (assert (stringp new)) |
| 158 | + (encode new stream)) |
| 159 | + ) |
149 | 160 |
|
150 | 161 | (defun encode-symbol-key-error (key)
|
151 | 162 | (declare (ignore key))
|
152 | 163 | (error "No policy for symbols as keys defined. ~
|
153 | 164 | Please check YASON:*SYMBOL-KEY-ENCODER*."))
|
154 | 165 |
|
| 166 | +(defun encode-symbol-error (key) |
| 167 | + (declare (ignore key)) |
| 168 | + (error "No policy for symbols as keys defined. ~ |
| 169 | + Please check YASON:*SYMBOL-ENCODER*.")) |
| 170 | + |
| 171 | + |
155 | 172 | (defun encode-symbol-as-lowercase (key)
|
156 | 173 | "Encodes a symbol KEY as a lowercase string.
|
157 | 174 | Ensure that there's no intentional lower-case character lost."
|
158 | 175 | (let ((name (symbol-name key)))
|
159 | 176 | (assert (notany #'lower-case-p name))
|
160 | 177 | (string-downcase name)))
|
161 | 178 |
|
162 |
| -(defun encode-symbol-as-string (sym &optional stream prefix) |
| 179 | +(defun encode-symbol-as-string (sym &optional prefix) |
163 | 180 | "Encodes a symbol SYM as string PACKAGE:SYMBOL-NAME.
|
164 | 181 | Always prints a double colon, as exportedness
|
165 | 182 | might not make sense for the receiver;
|
|
169 | 186 | (let ((*print-readably* t)
|
170 | 187 | (*package* (symbol-package sym)))
|
171 | 188 | (if (keywordp sym)
|
172 |
| - (format stream "~a~s" prefix sym) |
173 |
| - (format stream "~a~a::~s" |
| 189 | + (format nil "~a~s" |
| 190 | + (or prefix "") |
| 191 | + sym) |
| 192 | + (format nil "~a~a::~s" |
174 | 193 | (or prefix "")
|
175 | 194 | (package-name *package*)
|
176 | 195 | sym))))
|
|
0 commit comments