From nobody Sun Apr 28 04:33:55 2024 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 1522486889908934.77776564309; Sat, 31 Mar 2018 02:01:29 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 92A9736807; Sat, 31 Mar 2018 09:01:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 65B9860E3E; Sat, 31 Mar 2018 09:01:26 +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 94403180613A; Sat, 31 Mar 2018 09:01:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w2V91K5Z022617 for ; Sat, 31 Mar 2018 05:01:20 -0400 Received: by smtp.corp.redhat.com (Postfix) id A55F0215CDCA; Sat, 31 Mar 2018 09:01:20 +0000 (UTC) Received: from andariel.redhat.com (ovpn-204-50.brq.redhat.com [10.40.204.50]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6D23215CDC8; Sat, 31 Mar 2018 09:01:19 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Sat, 31 Mar 2018 11:01:15 +0200 Message-Id: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH] util: json: Remove yajl bits from virJSONValueToStr 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Sat, 31 Mar 2018 09:01:28 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than depending on yajl bits for creating the JSON structure replace it by few virBuffer bits. This will make the JSON formatter libary agnostic. Additionally remove the debug statement from the worker function since it was not very useful. Signed-off-by: Peter Krempa --- src/util/virjson.c | 188 ++++++++++++++++++++++++++++---------------------= ---- 1 file changed, 101 insertions(+), 87 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 6a02ddf0cc..772a205e9e 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1834,144 +1834,158 @@ virJSONValueFromString(const char *jsonstring) return ret; } +#else +virJSONValuePtr +virJSONValueFromString(const char *jsonstring ATTRIBUTE_UNUSED) +{ + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No JSON parser implementation is available")); + return NULL; +} +#endif + + +static void +virJSONValueToStringAddString(virBufferPtr buf, + virJSONValuePtr string) +{ + const char *t; + + virBufferAddLit(buf, "\""); + + for (t =3D string->data.string; *t; t++) { + switch (*t) { + case '"': + virBufferAddLit(buf, "\\\""); + break; + case '\\': + virBufferAddLit(buf, "\\\\"); + break; + case '\n': + virBufferAddLit(buf, "\\n"); + break; + case '\t': + virBufferAddLit(buf, "\\t"); + break; + default: + virBufferAdd(buf, t, 1); + break; + } + } + + virBufferAddLit(buf, "\""); +} + + +#define VIR_JSON_PRETTY_NEWLINE \ + if (pretty) \ + virBufferAddLit(buf, "\n") static int virJSONValueToStringOne(virJSONValuePtr object, - yajl_gen g) + virBufferPtr buf, + bool pretty) { size_t i; - VIR_DEBUG("object=3D%p type=3D%d gen=3D%p", object, object->type, g); - - switch (object->type) { + switch ((virJSONType) object->type) { case VIR_JSON_TYPE_OBJECT: - if (yajl_gen_map_open(g) !=3D yajl_gen_status_ok) - return -1; + virBufferAddLit(buf, "{"); + VIR_JSON_PRETTY_NEWLINE; + virBufferAdjustIndent(buf, 2); + for (i =3D 0; i < object->data.object.npairs; i++) { - if (yajl_gen_string(g, - (unsigned char *)object->data.object.pairs= [i].key, - strlen(object->data.object.pairs[i].key)) - !=3D yajl_gen_status_ok) - return -1; - if (virJSONValueToStringOne(object->data.object.pairs[i].value= , g) < 0) + virBufferStrcat(buf, "\"", object->data.object.pairs[i].key, "= \":", NULL); + + if (pretty) + virBufferAddLit(buf, " "); + + if (virJSONValueToStringOne(object->data.object.pairs[i].value, + buf, pretty) < 0) return -1; + + if (i !=3D object->data.object.npairs - 1) { + virBufferAddLit(buf, ","); + VIR_JSON_PRETTY_NEWLINE; + } } - if (yajl_gen_map_close(g) !=3D yajl_gen_status_ok) - return -1; + + virBufferAdjustIndent(buf, -2); + VIR_JSON_PRETTY_NEWLINE; + virBufferAddLit(buf, "}"); break; + case VIR_JSON_TYPE_ARRAY: - if (yajl_gen_array_open(g) !=3D yajl_gen_status_ok) - return -1; + virBufferAddLit(buf, "["); + VIR_JSON_PRETTY_NEWLINE; + virBufferAdjustIndent(buf, 2); + for (i =3D 0; i < object->data.array.nvalues; i++) { - if (virJSONValueToStringOne(object->data.array.values[i], g) <= 0) + if (virJSONValueToStringOne(object->data.array.values[i], buf,= pretty) < 0) return -1; + + if (i !=3D object->data.array.nvalues - 1) { + virBufferAddLit(buf, ","); + VIR_JSON_PRETTY_NEWLINE; + } } - if (yajl_gen_array_close(g) !=3D yajl_gen_status_ok) - return -1; + + virBufferAdjustIndent(buf, -2); + VIR_JSON_PRETTY_NEWLINE; + virBufferAddLit(buf, "]"); break; case VIR_JSON_TYPE_STRING: - if (yajl_gen_string(g, (unsigned char *)object->data.string, - strlen(object->data.string)) !=3D yajl_gen_sta= tus_ok) - return -1; + virJSONValueToStringAddString(buf, object); break; case VIR_JSON_TYPE_NUMBER: - if (yajl_gen_number(g, object->data.number, - strlen(object->data.number)) !=3D yajl_gen_sta= tus_ok) - return -1; + virBufferAdd(buf, object->data.number, -1); break; case VIR_JSON_TYPE_BOOLEAN: - if (yajl_gen_bool(g, object->data.boolean) !=3D yajl_gen_status_ok) - return -1; + if (object->data.boolean) + virBufferAddLit(buf, "true"); + else + virBufferAddLit(buf, "false"); break; case VIR_JSON_TYPE_NULL: - if (yajl_gen_null(g) !=3D yajl_gen_status_ok) - return -1; + virBufferAddLit(buf, "null"); break; default: + virReportEnumRangeError(virJSONType, object->type); return -1; } return 0; } +#undef VIR_JSON_PRETTY_NEWLINE + char * virJSONValueToString(virJSONValuePtr object, bool pretty) { - yajl_gen g; - const unsigned char *str; + virBuffer buf =3D VIR_BUFFER_INITIALIZER; char *ret =3D NULL; - yajl_size_t len; -# ifndef WITH_YAJL2 - yajl_gen_config conf =3D { pretty ? 1 : 0, pretty ? " " : " "}; -# endif VIR_DEBUG("object=3D%p", object); -# ifdef WITH_YAJL2 - g =3D yajl_gen_alloc(NULL); - if (g) { - yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0); - yajl_gen_config(g, yajl_gen_indent_string, pretty ? " " : " "); - yajl_gen_config(g, yajl_gen_validate_utf8, 1); - } -# else - g =3D yajl_gen_alloc(&conf, NULL); -# endif - if (!g) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to create JSON formatter")); - goto cleanup; - } - - if (virJSONValueToStringOne(object, g) < 0) { - virReportOOMError(); - goto cleanup; - } - - if (yajl_gen_get_buf(g, &str, &len) !=3D yajl_gen_status_ok) { - virReportOOMError(); - goto cleanup; + if (virJSONValueToStringOne(object, &buf, pretty) < 0 || + virBufferCheckError(&buf) < 0) { + virBufferFreeAndReset(&buf); + return NULL; } - ignore_value(VIR_STRDUP(ret, (const char *)str)); - - cleanup: - yajl_gen_free(g); - - VIR_DEBUG("result=3D%s", NULLSTR(ret)); - + ret =3D virBufferContentAndReset(&buf); + VIR_DEBUG("result=3D%s", ret); return ret; } -#else -virJSONValuePtr -virJSONValueFromString(const char *jsonstring ATTRIBUTE_UNUSED) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("No JSON parser implementation is available")); - return NULL; -} - - -char * -virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED, - bool pretty ATTRIBUTE_UNUSED) -{ - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("No JSON parser implementation is available")); - return NULL; -} -#endif - - /** * virJSONStringReformat: * @jsonstr: string to reformat --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list