symisc / unqlite

An Embedded NoSQL, Transactional Database Engine
https://unqlite.symisc.net
Other
2.11k stars 164 forks source link

jx9's json_decode fails to decode negative integer #124

Closed mdorier closed 3 years ago

mdorier commented 3 years ago

This is really a Jx9 problem rather than unqlite but I'm reporting it here since there doesn't seem to be a repository for jx9, and both the forum link and the mailing list link are dead.

I'm using the version of Jx9 downloaded from here (version 1.7.2) on a Linux Debian. I'm executing this simple jx9 script, decoding a string into an object:

$a = '{"x" : -1}';

print "As a string ------";
print $a;
print "As JSON ------";
$b = json_decode($a);
print $b;
print gettype($b);

The execution indicates that $b is null, even though the object described by $a is valid JSON.

I tried using JX9_VM_CONFIG_ERR_REPORT with jx9_vm_config to get more information about the problem but it doesn't print any error.

mdorier commented 3 years ago

Note that in the following code, $a is correctly encoded then decoded, while $b failed to be decoded:

$a = {"x": -43};
$b = '{"x": -43}';

print "A ------";
print $a;
print json_encode($a);
print json_decode(json_encode($a));

print "B ------";
print $b;
print json_decode($b);

Output:

A ------
{"x":-43}
{"x":-43}
B ------
{"x": -43}
symisc commented 3 years ago

The Jx9 JSON decoder implementation does not handle negative number directly. This is obviously a wrong design choice or omission from the team. To circumvent this, wrap your negative number in double quotes to be treated as a string, and make your conversion later from your jx9 script. You can rely on the built-in function is_numeric() to check if a given string looks like a number.