json: teach dispatch logic to also take numbers formatted as strings
authorLennart Poettering <lennart@poettering.net>
Tue, 7 Nov 2023 12:14:43 +0000 (13:14 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 7 Nov 2023 13:25:40 +0000 (14:25 +0100)
commit67a3028555c58cea152cd8b8e863a643eb147a97
tree4e258c9d4b1e9325b21a3ce008cd3054fb33886d
parent14ac242d5019a6d59cf664ef4b5ed30656bac89a
json: teach dispatch logic to also take numbers formatted as strings

JSON famously is problematic with integers beyond 53 bits, because
JavaScript stores everything in double precision floating points.
Various implementations in other languages can deal with signed 64 bit
integers, and a few can deal with unsigned 64bit too (like ours).

Typically program that need more then 53 bit of accuracy encode integers
as decimal strings, to make sure that even if consumers can't really
process larger values they at least won't corrupt the data while passing
it along. This is also recommended by JSON-I (RFC 7493)

To maximize compatibility with other implementations let's add 1st class
parsing support for such objects in the json_dispatch() API.

This makes json_dispatch_uint64() and related calls parse such
integers-formatted-as-decimal-strings as uint64_t. This logic will only
be enabled if the "type" field of JsonDispatch is left unspecified (i.e.
set to negative/_JSON_VARIANT_TYPE_INVALID) though, hence alone does not
change anything in effect.

This purely is about consuming such values, whether we should genreate
them also is a discussion for a separate PR.
src/shared/json.c
src/test/test-json.c