Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chrnybo
Created February 9, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrnybo/ed19dbc72665588cfce60a37e68934bf to your computer and use it in GitHub Desktop.
Save chrnybo/ed19dbc72665588cfce60a37e68934bf to your computer and use it in GitHub Desktop.
Make yason:encode emit property lists
(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