Skip to content

Commit 75b3ac5

Browse files
committed
Switch to built-in json support
1 parent 9ebb49d commit 75b3ac5

File tree

1 file changed

+33
-54
lines changed

1 file changed

+33
-54
lines changed

lisp/ghub.el

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +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-
10598
(defvar ghub-debug nil
10699
"Record additional debug information.")
107100

@@ -601,35 +594,25 @@ Signal an error if the id cannot be determined."
601594
url-http-response-status))
602595

603596
(defun ghub--read-json-payload (_status)
604-
(let ((raw (ghub--decode-payload)))
605-
(and raw
606-
(condition-case nil
607-
(if (and ghub-json-use-jansson
608-
(fboundp 'json-parse-string))
609-
(json-parse-string
610-
raw
611-
:object-type 'alist
612-
:array-type 'list
613-
:false-object nil
614-
:null-object nil)
615-
(require 'json)
616-
(let ((json-object-type 'alist)
617-
(json-array-type 'list)
618-
(json-false nil)
619-
(json-null nil))
620-
(json-read-from-string raw)))
621-
((json-parse-error json-readtable-error)
622-
(pop-to-buffer (current-buffer))
623-
(setq-local ghub-debug t)
624-
`((message
625-
. ,(if (looking-at "<!DOCTYPE html>")
626-
(if (re-search-forward
627-
"<p>\\(?:<strong>\\)?\\([^<]+\\)" nil t)
628-
(match-string 1)
629-
"error description missing")
630-
(string-trim (buffer-substring (point) (point-max)))))
631-
(documentation_url
632-
. "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"))))))
633616

634617
(defun ghub--decode-payload (&optional _status)
635618
(and (not (eobp))
@@ -638,24 +621,15 @@ Signal an error if the id cannot be determined."
638621
'utf-8)))
639622

640623
(defun ghub--encode-payload (payload)
641-
(and payload
642-
(progn
643-
(unless (stringp payload)
644-
(setq payload
645-
(if (and ghub-json-use-jansson
646-
(fboundp 'json-serialize))
647-
(json-serialize payload
648-
;; :object-type and :array-type
649-
;; are not supported here.
650-
:false-object nil
651-
:null-object :null)
652-
(require 'json)
653-
(let ((json-object-type ghub-json-object-type)
654-
(json-array-type ghub-json-array-type)
655-
(json-false nil)
656-
(json-null :null))
657-
(json-encode payload)))))
658-
(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))))
659633

660634
(defun ghub--url-encode-params (params)
661635
(mapconcat (lambda (param)
@@ -667,6 +641,11 @@ Signal an error if the id cannot be determined."
667641
(t (url-hexify-string val))))))
668642
params "&"))
669643

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+
670649
;;; Authentication
671650
;;;; API
672651

0 commit comments

Comments
 (0)