From 68ecbca5d30ba58722152c0b4f252d76341917f1 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 16 Aug 2018 16:19:22 +0200 Subject: [PATCH] util: json: Allow converting a virTristate(Bool|Switch) into JSON Add a new modifier letter for virJSONValueObjectAddVArgs which will add a boolean value with our tristate semantics. The value is omitted when the _ABSENT value is used. Signed-off-by: Peter Krempa Reviewed-by: Erik Skultety --- src/util/virjson.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index 29530dcb15..0f16086a9c 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -142,6 +142,8 @@ virJSONValueGetType(const virJSONValue *value) * * b: boolean value * B: boolean value, omitted if false + * T: boolean value specified by a virTristate(Bool|Switch) value, omitted on + * the _ABSENT value * * d: double precision floating point number * n: json null value @@ -265,6 +267,7 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj, rc = virJSONValueObjectAppendNumberDouble(obj, key, val); } break; + case 'T': case 'B': case 'b': { int val = va_arg(args, int); @@ -272,6 +275,16 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj, if (!val && type == 'B') continue; + if (type == 'T') { + if (val == VIR_TRISTATE_BOOL_ABSENT) + continue; + + if (val == VIR_TRISTATE_BOOL_NO) + val = 0; + else + val = 1; + } + rc = virJSONValueObjectAppendBoolean(obj, key, val); } break; -- GitLab