json: relax float parsing some more

We already support more float variants than standard JSON in the relaxed
format, adding extra restrictions does not actually help much. If you
need to know if this is a value JSON number, there is now a function to
check that instead.
This commit is contained in:
Wim Taymans 2026-03-09 16:48:05 +01:00
parent 231a41a22f
commit ddf63e0863
2 changed files with 11 additions and 15 deletions

View file

@ -425,20 +425,11 @@ SPA_API_JSON bool spa_json_is_null(const char *val, int len)
/* float */
SPA_API_JSON int spa_json_parse_float(const char *val, int len, float *result)
{
char buf[96];
char *end;
int pos;
char buf[96], *end;
if (len <= 0 || len >= (int)sizeof(buf))
return 0;
for (pos = 0; pos < len; ++pos) {
switch (val[pos]) {
case '+': case '-': case '0' ... '9': case '.': case 'e': case 'E': break;
default: return 0;
}
}
memcpy(buf, val, len);
buf[len] = '\0';
@ -466,8 +457,7 @@ SPA_API_JSON char *spa_json_format_float(char *str, int size, float val)
/* int */
SPA_API_JSON int spa_json_parse_int(const char *val, int len, int *result)
{
char buf[64];
char *end;
char buf[64], *end;
if (len <= 0 || len >= (int)sizeof(buf))
return 0;

View file

@ -717,11 +717,11 @@ PWTEST(json_float_check)
{ "0,0", 0, 0 },
{ "0.0.5", 0, 0 },
{ "0x0", 0, 0 },
{ "0x0.0", 0, 0 },
{ "0x0", 1, 0 },
{ "0x0.0", 1, 0 },
{ "E10", 0, 0 },
{ "e20", 0, 0 },
{ " 0.0", 0, 0 },
{ " 0.0", 1, 0 },
{ "0.0 ", 0, 0 },
{ " 0.0 ", 0, 0 },
@ -1002,6 +1002,12 @@ PWTEST(json_data)
"n_number_-2..json",
"n_number_hex_1_digit.json",
"n_number_hex_2_digits.json",
"n_number_infinity.json",
"n_number_+Inf.json",
"n_number_Inf.json",
"n_number_minus_infinity.json",
"n_number_-NaN.json",
"n_number_NaN.json",
"n_number_neg_int_starting_with_zero.json",
"n_number_neg_real_without_int_part.json",
"n_number_real_without_fractional_part.json",