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:
JsonError - NOT a Json Element. If JSON.parse return this, it means that the parsing failed, the error is in JsonError.what, it contains line number and some extra information.
JsonElement - One of JsonObject, JsonArray, JsonString, JsonNumber, JsonBool, or JsonNull
JsonObject - Hash Table that holds JsonElement values with String keys
JsonArray - Dynamic Array that holds JsonElement values
JsonString - String Literal
JsonNumber - One of JsonInt or JsonDouble
JsonInt - Integral Literal
JsonDouble - Decimal Literal
JsonBool - A true/false Literal
JsonNull - A null Literal
Re: [ZScript] JSON Parser
Posted: Tue Sep 14, 2021 12:33 pm
by Apeirogon
Add readme and license to the repo please.
Re: [ZScript] JSON Parser
Posted: Tue Sep 14, 2021 12:41 pm
by Jay0
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
Posted: Tue Sep 14, 2021 12:44 pm
by Apeirogon
Note to self, dont surf internet with beer in hand.
Re: [ZScript] JSON Parser
Posted: 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!
Posted: 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:
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!
Posted: Thu Sep 30, 2021 11:55 am
by Jay0
TheRatCircus wrote:Excellent library, but I've noticed a showstopper under some weird conditions. I can reliably reproduce it with the following snippet:
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
Re: [ZScript] JSON Parser - Now with prefixes!
Posted: 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!
Posted: Fri Oct 29, 2021 6:36 pm
by Jay0
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
Posted: Mon Nov 15, 2021 8:05 am
by Jay0
Update! Now you can turn a JsonElement back into JSON by calling JsonElement::serialize. (Object key order is not preserved)