From nobody Wed May 14 06:24:20 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; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1526043580881678.5929702516329; Fri, 11 May 2018 05:59:40 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 18AF830BA342; Fri, 11 May 2018 12:59:39 +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 DB8D595A5C; Fri, 11 May 2018 12:59:38 +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 96CF44CAAD; Fri, 11 May 2018 12:59:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4BCxQ1S008277 for ; Fri, 11 May 2018 08:59:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id B56346F9E8; Fri, 11 May 2018 12:59:26 +0000 (UTC) Received: from icr.brq.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5AD67728F3 for ; Fri, 11 May 2018 12:59:26 +0000 (UTC) From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Fri, 11 May 2018 14:59:04 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv3 03/13] Switch from yajl to Jansson 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Fri, 11 May 2018 12:59:40 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Yajl has not seen much activity upstream recently. Switch to using Jansson >=3D 2.7. All the platforms we target on https://libvirt.org/platforms.html have a version >=3D 2.7 listed on the sites below: https://repology.org/metapackage/jansson/versions https://build.opensuse.org/package/show/devel:libraries:c_c++/libjansson Implement virJSONValue{From,To}String using Jansson, delete the yajl code (and the related virJSONParser structure) and report an error if someone explicitly specifies --with-yajl. Also adjust the test data to account for Jansson's different whitespace usage for empty arrays and tune up the specfile to keep 'make rpm' working when bisecting. Signed-off-by: J=C3=A1n Tomko --- src/util/virjson.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 211 insertions(+) diff --git a/src/util/virjson.c b/src/util/virjson.c index 0559d40b64..2f7d624bb3 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -2008,6 +2008,217 @@ virJSONValueToString(virJSONValuePtr object, } =20 =20 +#elif WITH_JANSSON +# include + +static virJSONValuePtr +virJSONValueFromJansson(json_t *json) +{ + virJSONValuePtr ret =3D NULL; + const char *key; + json_t *cur; + size_t i; + + switch (json_typeof(json)) { + case JSON_OBJECT: + ret =3D virJSONValueNewObject(); + if (!ret) + goto error; + + json_object_foreach(json, key, cur) { + virJSONValuePtr val =3D virJSONValueFromJansson(cur); + if (!val) + goto error; + + if (virJSONValueObjectAppend(ret, key, val) < 0) { + virJSONValueFree(val); + goto error; + } + } + + break; + + case JSON_ARRAY: + ret =3D virJSONValueNewArray(); + if (!ret) + goto error; + + json_array_foreach(json, i, cur) { + virJSONValuePtr val =3D virJSONValueFromJansson(cur); + if (!val) + goto error; + + if (virJSONValueArrayAppend(ret, val) < 0) { + virJSONValueFree(val); + goto error; + } + } + break; + + case JSON_STRING: + ret =3D virJSONValueNewStringLen(json_string_value(json), + json_string_length(json)); + break; + + case JSON_INTEGER: + ret =3D virJSONValueNewNumberLong(json_integer_value(json)); + break; + + case JSON_REAL: + ret =3D virJSONValueNewNumberDouble(json_real_value(json)); + break; + + case JSON_TRUE: + case JSON_FALSE: + ret =3D virJSONValueNewBoolean(json_boolean_value(json)); + break; + + case JSON_NULL: + ret =3D virJSONValueNewNull(); + break; + } + + return ret; + + error: + virJSONValueFree(ret); + return NULL; +} + +virJSONValuePtr +virJSONValueFromString(const char *jsonstring) +{ + virJSONValuePtr ret =3D NULL; + json_t *json; + json_error_t error; + size_t flags =3D JSON_REJECT_DUPLICATES | + JSON_DECODE_ANY; + + if (!(json =3D json_loads(jsonstring, flags, &error))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("failed to parse JSON %d:%d: %s"), + error.line, error.column, error.text); + return NULL; + } + + ret =3D virJSONValueFromJansson(json); + json_decref(json); + return ret; +} + + +static json_t * +virJSONValueToJansson(virJSONValuePtr object) +{ + json_t *ret =3D NULL; + size_t i; + + switch ((virJSONType)object->type) { + case VIR_JSON_TYPE_OBJECT: + ret =3D json_object(); + if (!ret) { + virReportOOMError(); + goto error; + } + for (i =3D 0; i < object->data.object.npairs; i++) { + virJSONObjectPairPtr cur =3D object->data.object.pairs + i; + json_t *val =3D virJSONValueToJansson(cur->value); + + if (!val) + goto error; + if (json_object_set_new(ret, cur->key, val) < 0) { + virReportOOMError(); + goto error; + } + } + break; + + case VIR_JSON_TYPE_ARRAY: + ret =3D json_array(); + if (!ret) { + virReportOOMError(); + goto error; + } + for (i =3D 0; i < object->data.array.nvalues; i++) { + virJSONValuePtr cur =3D object->data.array.values[i]; + json_t *val =3D virJSONValueToJansson(cur); + + if (!val) + goto error; + if (json_array_append_new(ret, val) < 0) { + virReportOOMError(); + goto error; + } + } + break; + + case VIR_JSON_TYPE_STRING: + ret =3D json_string(object->data.string); + break; + + case VIR_JSON_TYPE_NUMBER: { + long long ll_val; + double d_val; + if (virStrToLong_ll(object->data.number, NULL, 10, &ll_val) < 0) { + if (virStrToDouble(object->data.number, NULL, &d_val) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("JSON value is not a number")); + return NULL; + } + ret =3D json_real(d_val); + } else { + ret =3D json_integer(ll_val); + } + } + break; + + case VIR_JSON_TYPE_BOOLEAN: + ret =3D json_boolean(object->data.boolean); + break; + + case VIR_JSON_TYPE_NULL: + ret =3D json_null(); + break; + + default: + virReportEnumRangeError(virJSONType, object->type); + goto error; + } + if (!ret) + virReportOOMError(); + return ret; + + error: + json_decref(ret); + return NULL; +} + + +char * +virJSONValueToString(virJSONValuePtr object, + bool pretty) +{ + size_t flags =3D JSON_ENCODE_ANY; + json_t *json; + char *str =3D NULL; + + if (pretty) + flags |=3D JSON_INDENT(2); + else + flags |=3D JSON_COMPACT; + + json =3D virJSONValueToJansson(object); + if (!json) + return NULL; + + str =3D json_dumps(json, flags); + if (!str) + virReportOOMError(); + json_decref(json); + return str; +} + + #else virJSONValuePtr virJSONValueFromString(const char *jsonstring ATTRIBUTE_UNUSED) --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list