bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Basil L. Contovounesios
Subject: bug#40693: 28.0.50; json-encode-alist changes alist
Date: Thu, 21 May 2020 22:14:20 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

João Távora <joaotavora@gmail.com> writes:

> "Basil L. Contovounesios" <contovob@tcd.ie> 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.

Thanks.  Indeed I don't think a macro is necessary here for now.

> 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?

The declarations are needed because the byte-compiler does not know that
loading json.el will e.g. define a dynamically bound variable
json-object-type and a nullary function symbol json-read.  It therefore
not only complains but also generates invalid byte-code.

One way to avoid having to write these declarations is e.g.:

(defalias 'jsonrpc--json-read
  (if (fboundp 'json-parse-buffer)
      (lambda () ...)
    (eval-and-compile
      (require 'json))
    (lambda () ...))
  ...)

But I find this more heavy handed and intrusive, since it
unconditionally loads json.el during byte-compilation, even when
json-parse-buffer is available.

If you prefer this approach as a matter of style I'll push the patch
with the corresponding changes made.

>> -(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,

-- 
Basil





reply via email to

[Prev in Thread] Current Thread [Next in Thread]