Serialize the object to a plist

This commit is contained in:
Marcus Kammer 2024-10-02 19:54:35 +02:00
parent 42b08badb9
commit 7909196650
Signed by: marcuskammer
GPG key ID: C374817BE285268F

View file

@ -100,3 +100,16 @@
(duration :initarg :duration
:accessor object-duration
:documentation "When the object describes a time-bound resource, such as an audio or video, a meeting, etc, the duration property indicates the object's approximate duration.")))
(defgeneric to-plist (object)
(:documentation "Serialize an object to a plist. This function is independent from any JSON library, allowing serialization to a plist which can then be used to create a JSON string with any desired library."))
(defmethod to-plist ((obj object))
(loop :for slot :in (sb-mop:class-slots (class-of obj))
:for slot-name = (sb-mop:slot-definition-name slot)
:when (slot-boundp obj slot-name)
append (list (intern (symbol-name slot-name) "KEYWORD")
(let ((value (slot-value obj slot-name)))
(if (typep value 'object)
(to-plist value)
value)))))