juliensf / mercury-json

A Mercury JSON library.
BSD 2-Clause "Simplified" License
20 stars 3 forks source link

Failed to convert JSON objects to Mercury maps #4

Closed dram closed 5 years ago

dram commented 5 years ago

When trying to convert JSON objects to Mercury maps, a confusing error will be reported, i.e.:

conversion to tree234.tree234(string, list.list(string)): argument is object, expected object

Following is demo code:

:- module test.

:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module json.

:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module string.

main(!IO) :-
    Result = json.from_json(
                 json.det_from_string(
                     "{\"foo\": [\"a\", \"b\"], \"bar\": [\"c\"]}")),
    ( Result = maybe.ok(Data : map(string, list(string))),
      io.write_line(Data, !IO)
    ; Result = maybe.error(Error),
      io.write_line(Error, !IO)
    ).

Tested Mercury version is:

Mercury Compiler, version rotd-2019-08-16, on x86_64-pc-linux-gnu
juliensf commented 5 years ago

Thanks for reporting that, the error message for that case is supposed to read:

argument is object, expected array

(It was also wrong for bimaps and retrees.). I've pushed a fix for it.