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

Post your example zscripts/ACS scripts/etc here.
Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
User avatar
RicardoLuis0
Posts: 47
Joined: Tue Aug 21, 2018 9:31 pm
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Graphics Processor: nVidia (Modern GZDoom)
Location: Brazil

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

Post 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:
Last edited by RicardoLuis0 on Mon Nov 15, 2021 8:05 am, edited 7 times in total.
User avatar
Apeirogon
Posts: 1592
Joined: Mon Jun 12, 2017 12:57 am

Re: [ZScript] JSON Parser

Post by Apeirogon »

Add readme and license to the repo please.
User avatar
RicardoLuis0
Posts: 47
Joined: Tue Aug 21, 2018 9:31 pm
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Graphics Processor: nVidia (Modern GZDoom)
Location: Brazil

Re: [ZScript] JSON Parser

Post 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
User avatar
Apeirogon
Posts: 1592
Joined: Mon Jun 12, 2017 12:57 am

Re: [ZScript] JSON Parser

Post by Apeirogon »

Note to self, dont surf internet with beer in hand.
User avatar
Nash
 
 
Posts: 17283
Joined: Mon Oct 27, 2003 12:07 am
Twitch ID: nashmuhandes
Github ID: nashmuhandes
Location: Kuala Lumpur, Malaysia

Re: [ZScript] JSON Parser

Post by Nash »

Very nice! Been wanting one for quite some time and here it is. :D Thanks so much for doing this!
User avatar
TheRatCircus
Posts: 14
Joined: Mon Aug 02, 2021 1:58 am
Discord: Rat Circus#2998
Github ID: TheRatCircus
Graphics Processor: nVidia with Vulkan support

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

Post 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 all

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.
User avatar
RicardoLuis0
Posts: 47
Joined: Tue Aug 21, 2018 9:31 pm
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Graphics Processor: nVidia (Modern GZDoom)
Location: Brazil

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

Post 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 all

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: viewtopic.php?f=2&t=73477
User avatar
TheRatCircus
Posts: 14
Joined: Mon Aug 02, 2021 1:58 am
Discord: Rat Circus#2998
Github ID: TheRatCircus
Graphics Processor: nVidia with Vulkan support

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

Post by TheRatCircus »

The workaround is doing well for me. Thanks for the help.
User avatar
RicardoLuis0
Posts: 47
Joined: Tue Aug 21, 2018 9:31 pm
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Graphics Processor: nVidia (Modern GZDoom)
Location: Brazil

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

Post 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.
User avatar
RicardoLuis0
Posts: 47
Joined: Tue Aug 21, 2018 9:31 pm
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Graphics Processor: nVidia (Modern GZDoom)
Location: Brazil

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

Post by RicardoLuis0 »

Update! Now you can turn a JsonElement back into JSON by calling JsonElement::serialize. (Object key order is not preserved)

Return to “Script Library”