aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/jsonrpc.el
diff options
context:
space:
mode:
authorJoão Távora2023-11-08 08:36:04 -0600
committerJoão Távora2023-12-02 02:28:38 -0600
commit71fc7020ff975de56005c815ed8e717770846ea8 (patch)
treed16df0fd1d678419178b1c44facc0a330d808553 /lisp/jsonrpc.el
parent5465d88b635dd8070b7d7b04e7522bf7e83cd85b (diff)
downloademacs-scratch/jsonrpc-things.tar.gz
emacs-scratch/jsonrpc-things.zip
Jsonrpc: support some JSONesque non-JSONRPC protocols, like DAPscratch/jsonrpc-things
* lisp/jsonrpc.el (jsonrpc-convert-from-jsonrpc) (jsonrpc-convert-to-jsonrpc): New generics. (jsonrpc-connection-receive, jsonrpc-connection-send): Call them.
Diffstat (limited to 'lisp/jsonrpc.el')
-rw-r--r--lisp/jsonrpc.el18
1 files changed, 15 insertions, 3 deletions
diff --git a/lisp/jsonrpc.el b/lisp/jsonrpc.el
index 2036e135f52..fc720da2a6e 100644
--- a/lisp/jsonrpc.el
+++ b/lisp/jsonrpc.el
@@ -131,6 +131,18 @@ immediately."
131 (:method (_s _what) ;; by default all connections are ready 131 (:method (_s _what) ;; by default all connections are ready
132 t)) 132 t))
133 133
134;;; API optional
135(cl-defgeneric jsonrpc-convert-from-jsonrpc (connection jsonrpc-message)
136 "Convert JSONRPC-MESSAGE to whatever JSON-esque thing the endpoint accepts.
137Return a plist."
138 (:method (_s jsonrpc-message) `(:jsonrpc "2.0" ,@jsonrpc-message)))
139
140;;; API optional
141(cl-defgeneric jsonrpc-convert-to-jsonrpc (connection remote-message)
142 "Convert JSON-esque REMOTE-MESSAGE to a JSONRPC message.
143Return a plist."
144 (:method (_s remote-message) remote-message))
145
134 146
135;;; Convenience 147;;; Convenience
136;;; 148;;;
@@ -167,10 +179,10 @@ circumvent that.")
167 "Process MESSAGE just received from CONNECTION. 179 "Process MESSAGE just received from CONNECTION.
168This function will destructure MESSAGE and call the appropriate 180This function will destructure MESSAGE and call the appropriate
169dispatcher in CONNECTION." 181dispatcher in CONNECTION."
182 (jsonrpc--log-event connection message 'server)
170 (cl-destructuring-bind (&key method id error params result _jsonrpc) 183 (cl-destructuring-bind (&key method id error params result _jsonrpc)
171 message 184 (jsonrpc-convert-to-jsonrpc connection message)
172 (let (continuations) 185 (let (continuations)
173 (jsonrpc--log-event connection message 'server)
174 (setf (jsonrpc-last-error connection) error) 186 (setf (jsonrpc-last-error connection) error)
175 (cond 187 (cond
176 (;; A remote request 188 (;; A remote request
@@ -446,7 +458,7 @@ connection object, called when the process dies.")
446 ((symbolp method) (symbol-name method)) 458 ((symbolp method) (symbol-name method))
447 ((stringp method) method) 459 ((stringp method) method)
448 (t (error "[jsonrpc] invalid method %s" method))))) 460 (t (error "[jsonrpc] invalid method %s" method)))))
449 (let* ( (message `(:jsonrpc "2.0" ,@args)) 461 (let* ( (message (jsonrpc-convert-from-jsonrpc connection args))
450 (json (jsonrpc--json-encode message)) 462 (json (jsonrpc--json-encode message))
451 (headers 463 (headers
452 `(("Content-Length" . ,(format "%d" (string-bytes json))) 464 `(("Content-Length" . ,(format "%d" (string-bytes json)))