GNU bug report logs -
#40693
28.0.50; json-encode-alist changes alist
Previous Next
Reported by: Ivan Andrus <darthandrus <at> gmail.com>
Date: Sat, 18 Apr 2020 03:01:02 UTC
Severity: normal
Tags: fixed, patch
Found in version 28.0.50
Fixed in version 28.1
Done: "Basil L. Contovounesios" <contovob <at> tcd.ie>
Bug is archived. No further changes may be made.
Full log
Message #103 received at 40693 <at> debbugs.gnu.org (full text, mbox):
João Távora <joaotavora <at> gmail.com> writes:
> "Basil L. Contovounesios" <contovob <at> 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
This bug report was last modified 5 years and 58 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.