Page 1 of 1

[ZScript] JSON Parser - Now with Serialization! [11/15/21]

PostPosted: Tue Sep 14, 2021 12:13 pm
by RicardoLuis0
This is a JSON parsing library for ZScript.

Includes a usage example and a small test suite.

It supports, in addition to strict JSON: trailing commas, single-quote strings, and single/multi-line comments (C/C++-style).

To use in your mod, just generate the prefixes, copy over the generated/ZJSON folder, and add #include "ZJSON/Include.zs" to your main ZScript file.

Get it from GitHub

Generating Prefixes:

Run generate.sh [your prefix] on linux/wsl/msys/cygwin. The prefixed files will be in the generated folder.

How to Use:
Call JSON.parse with a string that contains JSON text, it will return either a JsonElement in case of success, or a JsonError in case of failure. Call JsonElement::serialize to generate JSON back from a JsonElement.

Types:
Spoiler:

Re: [ZScript] JSON Parser

PostPosted: Tue Sep 14, 2021 12:33 pm
by Apeirogon
Add readme and license to the repo please.

Re: [ZScript] JSON Parser

PostPosted: Tue Sep 14, 2021 12:41 pm
by RicardoLuis0
Apeirogon wrote:Add readme and license to the repo please.

sure, i'll add a readme, but regarding license, it has had a license since the start: MIT

Re: [ZScript] JSON Parser

PostPosted: Tue Sep 14, 2021 12:44 pm
by Apeirogon
Note to self, dont surf internet with beer in hand.

Re: [ZScript] JSON Parser

PostPosted: Tue Sep 14, 2021 5:02 pm
by Nash
Very nice! Been wanting one for quite some time and here it is. :D Thanks so much for doing this!

Re: [ZScript] JSON Parser - Now with prefixes!

PostPosted: Thu Sep 30, 2021 12:15 am
by TheRatCircus
Excellent library, but I've noticed a showstopper under some weird conditions. I can reliably reproduce it with the following snippet:
Code: Select allExpand view
JSON.parse("[ { \"type\": \"WEAPON\", \"rarity\": \"COMMON\", \"items\": { \"Rattler\": 10 } } ]");


The executable crashes due to a VM abort at this function, citing "array access out of bounds. Negative current index = -125".

Turning the "R" in "Rattler" to lower-case fixes it. It seems to always have something to do with upper-case letters in field keys.

However, changing "Rattler" to something like "BIO" also works fine. If it helps, anything I've sent to get parsed have been in UTF-8 with LF line endings.

Thanks for any help you can give.

Re: [ZScript] JSON Parser - Now with prefixes!

PostPosted: Thu Sep 30, 2021 11:55 am
by RicardoLuis0
TheRatCircus wrote:Excellent library, but I've noticed a showstopper under some weird conditions. I can reliably reproduce it with the following snippet:
Code: Select allExpand view
JSON.parse("[ { \"type\": \"WEAPON\", \"rarity\": \"COMMON\", \"items\": { \"Rattler\": 10 } } ]");


The executable crashes due to a VM abort at this function, citing "array access out of bounds. Negative current index = -125".

Turning the "R" in "Rattler" to lower-case fixes it. It seems to always have something to do with upper-case letters in field keys.

However, changing "Rattler" to something like "BIO" also works fine. If it helps, anything I've sent to get parsed have been in UTF-8 with LF line endings.

Thanks for any help you can give.

it seems that an uint (hashed string) is turning negative when being constrained to the limit of the hash table array (256)? seems possibly to be a bug with the modulus operator.
the expression that's crashing is table[hash(key)%table_size], where hash returns an uint, and table_size is 256.
It seems to be caused by the fact that table_size is a constant, if i replace it with an uint variable, it works properly, so i'll implement that workaround for now.
I've filed a bug report: https://forum.zdoom.org/viewtopic.php?f=2&t=73477

Re: [ZScript] JSON Parser - Now with prefixes!

PostPosted: Thu Sep 30, 2021 2:42 pm
by TheRatCircus
The workaround is doing well for me. Thanks for the help.

Re: [ZScript] JSON Parser - Now with prefixes!

PostPosted: Fri Oct 29, 2021 6:36 pm
by RicardoLuis0
Update! Due to an oversight while imeplementing, the parser didn't recognize signs in numbers, now it properly handles positive (+) and negative (-) signs in numbers.

Re: [ZScript] JSON Parser - Now with Serialization! [11/15/2

PostPosted: Mon Nov 15, 2021 8:05 am
by RicardoLuis0
Update! Now you can turn a JsonElement back into JSON by calling JsonElement::serialize. (Object key order is not preserved)