juliensf / mercury-json Goto Github PK
View Code? Open in Web Editor NEWA Mercury JSON library.
License: BSD 2-Clause "Simplified" License
A Mercury JSON library.
License: BSD 2-Clause "Simplified" License
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.
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
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))
).
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
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.)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.