Created
February 9, 2019 14:21
-
-
Save chrnybo/ed19dbc72665588cfce60a37e68934bf to your computer and use it in GitHub Desktop.
Make yason:encode emit property lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmethod yason:encode :around ((object list) &optional (stream *standard-output*)) | |
"If *lists-as-plists*, encode OBJECT as plist | |
if object satisfies #'plistp and every key is a keyword. | |
Otherwise encode as list." | |
(if (and (boundp '*lists-as-plists*) | |
*lists-as-plists* | |
(system:plistp object) | |
(loop for key in object by 'cddr | |
always (keywordp key))) | |
(yason:encode-plist object stream) | |
(call-next-method))) | |
(let ((*lists-as-plists* t)) | |
(declare (special *lists-as-plists*)) | |
(yason:encode '(:foo 1 :bar ((:zot 2))))) | |
-> {"FOO":1,"BAR":[{"ZOT":2}]}(:FOO 1 :BAR ((:ZOT 2))) | |
(yason:encode '(:foo 1 :bar ((:zot 2)))) | |
-> ["FOO",1,"BAR",[["ZOT",2]]](:FOO 1 :BAR ((:ZOT 2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment