Skip to content

Commit

Permalink
Allow (de)serialization of the :false value
Browse files Browse the repository at this point in the history
  • Loading branch information
notmgsk authored and stylewarning committed Jul 23, 2019
1 parent a82554c commit 2b90d41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src-tests/suite.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@
(is (string= (rpcq::|RPCWarning-body| warning)
(rpcq::|RPCWarning-body| cloned-warning)))
(is (string= (rpcq::|RPCWarning-kind| warning)
(rpcq::|RPCWarning-kind| cloned-warning)))))))
(rpcq::|RPCWarning-kind| cloned-warning))))))

(let ((rpcq::*use-false* nil))
(is (null (rpcq:deserialize (rpcq:serialize :false))))
(is (null (rpcq:deserialize (rpcq:serialize nil)))))

(let ((rpcq::*use-false* t))
(is (eql :false (rpcq:deserialize (rpcq:serialize :false))))
(is (null (rpcq:deserialize (rpcq:serialize nil))))))
18 changes: 12 additions & 6 deletions src/rpcq.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
;; store all messages defined thus far in their namespace
(defvar *messages* (make-hash-table :test 'equal))

;; Wrapper for messagepack's treatment of false/nil.
(defvar *use-false* t
"Discriminate between a false value and a nil value. If set to NIL both :FALSE and NIL with serialize and deserialize into NIL.")

(defun clear-messages ()
"Clear the stored message definitions."
(clrhash *messages*))
Expand Down Expand Up @@ -70,7 +74,8 @@ The input strings are assumed to be FORMAT-compatible, so sequences like ~<newli

(defun serialize (obj &optional stream)
"Serialize OBJ, either written to a stream or returned as a vector of (INTEGER 0 255)."
(let ((messagepack:*encode-alist-as-map* nil))
(let ((messagepack:*encode-alist-as-map* nil)
(messagepack::*use-false* *use-false*))
(etypecase stream
(stream
(messagepack:encode-stream (%serialize obj) stream))
Expand Down Expand Up @@ -132,11 +137,12 @@ The input strings are assumed to be FORMAT-compatible, so sequences like ~<newli

(defun deserialize (payload)
"Deserialize the object(s) encoded in PAYLOAD (string or stream)."
(etypecase payload
(vector
(%deserialize (messagepack:decode payload)))
(stream
(%deserialize (messagepack:decode-stream payload)))))
(let ((messagepack::*use-false* *use-false*))
(etypecase payload
(vector
(%deserialize (messagepack:decode payload)))
(stream
(%deserialize (messagepack:decode-stream payload))))))

(defun slot-type-and-initform (field-type required default)
"Translate a FIELD-TYPE to a Lisp type and initform taking into account
Expand Down

0 comments on commit 2b90d41

Please sign in to comment.