Giter VIP home page Giter VIP logo

mercury-json's People

Contributors

juliensf avatar opturion-jfischer avatar sebgod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mercury-json's Issues

Unused imports warning

Following is a warning message when compiling mercury-json with --warn-unused-imports enabled.

mercury-json/src/json.m:1023: In module `json':
mercury-json/src/json.m:1023:   warning: module `univ' has a
mercury-json/src/json.m:1023:   `:- import_module' declaration, but
mercury-json/src/json.m:1023:   is not used anywhere in the module.

I'm using Mercury 22.01.5.

Failed to convert JSON objects to Mercury maps

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

Performance issue when parsing large JSON data

It seems that mercury-json has a poor performance when parsing large JSON data.

For example, using following Python snippet to generate some random data:

import json
import random
with open("data.json", "w") as f: json.dump([[random.random() for i in range(5)] for i in range(5000)], f)

Then trying to load data using mercury-json:

:- module main.

:- interface.

:- import_module io.

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

:- implementation.

:- import_module list.
:- import_module string.
:- import_module require.

:- import_module json.

main(!IO) :-
    io.read_named_file_as_string("data.json", Result, !IO),
    ( Result = io.ok(Data),
      ( json.from_string(Data, json.array(Rows)) ->
        io.write_string(string.format("rows = %d\n", [i(list.length(Rows))]), !IO)
      ; require.error("Invalid JSON")
      )
    ; Result = io.error(Error),
      require.error(io.error_message(Error))
    ).

Convert Mercury maps to JSON objects

When trying to convert Mercury map to JSON object, a list of key and value pair objects is returned, instead of an object, which I think is not quite correct.

For example, with following demo code, the result is:

[{"key":"bar","value":["c"]},{"key":"foo","value":["a","b"]}]

Instead of:

{"bar": ["c"], "foo": ["a", "b"]}

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 pair.

main(!IO) :-
    Json = json.to_json(
               map.from_assoc_list(
                   ["foo" - ["a", "b"], "bar" - ["c"]])),
    json.write_compact(Json, !IO).

Tested on:

Mercury Compiler, version rotd-2019-08-16, on x86_64-pc-linux-gnu

Misleading error message

The following JSON, containing an unterminated string literal,

{ "compatible_crews": [ "37003 ] }

results in the following misleading error message from the parser:

bug.json:4:0: error: unescaped control character: U+000A
(It's technically correct but quite confusing.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.