From c7427fbe8046541be1b334088a92138c2a19689a Mon Sep 17 00:00:00 2001 From: Daniel Nussenbaum Date: Sun, 3 Nov 2024 11:39:41 +0200 Subject: [PATCH 1/2] Unnecessary use of macro. Changing macro usage to generic function. --- src/jzon.lisp | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/jzon.lisp b/src/jzon.lisp index 296518a..867e37e 100644 --- a/src/jzon.lisp +++ b/src/jzon.lisp @@ -1121,22 +1121,24 @@ see `close-parser'" (with-open-file (in in :direction :input :external-format :utf-8 :element-type 'character) (parse in :max-depth max-depth :allow-comments allow-comments :allow-trailing-comma allow-trailing-comma :allow-multiple-content allow-multiple-content :max-string-length max-string-length :key-fn key-fn))) (t - (multiple-value-bind (%step %read-string %pos) (%make-fns in max-string-length) - (declare (dynamic-extent %step %read-string %pos)) - (%parse %step %read-string %pos key-fn max-depth (and allow-comments t) (and allow-trailing-comma t) (and allow-multiple-content t))))))) - -(macrolet ((%coerced-fields-slots (element) - `(let ((class (class-of ,element))) - (c2mop:ensure-finalized class) - (mapcar (lambda (s) - (let ((slot-name (c2mop:slot-definition-name s))) - (list slot-name - (slot-value ,element slot-name) - (c2mop:slot-definition-type s)))) - (remove-if-not (lambda (s) (slot-boundp ,element (c2mop:slot-definition-name s))) - (c2mop:class-slots class)))))) - (defgeneric coerced-fields (element) - (:documentation "Return a list of key definitions for `element'. + (multiple-value-bind (%step %read-string %pos) (%make-fns in max-string-length) + (declare (dynamic-extent %step %read-string %pos)) + (%parse %step %read-string %pos key-fn max-depth (and allow-comments t) (and allow-trailing-comma t) (and allow-multiple-content t))))))) + +(defgeneric %coerced-fields-slots (element)) +(defmethod %coerced-fields-slots (element) + (let ((class (class-of element))) + (c2mop:ensure-finalized class) + (mapcar (lambda (s) + (let ((slot-name (c2mop:slot-definition-name s))) + (list slot-name + (slot-value element slot-name) + (c2mop:slot-definition-type s)))) + (remove-if-not (lambda (s) (slot-boundp element (c2mop:slot-definition-name s))) + (c2mop:class-slots class))))) + +(defgeneric coerced-fields (element) + (:documentation "Return a list of key definitions for `element'. A key definition is a three-element list of the form (name value &optional type) name is the key name and will be coerced if not already a string @@ -1147,13 +1149,13 @@ Example return value: ((name :zulu) (hobbies nil list)) ") - (:method (element) - nil) - #+(or ccl clisp sbcl lispworks8) - (:method ((element structure-object)) - (%coerced-fields-slots element)) - (:method ((element standard-object)) - (%coerced-fields-slots element)))) + (:method (element) + nil) + #+(or ccl clisp sbcl lispworks8) + (:method ((element structure-object)) + (%coerced-fields-slots element)) + (:method ((element standard-object)) + (%coerced-fields-slots element))) (eval-when (:compile-toplevel :load-toplevel :execute) (declaim (inline %type=)) From b7fb3b97549957de27692c64871999c4cbd27d94 Mon Sep 17 00:00:00 2001 From: Daniel Nussenbaum Date: Sun, 3 Nov 2024 12:37:12 +0200 Subject: [PATCH 2/2] Allow extending the dispatch based on the metaclass of the object --- src/jzon.lisp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/jzon.lisp b/src/jzon.lisp index 867e37e..8064ede 100644 --- a/src/jzon.lisp +++ b/src/jzon.lisp @@ -1134,8 +1134,14 @@ see `close-parser'" (list slot-name (slot-value element slot-name) (c2mop:slot-definition-type s)))) - (remove-if-not (lambda (s) (slot-boundp element (c2mop:slot-definition-name s))) - (c2mop:class-slots class))))) + (get-slots-to-encode class element)))) + +(defgeneric get-slots-to-encode (class element) + (:documentation "This function is used to get the slots to be json encoded. The class is the first parameter in order to allow for extending the dispatch based on the metaclass of the object.")) + +(defmethod get-slots-to-encode (class element) + (remove-if-not (lambda (s) (slot-boundp element (c2mop:slot-definition-name s))) + (c2mop:class-slots class))) (defgeneric coerced-fields (element) (:documentation "Return a list of key definitions for `element'.