unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to test for native JSON support?
@ 2021-05-02 12:09 Joost Kremers
  2021-05-03  0:20 ` Nikolay Kudryavtsev
  0 siblings, 1 reply; 19+ messages in thread
From: Joost Kremers @ 2021-05-02 12:09 UTC (permalink / raw)
  To: gnu-emacs-help

Hi list,

What is the best way to find out in Elisp code if Emacs is compiled with native
JSON support? Can I simply test if the function `json-parse-buffer` exists?

TIA

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-02 12:09 How to test for native JSON support? Joost Kremers
@ 2021-05-03  0:20 ` Nikolay Kudryavtsev
  2021-05-03  8:21   ` Joost Kremers
  2021-05-03 19:42   ` Dmitry Gutov
  0 siblings, 2 replies; 19+ messages in thread
From: Nikolay Kudryavtsev @ 2021-05-03  0:20 UTC (permalink / raw)
  To: Joost Kremers, gnu-emacs-help

In my testing the safest way seems to be checking if (json-serialize 
‘((test . 1))) would return a non-nil value.




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03  0:20 ` Nikolay Kudryavtsev
@ 2021-05-03  8:21   ` Joost Kremers
  2021-05-03 14:38     ` Nikolay Kudryavtsev
  2021-05-03 19:42   ` Dmitry Gutov
  1 sibling, 1 reply; 19+ messages in thread
From: Joost Kremers @ 2021-05-03  8:21 UTC (permalink / raw)
  To: Nikolay Kudryavtsev; +Cc: help-gnu-emacs, gnu-emacs-help


On Mon, May 03 2021, Nikolay Kudryavtsev wrote:
> In my testing the safest way seems to be checking if (json-serialize ‘((test .
> 1))) would return a non-nil value.

Thanks. I had hoped there would be a canonical way of doing this... Guess not,
then.

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03  8:21   ` Joost Kremers
@ 2021-05-03 14:38     ` Nikolay Kudryavtsev
  2021-05-03 18:59       ` Joost Kremers
  0 siblings, 1 reply; 19+ messages in thread
From: Nikolay Kudryavtsev @ 2021-05-03 14:38 UTC (permalink / raw)
  To: Joost Kremers; +Cc: help-gnu-emacs

There is system-configuration-features variable, but it only reports 
features present at the compile time and does not guarantee that the 
feature you're looking for is actually working currently.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03 14:38     ` Nikolay Kudryavtsev
@ 2021-05-03 18:59       ` Joost Kremers
  0 siblings, 0 replies; 19+ messages in thread
From: Joost Kremers @ 2021-05-03 18:59 UTC (permalink / raw)
  To: Nikolay Kudryavtsev; +Cc: help-gnu-emacs


On Mon, May 03 2021, Nikolay Kudryavtsev wrote:
> There is system-configuration-features variable, but it only reports features
> present at the compile time and does not guarantee that the feature you're
> looking for is actually working currently.

Thanks, that's useful to know. 

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03  0:20 ` Nikolay Kudryavtsev
  2021-05-03  8:21   ` Joost Kremers
@ 2021-05-03 19:42   ` Dmitry Gutov
  2021-05-03 22:33     ` Nikolay Kudryavtsev
  1 sibling, 1 reply; 19+ messages in thread
From: Dmitry Gutov @ 2021-05-03 19:42 UTC (permalink / raw)
  To: Nikolay Kudryavtsev, Joost Kremers, gnu-emacs-help

On 03.05.2021 03:20, Nikolay Kudryavtsev wrote:
> In my testing the safest way seems to be checking if (json-serialize 
> ‘((test . 1))) would return a non-nil value.

Have you encountered any specific situations where the function was 
fbound, but failed to work?



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03 19:42   ` Dmitry Gutov
@ 2021-05-03 22:33     ` Nikolay Kudryavtsev
  2021-05-03 22:39       ` Dmitry Gutov
  2021-05-04 11:40       ` Tim Landscheidt
  0 siblings, 2 replies; 19+ messages in thread
From: Nikolay Kudryavtsev @ 2021-05-03 22:33 UTC (permalink / raw)
  To: Dmitry Gutov, Joost Kremers, gnu-emacs-help

json-serialize would return nil if libjansson library is not available 
during the execution time. So the scenario would be someone building 
Emacs with json support, then forgetting to put libjansson on his path 
and/or moving the binary to another machine. Not very likely to happen 
on any POSIX systems, but a more or less tangible case on Windows.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03 22:33     ` Nikolay Kudryavtsev
@ 2021-05-03 22:39       ` Dmitry Gutov
  2021-05-04 11:40       ` Tim Landscheidt
  1 sibling, 0 replies; 19+ messages in thread
From: Dmitry Gutov @ 2021-05-03 22:39 UTC (permalink / raw)
  To: Nikolay Kudryavtsev, Joost Kremers, gnu-emacs-help

On 04.05.2021 01:33, Nikolay Kudryavtsev wrote:
> json-serialize would return nil if libjansson library is not available 
> during the execution time. So the scenario would be someone building 
> Emacs with json support, then forgetting to put libjansson on his path 
> and/or moving the binary to another machine. Not very likely to happen 
> on any POSIX systems, but a more or less tangible case on Windows.

Windows-only, then. That makes sense. Thanks for the reply.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-03 22:33     ` Nikolay Kudryavtsev
  2021-05-03 22:39       ` Dmitry Gutov
@ 2021-05-04 11:40       ` Tim Landscheidt
  2021-05-04 12:16         ` Eli Zaretskii
  1 sibling, 1 reply; 19+ messages in thread
From: Tim Landscheidt @ 2021-05-04 11:40 UTC (permalink / raw)
  To: help-gnu-emacs

Nikolay Kudryavtsev <nikolay.kudryavtsev@gmail.com> wrote:

> json-serialize would return nil if libjansson library is not
> available during the execution time. So the scenario would
> be someone building Emacs with json support, then forgetting
> to put libjansson on his path and/or moving the binary to
> another machine. Not very likely to happen on any POSIX
> systems, but a more or less tangible case on Windows.

That's a rather unexpected, at least undocumented failure
mode (vulgo: a bug).  IMNSHO in that case json-serialize
should either "work" or throw an error.

Tim




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 11:40       ` Tim Landscheidt
@ 2021-05-04 12:16         ` Eli Zaretskii
  2021-05-04 12:29           ` Joost Kremers
  0 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-04 12:16 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Tim Landscheidt <tim@tim-landscheidt.de>
> Date: Tue, 04 May 2021 11:40:41 +0000
> 
> Nikolay Kudryavtsev <nikolay.kudryavtsev@gmail.com> wrote:
> 
> > json-serialize would return nil if libjansson library is not
> > available during the execution time. So the scenario would
> > be someone building Emacs with json support, then forgetting
> > to put libjansson on his path and/or moving the binary to
> > another machine. Not very likely to happen on any POSIX
> > systems, but a more or less tangible case on Windows.
> 
> That's a rather unexpected, at least undocumented failure
> mode (vulgo: a bug).  IMNSHO in that case json-serialize
> should either "work" or throw an error.

It actually displays an error message, in addition to returning nil
(as do all other JSON primitives in that case).



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 12:16         ` Eli Zaretskii
@ 2021-05-04 12:29           ` Joost Kremers
  2021-05-04 12:50             ` Eli Zaretskii
  0 siblings, 1 reply; 19+ messages in thread
From: Joost Kremers @ 2021-05-04 12:29 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On Tue, May 04 2021, Eli Zaretskii wrote:
>> From: Tim Landscheidt <tim@tim-landscheidt.de>
>> Date: Tue, 04 May 2021 11:40:41 +0000
>> 
>> Nikolay Kudryavtsev <nikolay.kudryavtsev@gmail.com> wrote:
>> 
>> > json-serialize would return nil if libjansson library is not
>> > available during the execution time. So the scenario would
>> > be someone building Emacs with json support, then forgetting
>> > to put libjansson on his path and/or moving the binary to
>> > another machine. Not very likely to happen on any POSIX
>> > systems, but a more or less tangible case on Windows.
>> 
>> That's a rather unexpected, at least undocumented failure
>> mode (vulgo: a bug).  IMNSHO in that case json-serialize
>> should either "work" or throw an error.
>
> It actually displays an error message, in addition to returning nil
> (as do all other JSON primitives in that case).

So should I wrap my call to `(json-serialize '((test . 1)))` in `ignore-errors`
if I don't want to worry unsuspecting users with a (possibly to them arcane)
error message? (Unfortunately, I don't have access to a Windows machine so I
can't test this myself.)

And is there a reason why the native json-functions do not gracefully degrade to
their counterparts in the `json.el` library? That seems like the logical thing
to do and would render testing for native JSON support unnecessary, but perhaps
there's a reason for not doing it.

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 12:29           ` Joost Kremers
@ 2021-05-04 12:50             ` Eli Zaretskii
  2021-05-04 13:06               ` Joost Kremers
  2021-05-04 13:17               ` Dmitry Gutov
  0 siblings, 2 replies; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-04 12:50 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Joost Kremers <joostkremers@fastmail.fm>
> Cc: help-gnu-emacs@gnu.org
> Date: Tue, 04 May 2021 14:29:53 +0200
> 
> > It actually displays an error message, in addition to returning nil
> > (as do all other JSON primitives in that case).
> 
> So should I wrap my call to `(json-serialize '((test . 1)))` in `ignore-errors`

It doesn't signal an error, just displays a message.  So it
effectively already does ignore-errors.

> if I don't want to worry unsuspecting users with a (possibly to them arcane)
> error message? (Unfortunately, I don't have access to a Windows machine so I
> can't test this myself.)

The message is "jansson library not found".

But to answer your question, you should allow the user to determine
whether the built-in or the Lisp implementation will be used, because
only the user knows whether the library is installed.

> And is there a reason why the native json-functions do not gracefully degrade to
> their counterparts in the `json.el` library?

First, because no one wrote the code to do that; patches welcome.
Second, because the implementations are not 100% equivalent, neither
in the API names nor in some aspects of the functionality.  So
automatic replacement is not trivial, not in general anyway.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 12:50             ` Eli Zaretskii
@ 2021-05-04 13:06               ` Joost Kremers
  2021-05-04 13:17               ` Dmitry Gutov
  1 sibling, 0 replies; 19+ messages in thread
From: Joost Kremers @ 2021-05-04 13:06 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On Tue, May 04 2021, Eli Zaretskii wrote:
> The message is "jansson library not found".
>
> But to answer your question, you should allow the user to determine
> whether the built-in or the Lisp implementation will be used, because
> only the user knows whether the library is installed.

I'm not sure every user will. I suspect many won't care either way, as long as
the relevant json file can be read.

>> And is there a reason why the native json-functions do not gracefully degrade
>> to
>> their counterparts in the `json.el` library?
>
> First, because no one wrote the code to do that; patches welcome.
> Second, because the implementations are not 100% equivalent, neither
> in the API names nor in some aspects of the functionality.  So
> automatic replacement is not trivial, not in general anyway.

Ok, I see. I haven't run into any functionality differences yet, but if and when
I do, I'll deal with them then. Then who knows, some day I might be able to
write that patch...

Thanks for your answer!

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 12:50             ` Eli Zaretskii
  2021-05-04 13:06               ` Joost Kremers
@ 2021-05-04 13:17               ` Dmitry Gutov
  2021-05-04 14:01                 ` Eli Zaretskii
  1 sibling, 1 reply; 19+ messages in thread
From: Dmitry Gutov @ 2021-05-04 13:17 UTC (permalink / raw)
  To: Eli Zaretskii, help-gnu-emacs

On 04.05.2021 15:50, Eli Zaretskii wrote:
> It doesn't signal an error, just displays a message.  So it
> effectively already does ignore-errors.

That sounds like a bug.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 13:17               ` Dmitry Gutov
@ 2021-05-04 14:01                 ` Eli Zaretskii
  2021-05-04 14:30                   ` Dmitry Gutov
  0 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-04 14:01 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Dmitry Gutov <dgutov@yandex.ru>
> Date: Tue, 4 May 2021 16:17:05 +0300
> 
> On 04.05.2021 15:50, Eli Zaretskii wrote:
> > It doesn't signal an error, just displays a message.  So it
> > effectively already does ignore-errors.
> 
> That sounds like a bug.

Feel free to suggest a patch, and let's see if you will be able to
convince The Powers That Be.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 14:01                 ` Eli Zaretskii
@ 2021-05-04 14:30                   ` Dmitry Gutov
  2021-05-04 14:32                     ` Philipp Stephani
  2021-05-04 15:16                     ` Eli Zaretskii
  0 siblings, 2 replies; 19+ messages in thread
From: Dmitry Gutov @ 2021-05-04 14:30 UTC (permalink / raw)
  To: Eli Zaretskii, help-gnu-emacs

On 04.05.2021 17:01, Eli Zaretskii wrote:

> Feel free to suggest a patch,

Like this?

diff --git a/src/json.c b/src/json.c
index 3f1d27ad7f..ece057ae41 100644
--- a/src/json.c
+++ b/src/json.c
@@ -596,8 +596,7 @@ DEFUN ("json-serialize", Fjson_serialize, 
Sjson_serialize, 1, MANY,
      }
    if (!json_initialized)
      {
-      message1 ("jansson library not found");
-      return Qnil;
+      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library 
not found")));
      }
  #endif

@@ -707,8 +706,7 @@ DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1, 
MANY,
      }
    if (!json_initialized)
      {
-      message1 ("jansson library not found");
-      return Qnil;
+      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library 
not found")));
      }
  #endif

@@ -966,8 +964,7 @@ DEFUN ("json-parse-string", Fjson_parse_string, 
Sjson_parse_string, 1, MANY,
      }
    if (!json_initialized)
      {
-      message1 ("jansson library not found");
-      return Qnil;
+      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library 
not found")));
      }
  #endif

@@ -1065,8 +1062,7 @@ DEFUN ("json-parse-buffer", Fjson_parse_buffer, 
Sjson_parse_buffer,
      }
    if (!json_initialized)
      {
-      message1 ("jansson library not found");
-      return Qnil;
+      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library 
not found")));
      }
  #endif


> and let's see if you will be able to
> convince The Powers That Be.

Praise the Powers! I pray they be convinced!



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 14:30                   ` Dmitry Gutov
@ 2021-05-04 14:32                     ` Philipp Stephani
  2021-05-04 14:41                       ` Dmitry Gutov
  2021-05-04 15:16                     ` Eli Zaretskii
  1 sibling, 1 reply; 19+ messages in thread
From: Philipp Stephani @ 2021-05-04 14:32 UTC (permalink / raw)
  To: Dmitry Gutov; +Cc: help-gnu-emacs

Am Di., 4. Mai 2021 um 16:31 Uhr schrieb Dmitry Gutov <dgutov@yandex.ru>:
>
> On 04.05.2021 17:01, Eli Zaretskii wrote:
>
> > Feel free to suggest a patch,
>
> Like this?
>
> diff --git a/src/json.c b/src/json.c
> index 3f1d27ad7f..ece057ae41 100644
> --- a/src/json.c
> +++ b/src/json.c
> @@ -596,8 +596,7 @@ DEFUN ("json-serialize", Fjson_serialize,
> Sjson_serialize, 1, MANY,
>       }
>     if (!json_initialized)
>       {
> -      message1 ("jansson library not found");
> -      return Qnil;
> +      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library
> not found")));
>       }
>   #endif
>
> @@ -707,8 +706,7 @@ DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1,
> MANY,
>       }
>     if (!json_initialized)
>       {
> -      message1 ("jansson library not found");
> -      return Qnil;
> +      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library
> not found")));
>       }
>   #endif
>
> @@ -966,8 +964,7 @@ DEFUN ("json-parse-string", Fjson_parse_string,
> Sjson_parse_string, 1, MANY,
>       }
>     if (!json_initialized)
>       {
> -      message1 ("jansson library not found");
> -      return Qnil;
> +      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library
> not found")));
>       }
>   #endif
>
> @@ -1065,8 +1062,7 @@ DEFUN ("json-parse-buffer", Fjson_parse_buffer,
> Sjson_parse_buffer,
>       }
>     if (!json_initialized)
>       {
> -      message1 ("jansson library not found");
> -      return Qnil;
> +      Fsignal (Qerror, list1 (build_unibyte_string ("jansson library
> not found")));
>       }
>   #endif
>

Can you make this signal a specific error symbol so that callers can
catch the error and react accordingly, e.g. by falling back to
json.el?



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 14:32                     ` Philipp Stephani
@ 2021-05-04 14:41                       ` Dmitry Gutov
  0 siblings, 0 replies; 19+ messages in thread
From: Dmitry Gutov @ 2021-05-04 14:41 UTC (permalink / raw)
  To: Philipp Stephani; +Cc: help-gnu-emacs

On 04.05.2021 17:32, Philipp Stephani wrote:

> Can you make this signal a specific error symbol so that callers can
> catch the error and react accordingly, e.g. by falling back to
> json.el?

I almost certainly can, and it does sound like a good idea.

I can't test the resulting patch, though (having no access to a Windows 
machine), and our Windows developers can better choose the symbol to use 
(AFAICT the situation with libraries possibly being absent at runtime is 
solely Windows-specific).

Step 2 might be to propagate the same error-signaling behavior to other 
features, like xml.c.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How to test for native JSON support?
  2021-05-04 14:30                   ` Dmitry Gutov
  2021-05-04 14:32                     ` Philipp Stephani
@ 2021-05-04 15:16                     ` Eli Zaretskii
  1 sibling, 0 replies; 19+ messages in thread
From: Eli Zaretskii @ 2021-05-04 15:16 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Dmitry Gutov <dgutov@yandex.ru>
> Date: Tue, 4 May 2021 17:30:28 +0300
> 
> On 04.05.2021 17:01, Eli Zaretskii wrote:
> 
> > Feel free to suggest a patch,
> 
> Like this?

Maybe, but not here.



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2021-05-04 15:16 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-02 12:09 How to test for native JSON support? Joost Kremers
2021-05-03  0:20 ` Nikolay Kudryavtsev
2021-05-03  8:21   ` Joost Kremers
2021-05-03 14:38     ` Nikolay Kudryavtsev
2021-05-03 18:59       ` Joost Kremers
2021-05-03 19:42   ` Dmitry Gutov
2021-05-03 22:33     ` Nikolay Kudryavtsev
2021-05-03 22:39       ` Dmitry Gutov
2021-05-04 11:40       ` Tim Landscheidt
2021-05-04 12:16         ` Eli Zaretskii
2021-05-04 12:29           ` Joost Kremers
2021-05-04 12:50             ` Eli Zaretskii
2021-05-04 13:06               ` Joost Kremers
2021-05-04 13:17               ` Dmitry Gutov
2021-05-04 14:01                 ` Eli Zaretskii
2021-05-04 14:30                   ` Dmitry Gutov
2021-05-04 14:32                     ` Philipp Stephani
2021-05-04 14:41                       ` Dmitry Gutov
2021-05-04 15:16                     ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).