Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast JSON library #5

Open
guregu opened this issue Sep 30, 2022 · 3 comments
Open

Fast JSON library #5

guregu opened this issue Sep 30, 2022 · 3 comments

Comments

@guregu
Copy link
Owner

guregu commented Sep 30, 2022

Current JSON library (borrowed from Scryer) is nice but slow (mthom/scryer-prolog#1566).

I had the potentially terrible idea of using regular Prolog terms to represent JSON. This seems to work as long as we tweak ASCII control characters to print with \u0000 escapes instead of \x0\ escapes.

Of course it is very bad at validating JSON as it accepts input like '{}'(':'([f,o,o], "bar")) as {"foo":"bar"}.
There are probably more issues I haven't caught yet.

It uses the same representation for JSON values as the real JSON lib (e.g. pairs([string("foo")-string("bar")])) so it should be possible to swap this out for a proper lib when it is optimized.

@guregu
Copy link
Owner Author

guregu commented Oct 17, 2022

I found one (maybe only one) discrepancy. Empty strings encode as [] (which is correct in Prolog terms). Unfortunately this happens for empty atoms too ('' → "" → []). The Go & JS ports work around this but it's not ideal. Maybe need to switch to SWI's verbose JSON format.

@guregu
Copy link
Owner Author

guregu commented Dec 26, 2022

Here's a nasty bug:

This fails to parse:

{"foo":-1,"bar":-2}

This is OK:

{"foo": -1,"bar": -2}

It's confusing negative numbers with Prolog's :- operator. Probably not easy to work around 🤔
Going to look into embedding jsmn or something.

@Jean-Luc-Picard-2021
Copy link

Jean-Luc-Picard-2021 commented Aug 11, 2024

The library here:

Current JSON library (borrowed from Scryer) is
nice but slow (mthom/scryer-prolog#1566).

Is not so slow anymore. Maybe Scryer Prolog improved or
something else improved? I don't know. I cannot close the
Scryer Prolog ticket myself.

I did also some trealla testing, with the same library I guess
that one gets via use_module(library(json)) in Trealla Prolog.
Its also ok speed wise.

Not as fast as SWI-Prolog, but somehow, not as drastic
as in the times back when the above Scryer Issue was raised.
Besides closing the Scryer Prolog ticket, maybe

the issue is also settled for Trealla Prolog?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants