@@ -95,19 +95,6 @@ only serves as documentation.")
95
95
(defvar ghub-insecure-hosts nil
96
96
" List of hosts that use http instead of https." )
97
97
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
-
111
98
(defvar ghub-debug nil
112
99
" Record additional debug information." )
113
100
@@ -607,35 +594,25 @@ Signal an error if the id cannot be determined."
607
594
url-http-response-status))
608
595
609
596
(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" ))))))
639
616
640
617
(defun ghub--decode-payload (&optional _status )
641
618
(and (not (eobp ))
@@ -644,24 +621,15 @@ Signal an error if the id cannot be determined."
644
621
'utf-8 )))
645
622
646
623
(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 ))))
665
633
666
634
(defun ghub--url-encode-params (params )
667
635
(mapconcat (lambda (param )
@@ -673,6 +641,11 @@ Signal an error if the id cannot be determined."
673
641
(t (url-hexify-string val))))))
674
642
params " &" ))
675
643
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
+
676
649
; ;; Authentication
677
650
; ;;; API
678
651
0 commit comments