[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/jsonrpc-things 71fc7020ff9 2/2: Jsonrpc: support some JSONesque
From: |
João Távora |
Subject: |
scratch/jsonrpc-things 71fc7020ff9 2/2: Jsonrpc: support some JSONesque non-JSONRPC protocols, like DAP |
Date: |
Sat, 2 Dec 2023 03:28:52 -0500 (EST) |
branch: scratch/jsonrpc-things
commit 71fc7020ff975de56005c815ed8e717770846ea8
Author: João Távora <joaotavora@gmail.com>
Commit: João Távora <joaotavora@gmail.com>
Jsonrpc: support some JSONesque non-JSONRPC protocols, like DAP
* lisp/jsonrpc.el (jsonrpc-convert-from-jsonrpc)
(jsonrpc-convert-to-jsonrpc): New generics.
(jsonrpc-connection-receive, jsonrpc-connection-send): Call them.
---
lisp/jsonrpc.el | 18 +++++++++++++++---
1 file 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."
(:method (_s _what) ;; by default all connections are ready
t))
+;;; API optional
+(cl-defgeneric jsonrpc-convert-from-jsonrpc (connection jsonrpc-message)
+ "Convert JSONRPC-MESSAGE to whatever JSON-esque thing the endpoint accepts.
+Return a plist."
+ (:method (_s jsonrpc-message) `(:jsonrpc "2.0" ,@jsonrpc-message)))
+
+;;; API optional
+(cl-defgeneric jsonrpc-convert-to-jsonrpc (connection remote-message)
+ "Convert JSON-esque REMOTE-MESSAGE to a JSONRPC message.
+Return a plist."
+ (:method (_s remote-message) remote-message))
+
;;; Convenience
;;;
@@ -167,10 +179,10 @@ circumvent that.")
"Process MESSAGE just received from CONNECTION.
This function will destructure MESSAGE and call the appropriate
dispatcher in CONNECTION."
+ (jsonrpc--log-event connection message 'server)
(cl-destructuring-bind (&key method id error params result _jsonrpc)
- message
+ (jsonrpc-convert-to-jsonrpc connection message)
(let (continuations)
- (jsonrpc--log-event connection message 'server)
(setf (jsonrpc-last-error connection) error)
(cond
(;; A remote request
@@ -446,7 +458,7 @@ connection object, called when the process dies.")
((symbolp method) (symbol-name method))
((stringp method) method)
(t (error "[jsonrpc] invalid method %s" method)))))
- (let* ( (message `(:jsonrpc "2.0" ,@args))
+ (let* ( (message (jsonrpc-convert-from-jsonrpc connection args))
(json (jsonrpc--json-encode message))
(headers
`(("Content-Length" . ,(format "%d" (string-bytes json)))