From eb5b7722ea54497c1dec3c45ef62c08627b244f3 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 5 Oct 2021 14:07:31 +0200 Subject: [PATCH] virJSONValueObjectAddVArgs: Add 'k' convertor for formatting non-negative integers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In many cases we use a signed value, but use the sign to note that it was not assigned. For converting to JSON objects it will be handy to have possibility to do this automatically. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/util/virjson.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index 9adcea4fff..26491bd6c7 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -115,6 +115,7 @@ virJSONValueGetType(const virJSONValue *value) * * i: signed integer value * j: signed integer value, error if negative + * k: signed integer value, omitted if negative * z: signed integer value, omitted if zero * y: signed integer value, omitted if zero, error if negative * @@ -189,6 +190,7 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'z': case 'y': + case 'k': case 'j': case 'i': { int val = va_arg(args, int); @@ -203,6 +205,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, if (!val && (type == 'z' || type == 'y')) continue; + if (val < 0 && type == 'k') + continue; + rc = virJSONValueObjectAppendNumberInt(obj, key, val); } break;