From: Peter Krempa Date: Wed, 30 Nov 2016 09:46:57 +0000 (+0100) Subject: util: json: Add helper to reformat JSON strings X-Git-Tag: CVE-2017-2635~296 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=15727562a6744a13f144cb3c014539a7d166ddd0;p=thirdparty%2Flibvirt.git util: json: Add helper to reformat JSON strings For use in test cases it will be helpful to allow reformatting JSON strings. Add a wrapper on top of the parser and formatter to achieve this. --- diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 70ed87b953..cfeb43cf0d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1791,6 +1791,7 @@ virISCSIScanTargets; # util/virjson.h +virJSONStringReformat; virJSONValueArrayAppend; virJSONValueArrayForeachSteal; virJSONValueArrayGet; diff --git a/src/util/virjson.c b/src/util/virjson.c index 3804e7e977..c907b5ded1 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1918,3 +1918,32 @@ virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED, return NULL; } #endif + + +/** + * virJSONStringReformat: + * @jsonstr: string to reformat + * @pretty: use the pretty formatter + * + * Reformats a JSON string by passing it to the parser and then to the + * formatter. If @pretty is true the JSON is formatted for human eye + * compatibility. + * + * Returns the reformatted JSON string on success; NULL and a libvirt error on + * failure. + */ +char * +virJSONStringReformat(const char *jsonstr, + bool pretty) +{ + virJSONValuePtr json; + char *ret; + + if (!(json = virJSONValueFromString(jsonstr))) + return NULL; + + ret = virJSONValueToString(json, pretty); + + virJSONValueFree(json); + return ret; +} diff --git a/src/util/virjson.h b/src/util/virjson.h index 122de96c52..5e32cb9a41 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -181,4 +181,6 @@ int virJSONValueObjectForeachKeyValue(virJSONValuePtr object, virJSONValuePtr virJSONValueCopy(const virJSONValue *in); +char *virJSONStringReformat(const char *jsonstr, bool pretty); + #endif /* __VIR_JSON_H_ */