json-util: Add JSON_BUILD_PAIR_INTEGER_NON_ZERO()

This commit is contained in:
Daan De Meyer 2024-08-09 15:12:22 +02:00
parent 9ba489c7f6
commit ab8a5e1f99
2 changed files with 30 additions and 0 deletions

View File

@ -143,6 +143,7 @@ enum {
_JSON_BUILD_DUAL_TIMESTAMP,
_JSON_BUILD_RATELIMIT,
_JSON_BUILD_PAIR_INTEGER_NON_ZERO,
_JSON_BUILD_PAIR_UNSIGNED_NON_ZERO,
_JSON_BUILD_PAIR_FINITE_USEC,
_JSON_BUILD_PAIR_STRING_NON_EMPTY,
@ -176,6 +177,7 @@ enum {
#define JSON_BUILD_DUAL_TIMESTAMP(t) _JSON_BUILD_DUAL_TIMESTAMP, (dual_timestamp*) { t }
#define JSON_BUILD_RATELIMIT(rl) _JSON_BUILD_RATELIMIT, (const RateLimit*) { rl }
#define JSON_BUILD_PAIR_INTEGER_NON_ZERO(name, i) _JSON_BUILD_PAIR_INTEGER_NON_ZERO, (const char*) { name }, (int64_t) { i }
#define JSON_BUILD_PAIR_UNSIGNED_NON_ZERO(name, u) _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, (const char*) { name }, (uint64_t) { u }
#define JSON_BUILD_PAIR_FINITE_USEC(name, u) _JSON_BUILD_PAIR_FINITE_USEC, (const char*) { name }, (usec_t) { u }
#define JSON_BUILD_PAIR_STRING_NON_EMPTY(name, s) _JSON_BUILD_PAIR_STRING_NON_EMPTY, (const char*) { name }, (const char*) { s }

View File

@ -4330,6 +4330,34 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) {
break;
}
case _JSON_BUILD_PAIR_INTEGER_NON_ZERO: {
const char *n;
int64_t i;
if (current->expect != EXPECT_OBJECT_KEY) {
r = -EINVAL;
goto finish;
}
n = va_arg(ap, const char*);
i = va_arg(ap, int64_t);
if (i != 0 && current->n_suppress == 0) {
r = sd_json_variant_new_string(&add, n);
if (r < 0)
goto finish;
r = sd_json_variant_new_integer(&add_more, i);
if (r < 0)
goto finish;
}
n_subtract = 2; /* we generated two item */
current->expect = EXPECT_OBJECT_KEY;
break;
}
case _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO: {
const char *n;
uint64_t u;