Skip to content

Commit 320a155

Browse files
committed
ENCODE for SYMBOLs indirected as well.
1 parent 636fc03 commit 320a155

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

doc.xml

+12
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ CL-USER> (let* ((yason:*parse-json-arrays-as-vectors* t)
384384
</clix:special-variable>
385385

386386

387+
<clix:special-variable name="*symbol-encoder*">
388+
<clix:description>
389+
Function to call to translate a CL symbol into a JSON string.
390+
The default is to error out, to provide backwards-compatible behaviour.
391+
<p>
392+
A useful function that can be bound to this variable is
393+
<clix:ref>YASON:ENCODE-SYMBOL-AS-LOWERCASE.</clix:ref>
394+
</p>
395+
</clix:description>
396+
</clix:special-variable>
397+
398+
387399
<clix:special-variable name="*symbol-key-encoder*">
388400
<clix:description>
389401
Defines the policy to encode symbols as keys (eg. in hash tables).

encode.lisp

+22-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
(defparameter *symbol-key-encoder* 'encode-symbol-key-error
2626
"The actual function used to encode a SYMBOL when seen as a key.
2727
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
2834
ENCODE-SYMBOL-AS-STRING here.")
2935

3036

@@ -146,20 +152,31 @@
146152
(defmethod encode ((object list) &optional (stream *json-output*))
147153
(funcall *list-encoder* object stream))
148154

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+
)
149160

150161
(defun encode-symbol-key-error (key)
151162
(declare (ignore key))
152163
(error "No policy for symbols as keys defined. ~
153164
Please check YASON:*SYMBOL-KEY-ENCODER*."))
154165

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+
155172
(defun encode-symbol-as-lowercase (key)
156173
"Encodes a symbol KEY as a lowercase string.
157174
Ensure that there's no intentional lower-case character lost."
158175
(let ((name (symbol-name key)))
159176
(assert (notany #'lower-case-p name))
160177
(string-downcase name)))
161178

162-
(defun encode-symbol-as-string (sym &optional stream prefix)
179+
(defun encode-symbol-as-string (sym &optional prefix)
163180
"Encodes a symbol SYM as string PACKAGE:SYMBOL-NAME.
164181
Always prints a double colon, as exportedness
165182
might not make sense for the receiver;
@@ -169,8 +186,10 @@
169186
(let ((*print-readably* t)
170187
(*package* (symbol-package sym)))
171188
(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"
174193
(or prefix "")
175194
(package-name *package*)
176195
sym))))

package.lisp

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#:encode-alist
3232
#:encode-plain-list-to-array
3333
#:*list-encoder*
34+
#:*symbol-encoder*
3435
#:*symbol-key-encoder*
3536
#:encode-symbol-as-lowercase
3637
#:encode-symbol-as-string

0 commit comments

Comments
 (0)