Giter VIP home page Giter VIP logo

mercury-json's Introduction

Mercury-JSON

mercury_json is a Mercury library for reading and writing JSON from character streams.

Features

  • conforms to RFC 8259
  • optionally allows single and multiline comments in JSON
  • optionally allows single trailing commas in JSON objects and arrays
  • optionally allows numbers to be -Infinity / Infinity
  • optionally allows single quoted strings
  • user configurable behaviour for handling repeated object members
  • marshaling and unmarshaling of Mercury data to and from JSON
  • user configurable maximum nesting depth limit

License

mercury_json is licensed under a simple 2-clause BSD style license. See the file COPYING for details.

Installation

Check the values in the file Make.options to see if they agree with your system, then do:

$ make install

You can also override values in Make.options on the command line, for example

$ make INSTALL_PREFIX=/foo/bar install

causes the library to be installed in the directory /foo/bar.

Testing

To run the regression test suite, do:

$ make runtests

Examples

The samples directory contains some example programs that demonstrate different features of the library.

Mercury 20.06.X Compatibility

The code on the master branch is not compatible with Mercury 20.06.X. If you require a version of mercury_json that works with Mercury 20.06.X, then checkout the mercury_20_06 branch.

Mercury 20.01.X Compatibility

The code on the master branch is not compatible with Mercury 20.01.X. If you require a version of mercury_json that works with Mercury 20.01.X, then checkout the mercury_20_01 branch.

Mercury 14.01.X Compatibility

The code on the master branch is not compatible with Mercury 14.01.X. If you require a version of mercury_json that works with Mercury 14.01.X, then checkout the mercury_14_01 branch.

Author

Julien Fischer [email protected]

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

Watchers

 avatar  avatar  avatar  avatar

mercury-json's Issues

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

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

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

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

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.

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.