From nobody Thu May 15 03:59:02 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15173969516911006.6957550488245; Wed, 31 Jan 2018 03:09:11 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2EE64693D0; Wed, 31 Jan 2018 11:09:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 48FDA608F1; Wed, 31 Jan 2018 11:09:02 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5A6FF3FB1F; Wed, 31 Jan 2018 11:09:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w0VB8DjX009666 for ; Wed, 31 Jan 2018 06:08:13 -0500 Received: by smtp.corp.redhat.com (Postfix) id EF6535EDEE; Wed, 31 Jan 2018 11:08:12 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4AF125EDE0; Wed, 31 Jan 2018 11:08:12 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 31 Jan 2018 12:07:31 +0100 Message-Id: <2f7111c75b7b1f14963e0f08db7c0774c0ab116e.1517396746.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 1/2] util: json: Add helper to return string or number properties as string X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 31 Jan 2018 11:09:04 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The helper is useful when in cases when JSON we have to parse may return one of the two due to historical reasons and the number value itself would be stored as a string. --- src/util/virjson.c | 27 +++++++++++++++++++++++++++ src/util/virjson.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index 17b11f2b3d..14b68b8c93 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1208,6 +1208,33 @@ virJSONValueObjectGetString(virJSONValuePtr object, } +/** + * virJSONValueObjectGetStringOrNumber: + * @object: JSON value object + * @key: name of the property in @object to get + * + * Gets a property named @key from the JSON object @object. The value may = be + * a number or a string and is returned as a string. In cases when the pro= perty + * is not present or is not a string or number NULL is returned. + */ +const char * +virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, + const char *key) +{ + virJSONValuePtr val =3D virJSONValueObjectGet(object, key); + + if (!val) + return NULL; + + if (val->type =3D=3D VIR_JSON_TYPE_STRING) + return val->data.string; + else if (val->type =3D=3D VIR_JSON_TYPE_NUMBER) + return val->data.number; + + return NULL; +} + + int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, diff --git a/src/util/virjson.h b/src/util/virjson.h index e89a776ab5..b76a3c3472 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -149,6 +149,7 @@ virJSONValuePtr virJSONValueObjectStealArray(virJSONVal= uePtr object, const char *key); const char *virJSONValueObjectGetString(virJSONValuePtr object, const char= *key); +const char *virJSONValueObjectGetStringOrNumber(virJSONValuePtr object, co= nst char *key); int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key= , int *value); int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *ke= y, unsigned int *value); int virJSONValueObjectGetNumberLong(virJSONValuePtr object, const char *ke= y, long long *value); --=20 2.15.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list