unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: swedebugia <swedebugia@riseup.net>
To: "Aleix Conchillo Flaqué" <aconchillo@gmail.com>,
	guile-user <guile-user@gnu.org>
Subject: Re: help with guile-json inconsistencies
Date: Mon, 10 Dec 2018 09:12:47 +0100	[thread overview]
Message-ID: <1407eaad-1fc8-f69f-a4dc-949af52ad5c9@riseup.net> (raw)
In-Reply-To: <CA+XASoVUYLXJGBdpiz-tq02FXsDqVO5=tbs0W1W7xDmW-vYrCQ@mail.gmail.com>

On 2018-12-10 08:27, Aleix Conchillo Flaqué wrote:
> On Sun, Dec 9, 2018 at 10:33 PM Aleix Conchillo Flaqué
> <aconchillo@gmail.com> wrote:
>>
>> On Sun, Dec 9, 2018 at 2:56 AM Aleix Conchillo Flaqué
>> <aconchillo@gmail.com> 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).

Nice to hear you solved it :)

Both Guix and guile-wikidata use guile-json but only from json->scm and 
that works flawlessly. Thanks for creating and maintaining this lib.

-- 
Cheers
Swedebugia



  reply	other threads:[~2018-12-10  8:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-09 10:56 help with guile-json inconsistencies Aleix Conchillo Flaqué
2018-12-10  6:33 ` Aleix Conchillo Flaqué
2018-12-10  7:27   ` Aleix Conchillo Flaqué
2018-12-10  8:12     ` swedebugia [this message]
2018-12-10 20:55     ` Alex Vong
2018-12-10 21:48       ` John Cowan
     [not found] <mailman.28267.1544480212.1283.guile-user@gnu.org>
2018-12-11  0:37 ` Zelphir Kaltstahl
2018-12-11  5:18   ` Aleix Conchillo Flaqué
2018-12-11  8:31     ` Zelphir Kaltstahl
2018-12-11 17:40       ` Aleix Conchillo Flaqué

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1407eaad-1fc8-f69f-a4dc-949af52ad5c9@riseup.net \
    --to=swedebugia@riseup.net \
    --cc=aconchillo@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).