From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani Newsgroups: gmane.emacs.devel Subject: Re: JSON->lisp Mapping: Hash vs AList Date: Sat, 16 Dec 2017 22:24:35 +0000 Message-ID: References: <838te5uvee.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a11c012769d078505607c95f0" X-Trace: blaine.gmane.org 1513462981 25678 195.159.176.226 (16 Dec 2017 22:23:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 16 Dec 2017 22:23:01 +0000 (UTC) Cc: emacs-devel@gnu.org, raman@google.com To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Dec 16 23:22:57 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eQKr9-0006QT-CK for ged-emacs-devel@m.gmane.org; Sat, 16 Dec 2017 23:22:55 +0100 Original-Received: from localhost ([::1]:52361 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQKt6-0007mx-2i for ged-emacs-devel@m.gmane.org; Sat, 16 Dec 2017 17:24:56 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54816) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQKsz-0007mq-Fk for emacs-devel@gnu.org; Sat, 16 Dec 2017 17:24:50 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eQKsy-0006tB-Em for emacs-devel@gnu.org; Sat, 16 Dec 2017 17:24:49 -0500 Original-Received: from mail-qt0-x234.google.com ([2607:f8b0:400d:c0d::234]:45312) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eQKsw-0006sF-RY; Sat, 16 Dec 2017 17:24:46 -0500 Original-Received: by mail-qt0-x234.google.com with SMTP id g10so16092839qtj.12; Sat, 16 Dec 2017 14:24:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cQLXoA1a8A5RmDPCSd/LD3d++/WQt39FxL4eYKh3EdA=; b=Cn2Q1j2WSib33ORuwtt9Htpov3MkWG9JK+N23WNn4TphQhTyl9W47/SSYDnUid698y V2zBcolqoNqc2JvEbNR3AxhLTo/zbY0wUgCn3i4Y2HXM7pZkiaL9vyfYzKv+51k5h3Ry 9DbR+/Bwfa7z0tLvF49lIy4zirwBaTePaR8v7TXgzOt2EkfzWKSAV77y95CpfBTVo66Z j03m0qwiK9o9z48pKrFIf42iTZHT0FGxSU4PT6VCtUoGH6fHPeNVlohJAdnj0oAG+LcH NPMSdm8Hyqg40vYipeFaskAkxWbnc11Ty9StBTT/WuI0omvgRyyiR3ENrVYx3IiUh5Je +xTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cQLXoA1a8A5RmDPCSd/LD3d++/WQt39FxL4eYKh3EdA=; b=qHEFcS/KWgCOnsWHfcdFQjzYw1ArSzDnG59drGwZmtL6bxzYSCe8KNh3RP5RnV/hV+ QmLS4uDe8lAsGMugtMH9YOpfqAuPqiCcFwRrSgx9IdzuLHXPxypnnJBKbH+LDFl0Pfhw wdL8n7wo8rmh8a93CnA1Rtljh2CKVgXyQ+U3CBZIu4y7mrJ8CbdF7pjXGvDftOJmjYQw B0yLskSEO5U3zRZox8tGZAVDnN8Z57uYe+Jgh+odWBK4/9EQlWM30GTtHTdnhoXRHE1Z l9FFsF6RtYiyYc55jnKENp5vxBCFDY36b3kjNvMkF2XMRHVbp9USCqHUR42eJHP02VYN IV4g== X-Gm-Message-State: AKGB3mL3ulMJz8OzWQQzg/UUbJOevT4cVX++QSKZ+zkR/nIwkbTWty2F d7p9vRkdqwqzolMSRKu9jrc/fDwYLsGYS4A2MGQW6w== X-Google-Smtp-Source: ACJfBovU4Jnmzszhoo21SFUtOT56sKO4a9rPBjEt1P/2hTcztqKaZ61bmKpYioyG4YbZ5W1+LPGEyOBri0jN3XVjR+Y= X-Received: by 10.200.46.149 with SMTP id h21mr28043503qta.73.1513463085859; Sat, 16 Dec 2017 14:24:45 -0800 (PST) In-Reply-To: <838te5uvee.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::234 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:221153 Archived-At: --001a11c012769d078505607c95f0 Content-Type: text/plain; charset="UTF-8" Eli Zaretskii schrieb am Do., 14. Dez. 2017 um 17:28 Uhr: > > From: Philipp Stephani > > Date: Wed, 13 Dec 2017 22:37:46 +0000 > > Cc: emacs-devel@gnu.org > > > > Sounds reasonable, here is a patch. > > Thanks. > > > -@defun json-parse-string string > > +@defun json-parse-string string &key (object-type 'hash-table) > > Hmm.. why is there an apostrophe before "hash-table"? What do you > want to get in the output there? > An apostrophe? It seems to work as expected. > > And btw, I don't see "&key" mentioned anywhere in the ELisp manual, so > I wonder whether the reader will understand what it means. > This is the Common Lisp syntax, from cl-defun etc. It's a bit unfortunate that it's not used in Emacs core, even for functions that take keyword arguments such as `make-process'. I can switch to '&rest args' if you prefer that. > > > + case json_object_alist: > > + { > > + result = Qnil; > > + const char *key_str; > > + json_t *value; > > + json_object_foreach (json, key_str, value) > > + { > > + Lisp_Object key = Fintern (json_build_string > (key_str), Qnil); > > + result > > + = Fcons (Fcons (key, json_to_lisp (value, > object_type)), > > + result); > > + } > > + result = Fnreverse (result); > > Is there a reason for calling nreverse here? > It puts the elements in the same order as the original JSON. (The Jansson parser also retains the original order.) This isn't very important, just a bit nicer and less surprising. > > > +The keyword argument OBJECT-TYPE specifies which Lisp type is used to > ^^^^^^^^^^^ > Shouldn't that be `:object-type' (including quotes)? > Depending on whether we can use &key in a docstring in core. If so, then this one is correct, see e.g. the docstring of should-error. --001a11c012769d078505607c95f0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


Eli Za= retskii <eliz@gnu.org> schrieb am= Do., 14. Dez. 2017 um 17:28=C2=A0Uhr:
> From: Philipp Stephani <p.stephani2@gmail.com>
> Date: Wed, 13 Dec 2017 22:37:46 +0000
> Cc: emacs-dev= el@gnu.org
>
> Sounds reasonable, here is a patch.

Thanks.

> -@defun json-parse-string string
> +@defun json-parse-string string &key (object-type 'hash-table= )

Hmm.. why is there an apostrophe before "hash-table"?=C2=A0 What = do you
want to get in the output there?

An apo= strophe? It seems to work as expected.
=C2=A0

And btw, I don't see "&key" mentioned anywhere in the ELi= sp manual, so
I wonder whether the reader will understand what it means.
=

This is the Common Lisp syntax, from cl-defun etc. It&#= 39;s a bit unfortunate that it's not used in Emacs core, even for funct= ions that take keyword arguments such as `make-process'. I can switch t= o '&rest args' if you prefer that.
=C2=A0

> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 case json_object_alist:
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 result =3D Qnil;
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 const char *key_str;=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 json_t *value;
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 json_object_foreach = (json, key_str, value)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Lisp_O= bject key =3D Fintern (json_build_string (key_str), Qnil);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 result=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =3D Fcons (Fcons (key, json_to_lisp (value, object_type)),
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0result);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 result =3D Fnreverse= (result);

Is there a reason for calling nreverse here?

It puts the elements in the same order as the original JSON. (The Jan= sson parser also retains the original order.)
This isn't very= important, just a bit nicer and less surprising.
=C2=A0

> +The keyword argument OBJECT-TYPE specifies which Lisp type is used to=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 ^^^^^^^^^^^
Shouldn't that be `:object-type' (including quotes)?

Depending on whether we can use &key in a docstri= ng in core. If so, then this one is correct, see e.g. the docstring of shou= ld-error.=C2=A0
--001a11c012769d078505607c95f0--