Be less ambiguous when encoding/decoding messages

- Do not handle nil as an empty dictionary when encoding.

- Use the default `json-array-type`
This commit is contained in:
Nathaniel Nicandro 2018-05-22 21:43:08 -05:00
parent 554e519bf0
commit 17596ee1c4
4 changed files with 23 additions and 21 deletions

View file

@ -336,15 +336,12 @@ the ROUTING-ID of the socket. Return the created socket."
(defun jupyter-read-plist (file)
"Read a JSON encoded FILE as a property list."
(let ((json-object-type 'plist)
(json-array-type 'list)
(json-false nil))
(let ((json-object-type 'plist))
(json-read-file file)))
(defun jupyter-read-plist-from-string (string)
(let ((json-object-type 'plist)
;; TODO: See the comment in `jupyter--decode'
(json-array-type 'list))
"Read a property list from a JSON encoded STRING."
(let ((json-object-type 'plist))
(json-read-from-string string)))
(provide 'jupyter-base)

View file

@ -200,7 +200,7 @@ kernel. Starting a kernel involves the following steps:
(proc (jupyter--start-kernel
manager kernel-name (plist-get spec :env)
(cl-loop
for arg in (plist-get spec :argv)
for arg in (append (plist-get spec :argv) nil)
if (equal arg "{connection_file}")
collect conn-file
else if (equal arg "{resource_dir}")

View file

@ -91,8 +91,7 @@ They are all set to appropriate default values."
(cl-letf (((symbol-function 'json-encode)
(lambda (object)
(cond ((memq object (list t json-null json-false))
(if (eq object json-null) "{}"
(json-encode-keyword object)))
(json-encode-keyword object))
((stringp object) (json-encode-string object))
((keywordp object)
;; Handle `jupyter-message-type'
@ -107,14 +106,16 @@ They are all set to appropriate default values."
((listp object) (json-encode-list object))
(t (signal 'json-error (list object)))))))
(encode-coding-string
(if (stringp object) object (json-encode-plist object))
(cond
((stringp object) object)
;; FIXME: This seems expensive
((json-plist-p object) (json-encode-plist object))
(t (json-encode object)))
'utf-8 t)))
(defun jupyter--decode (str)
(setq str (decode-coding-string str 'utf-8))
(let* ((json-object-type 'plist)
(json-array-type 'list)
(json-false nil)
(val (condition-case nil
(json-read-from-string str)
;; If it can't be read as JSON, assume its just a regular
@ -233,7 +234,9 @@ They are all set to appropriate default values."
code
(silent nil)
(store-history t)
(user-expressions nil)
;; This needs to be an empty
;; dictionary is not specified
(user-expressions #s(hash-table))
(allow-stdin t)
(stop-on-error nil))
(cl-check-type code string)

View file

@ -957,7 +957,7 @@ lines then truncate it to something less than
(defun jupyter-repl--handle-payload (payload)
"Do the client actions in PAYLOAD."
(cl-loop
for pl in payload
for pl across payload
do (pcase (plist-get pl :source)
("page"
(let ((text (plist-get (plist-get pl :data) :text/plain))
@ -1226,7 +1226,8 @@ REPL buffer."
(cl-defmethod jupyter-handle-history-reply ((client jupyter-repl-client) _req history)
(with-jupyter-repl-buffer client
(cl-loop for (_session _line-number input-output) in history
(cl-loop for elem across history
for input-output = (aref elem 2)
do (ring-remove+insert+extend jupyter-repl-history input-output))))
(cl-defmethod jupyter-handle-is-complete-reply ((client jupyter-repl-client) _req status indent)
@ -1480,8 +1481,9 @@ value that PREFIX takes.
This function constructs candidates assuming that `company-mode'
is used for completion."
(let ((types (plist-get metadata :_jupyter_types_experimental))
(let* ((matches (append matches nil))
(tail matches)
(types (append (plist-get metadata :_jupyter_types_experimental) nil))
;; TODO: Handle the case when the matches are method signatures in the
;; Julia kernel. This information would be useful for doing some kind
;; of eldoc like feature.