From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alex Vong Newsgroups: gmane.lisp.guile.user Subject: Re: help with guile-json inconsistencies Date: Tue, 11 Dec 2018 04:55:07 +0800 Message-ID: <87efapgl04.fsf@gmail.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" X-Trace: blaine.gmane.org 1544475239 20343 195.159.176.226 (10 Dec 2018 20:53:59 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 10 Dec 2018 20:53:59 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: guile-user To: Aleix Conchillo =?utf-8?Q?Flaqu=C3=A9?= Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Dec 10 21:53:55 2018 Return-path: Envelope-to: guile-user@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 1gWSYt-0005DL-Bi for guile-user@m.gmane.org; Mon, 10 Dec 2018 21:53:55 +0100 Original-Received: from localhost ([::1]:34727 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWSb0-00023k-1f for guile-user@m.gmane.org; Mon, 10 Dec 2018 15:56:06 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWSaM-00022c-T6 for guile-user@gnu.org; Mon, 10 Dec 2018 15:55:28 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWSaI-0005y4-HR for guile-user@gnu.org; Mon, 10 Dec 2018 15:55:26 -0500 Original-Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]:39258) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gWSaI-0005tU-7g for guile-user@gnu.org; Mon, 10 Dec 2018 15:55:22 -0500 Original-Received: by mail-pl1-x635.google.com with SMTP id 101so5807441pld.6 for ; Mon, 10 Dec 2018 12:55:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version; bh=6fgxKyoU21vVLVeoN09yk2U8KADqXERHOX16pYDapgg=; b=JdoFFL7/kYmMMETVt91QS2yVcBVONLL1BSj6hwJVex+qRhRj3sx11rSZ+O5pt2oDMK yCjr5ejspvwJXMmFUsAf4GF80+xw9QMiEvpZSUr/eghbTKwXFayYTd4FK/pn7vRpPzg/ EnjXvSh1cSALtw0CYV8HbgHOYpavZm/lBSFh9SgvPZh3G8UE7xu0Jp7GOtuiAjmM9tdm hdA5oaZwNE7ZXc/G2RzEmc3/vi6LXxf3JjHaSoF86DwxFm7asYRsA3JaaG2c20TxbAAb BYv8oDBCxDSCKkE8OPYmQ9Fj2Efn9kPa5ZY1v97BwL32n/OD4MhV9C1xlabyCqFJxzDh 5X0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:cc:subject:in-reply-to:references :user-agent:date:message-id:mime-version; bh=6fgxKyoU21vVLVeoN09yk2U8KADqXERHOX16pYDapgg=; b=iPG+Ffue+reVSPLYf8HwKziWmxdTPb91XsapqWe9+dQos9ElzjOTUG0xQGvzM2tyO7 jdfR+5EaQEWlsjkym9JXg5+iHADBNG7/xfsoNcicQfpRIDpydrhvgaK0ERX6IsXMXomg OR0pKOyZ0ULQluy996GYLMioCg/1peJpPVzErnKAliP1n4ent402aW+QmVFN+Zk20lMC GcG7juy37/EEZvDZ9qUJJGd2GcniNvTJ6emU1SmCF0umcnTGI7YfDqRZhvoceO1HdMHC L4f/obEm5hGUIRJyb/A7Zcu5iEHZuLdJCxZmADHNWEwQOs0oXURkx6Lsz0JdtRbp5UeA ggNQ== X-Gm-Message-State: AA+aEWYS8fWaMiLL/D++pjAbL4Kor7XXLFAh1LyFSVWuXqC2pJk0xz1m DdLN5ngg5KI6eLcu6WDFWN0= X-Google-Smtp-Source: AFSGD/WiEJcw218WSv42MhujeKuZX4gh90TBwDisTdS5XqlVIN4ePwS/NvGrfKEMw6Xs9dMRzOKTYA== X-Received: by 2002:a17:902:8a91:: with SMTP id p17mr13461725plo.316.1544475316399; Mon, 10 Dec 2018 12:55:16 -0800 (PST) Original-Received: from debian (42-3-197-205.static.netvigator.com. [42.3.197.205]) by smtp.gmail.com with ESMTPSA id i72sm18408014pfe.181.2018.12.10.12.55.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 10 Dec 2018 12:55:15 -0800 (PST) In-Reply-To: ("Aleix Conchillo \=\?utf-8\?Q\?Flaqu\=C3\=A9\=22's\?\= message of "Sun, 9 Dec 2018 23:27:34 -0800") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::635 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:15045 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello Aleix, Aleix Conchillo Flaqu=C3=A9 writes: > On Sun, Dec 9, 2018 at 10:33 PM Aleix Conchillo Flaqu=C3=A9 > wrote: >> >> On Sun, Dec 9, 2018 at 2:56 AM Aleix Conchillo Flaqu=C3=A9 >> wrote: >> > >> > Hi guilers, >> > >> > a new guile-json issue came to me recently: >> > >> > https://github.com/aconchillo/guile-json/issues/22 >> > >> > I don't really have a solution yet on how to solve this, so I would >> > love some advice. The issue is that one can do: >> > >> > (scm->json '(1 2 3)) which returns [1,2,3] >> > >> > however, if you do: >> > >> > (scm->json '((1 2 3)) it returns {"1":[2,3]} but one would probably >> > expect [[1,2,3]] >> > (scm->json '((1 2 3) (4 5 6)) it returns {"1":[2,3], "4":[5,6]} but >> > one would probably expect [[1,2,3], [4,5,6]] >> > >> > I guess the first question is, does everyone else feel the same way >> > about the return value? >> > >> > The problem is that there's a function that checks if something is an >> > alist by basically checking if it's a pair? recursively and if so, it >> > builds a json object. >> > >> >> Just to clarify, the problem is not when parsing JSON to scheme data >> types, this is fine as internally guile-json uses hash tables and >> lists properly. >> >> The problem is with going from scheme to json. For convenience, >> guile-json doesn't expect a hash table from the user, instead it tries >> to guess what's a list and what's an alist however, that seems not >> really distinguishable. That is, there's no way to tell if '((1 2)) is >> [[1, 2]] or {"1" : [2]} or {"1" : "2"}. >> >> I guess going back to a macro approach for these type of cases would >> help (even though I had the same problem with my initial macro). >> > > Answering myself again. Re-adding the macro fixes the issue, but with > a breaking change. > > (scm->json '(1 2 3)) > -> [1,2,3] > > (scm->json '((1 2 3) (4 5 6))) > -> [[1,2,3],[4,5,6]] > > (scm->json (json (object ("key1" "value1") ("key2" (1 2 3))))) > -> {"key2":[1,2,3],"key1":"value1"} > > (scm->json `((1 2 3) ,(json (object (4 5))))) > -> [[1,2,3],{"4":5}] > > So, the (json) macro is only needed to specify JSON objects, otherwise > you can use scheme types directly (and lists are always arrays). > Another ambuiguity would be that both [] and {} corresponds to (). It seems to me that the core of this problem is that the Pair, List and Alist (which is a List of Pairs) types in scheme are not orthogonal, i.e. () are both List and Alist, and (1 2 3) are both List and Pair. Therefore, one fix would be to make Vector instead of List to corresponds to Json Array, but maybe this change will cause too much breakage, so I am not sure it is practical. Another way would be to make some other type other than Alist to corresponds to Json Object. If we were in Clojure, it would be Hash Map. I think VHash is a good candidate, but I am not happy with it not having a read/write syntax (Hash Table has the same problem too) which makes it not as flexible as Alist. (Actually, I had been trying to write a reader macro for VHash (w/ or w/o the Guile Reader library) but I didn't succeed in doing so because of my limited knowledge in how lexing works in Guile.) > Aleix Cheers, Alex --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQQwb8uPLAHCXSnTBVZh71Au9gJS8gUCXA7SqwAKCRBh71Au9gJS 8omqAP0Yxy5m8afof6Db8//6G7b/Q2ZFr88p6h3q629YassKOAEA2enhXMdA6IPT hqcky/B5pqyznP2j8vbjAVexzv0Bjws= =tAnd -----END PGP SIGNATURE----- --=-=-=--