Skip to content

Commit f1e6436

Browse files
committed
Switch to built-in json support
1 parent b9bb837 commit f1e6436

File tree

1 file changed

+33
-60
lines changed

1 file changed

+33
-60
lines changed

lisp/ghub.el

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ only serves as documentation.")
9595
(defvar ghub-insecure-hosts nil
9696
"List of hosts that use http instead of https.")
9797

98-
(defvar ghub-json-use-jansson nil
99-
"Whether to use the Jansson library, if available.
100-
This is experimental. Only let-bind this but do not enable it
101-
globally because doing that is likely to break other packages
102-
that use `ghub'. As a user also do not enable this yet.
103-
See https://github.com/magit/ghub/pull/149.")
104-
105-
(defvar ghub-json-object-type 'alist
106-
"The object type that is used for json payload decoding.")
107-
108-
(defvar ghub-json-array-type 'list
109-
"The array type that is used for json payload decoding.")
110-
11198
(defvar ghub-debug nil
11299
"Record additional debug information.")
113100

@@ -607,35 +594,25 @@ Signal an error if the id cannot be determined."
607594
url-http-response-status))
608595

609596
(defun ghub--read-json-payload (_status)
610-
(let ((raw (ghub--decode-payload)))
611-
(and raw
612-
(condition-case nil
613-
(if (and ghub-json-use-jansson
614-
(fboundp 'json-parse-string))
615-
(json-parse-string
616-
raw
617-
:object-type ghub-json-object-type
618-
:array-type ghub-json-array-type
619-
:false-object nil
620-
:null-object nil)
621-
(require 'json)
622-
(let ((json-object-type ghub-json-object-type)
623-
(json-array-type ghub-json-array-type)
624-
(json-false nil)
625-
(json-null nil))
626-
(json-read-from-string raw)))
627-
((json-parse-error json-readtable-error)
628-
(pop-to-buffer (current-buffer))
629-
(setq-local ghub-debug t)
630-
`((message
631-
. ,(if (looking-at "<!DOCTYPE html>")
632-
(if (re-search-forward
633-
"<p>\\(?:<strong>\\)?\\([^<]+\\)" nil t)
634-
(match-string 1)
635-
"error description missing")
636-
(string-trim (buffer-substring (point) (point-max)))))
637-
(documentation_url
638-
. "https://github.com/magit/ghub/wiki/Github-Errors")))))))
597+
(and-let* ((payload (ghub--decode-payload)))
598+
(ghub--assert-json-available)
599+
(condition-case nil
600+
(json-parse-string payload
601+
:object-type 'alist
602+
:array-type 'list
603+
:null-object nil
604+
:false-object nil)
605+
(json-parse-error
606+
(pop-to-buffer (current-buffer))
607+
(setq-local ghub-debug t)
608+
`((message . ,(if (looking-at "<!DOCTYPE html>")
609+
(if (re-search-forward
610+
"<p>\\(?:<strong>\\)?\\([^<]+\\)" nil t)
611+
(match-string 1)
612+
"error description missing")
613+
(string-trim (buffer-substring (point) (point-max)))))
614+
(documentation_url
615+
. "https://github.com/magit/ghub/wiki/Github-Errors"))))))
639616

640617
(defun ghub--decode-payload (&optional _status)
641618
(and (not (eobp))
@@ -644,24 +621,15 @@ Signal an error if the id cannot be determined."
644621
'utf-8)))
645622

646623
(defun ghub--encode-payload (payload)
647-
(and payload
648-
(progn
649-
(unless (stringp payload)
650-
(setq payload
651-
(if (and ghub-json-use-jansson
652-
(fboundp 'json-serialize))
653-
(json-serialize payload
654-
;; :object-type and :array-type
655-
;; are not supported here.
656-
:false-object nil
657-
:null-object :null)
658-
(require 'json)
659-
(let ((json-object-type ghub-json-object-type)
660-
(json-array-type ghub-json-array-type)
661-
(json-false nil)
662-
(json-null :null))
663-
(json-encode payload)))))
664-
(encode-coding-string payload 'utf-8))))
624+
(cl-typecase payload
625+
(null nil)
626+
(string (encode-coding-string payload 'utf-8))
627+
(t (ghub--assert-json-available)
628+
(encode-coding-string
629+
(json-serialize payload
630+
:null-object :null
631+
:false-object nil)
632+
'utf-8))))
665633

666634
(defun ghub--url-encode-params (params)
667635
(mapconcat (lambda (param)
@@ -673,6 +641,11 @@ Signal an error if the id cannot be determined."
673641
(t (url-hexify-string val))))))
674642
params "&"))
675643

644+
(defun ghub--assert-json-available ()
645+
(unless (and (fboundp 'json-available-p)
646+
(json-available-p))
647+
(error "Ghub requires Emacs 29 --with-json or Emacs >= 30")))
648+
676649
;;; Authentication
677650
;;;; API
678651

0 commit comments

Comments
 (0)