Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize rationals as floats #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src-tests/suite.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@
(deftest test-serialize-deserialize ()
(let* ((original (make-instance 'rpcq::|RPCRequest|
:|method| "test-method"
:|params| (make-hash-table)
:|params| (alexandria:alist-hash-table
'(("one-half" . 1/2)))
:|id| "test-id"))
(cloned (rpcq::deserialize (rpcq::serialize original))))
(is (typep cloned 'rpcq::|RPCRequest|))
(is (string= (rpcq::|RPCRequest-id| original) (rpcq::|RPCRequest-id| cloned)))
(is (string= (rpcq::|RPCRequest-method| original) (rpcq::|RPCRequest-method| cloned)))))
(is (string= (rpcq::|RPCRequest-method| original) (rpcq::|RPCRequest-method| cloned)))
(is (= 0.5 (gethash "one-half" (rpcq::|RPCRequest-params| cloned))))))
3 changes: 3 additions & 0 deletions src/rpcq.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ The input strings are assumed to be FORMAT-compatible, so sequences like ~<newli
(defmethod %serialize ((payload string))
payload)

(defmethod %serialize ((payload rational))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about specializing on ratio instead of rational?

(%serialize (float payload)))

(defmethod %serialize ((payload hash-table))
(let ((hash (make-hash-table :test #'equal)))
(loop :for k :being :the :hash-keys :of payload
Expand Down