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

Post your example zscripts/ACS scripts/etc here.

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

Postby RicardoLuis0 » Tue Sep 14, 2021 12:13 pm

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
RicardoLuis0
 
Joined: 21 Aug 2018
Location: Brazil
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: [ZScript] JSON Parser

Postby Apeirogon » Tue Sep 14, 2021 12:33 pm

Add readme and license to the repo please.
User avatar
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: [ZScript] JSON Parser

Postby RicardoLuis0 » Tue Sep 14, 2021 12:41 pm

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
RicardoLuis0
 
Joined: 21 Aug 2018
Location: Brazil
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: [ZScript] JSON Parser

Postby Apeirogon » Tue Sep 14, 2021 12:44 pm

Note to self, dont surf internet with beer in hand.
User avatar
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: [ZScript] JSON Parser

Postby Nash » Tue Sep 14, 2021 5:02 pm

Very nice! Been wanting one for quite some time and here it is. :D Thanks so much for doing this!
User avatar
Nash
AKA Nash Muhandes! Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby TheRatCircus » Thu Sep 30, 2021 12:15 am

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.
User avatar
TheRatCircus
 
Joined: 02 Aug 2021
Discord: Rat Circus#2998
Github ID: TheRatCircus
Operating System: Debian-like Linux (Debian, Ubuntu, Mint, etc) 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

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

Postby RicardoLuis0 » Thu Sep 30, 2021 11:55 am

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
User avatar
RicardoLuis0
 
Joined: 21 Aug 2018
Location: Brazil
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

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

Postby TheRatCircus » Thu Sep 30, 2021 2:42 pm

The workaround is doing well for me. Thanks for the help.
User avatar
TheRatCircus
 
Joined: 02 Aug 2021
Discord: Rat Circus#2998
Github ID: TheRatCircus
Operating System: Debian-like Linux (Debian, Ubuntu, Mint, etc) 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

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

Postby RicardoLuis0 » Fri Oct 29, 2021 6:36 pm

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
 
Joined: 21 Aug 2018
Location: Brazil
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

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

Postby RicardoLuis0 » Mon Nov 15, 2021 8:05 am

Update! Now you can turn a JsonElement back into JSON by calling JsonElement::serialize. (Object key order is not preserved)
User avatar
RicardoLuis0
 
Joined: 21 Aug 2018
Location: Brazil
Discord: RicardoLuis0#1862
Github ID: RicardoLuis0
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)


Return to Script Library

Who is online

Users browsing this forum: No registered users and 0 guests