diff --git a/src/jzon.lisp b/src/jzon.lisp index 296518a..8064ede 100644 --- a/src/jzon.lisp +++ b/src/jzon.lisp @@ -1121,22 +1121,30 @@ 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)))) + (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'. 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 +1155,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=))