Subject: Re: bug#40693: 28.0.50; json-encode-alist changes alist

From: João Távora <[email-masked]>
Date: Tue, 19 May 2020 00:50:33 +0100

Hi Basil,

"Basil L. Contovounesios" <[email-masked]> writes:

> * lisp/jsonrpc.el (jsonrpc--json-read, jsonrpc--json-encode): Check
> whether native JSON functions are fboundp only once, at load time.

I welcome this small improvement: in fact I had a TODO there to make
something similar.  In the TODO i suggested the jsonrpc--json-read could
be a macro.  You used `defalias` instead and that's fine.  However, I
don't understand the need for the ugly (require 'json), defvar and
declare-function there.  Can't we just use sth like `eval-and-compile`
at the top of the file?  

> -(defun jsonrpc--json-read ()
> -  "Read JSON object in buffer, move point to end of buffer."
> -  ;; TODO: I guess we can make these macros if/when jsonrpc.el
> -  ;; goes into Emacs core.
> -  (cond ((fboundp 'json-parse-buffer) (json-parse-buffer
> -                                       :object-type 'plist
> -                                       :null-object nil
> -                                       :false-object :json-false))
> -        (t                            (let ((json-object-type 'plist))
> -                                        (json-read)))))
> +(defalias 'jsonrpc--json-read
> +  (if (fboundp 'json-parse-buffer)
> +      (lambda ()
> +        (json-parse-buffer :object-type 'plist
> +                           :null-object nil
> +                           :false-object :json-false))
> +    (require 'json)
> +    (defvar json-object-type)
> +    (declare-function json-read "json" ())
> +    (lambda ()
> +      (let ((json-object-type 'plist))
> +        (json-read))))
> +  "Read JSON object in buffer, move point to end of buffer.")

Thanks,
João