mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-22 04:25:18 +00:00
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.
This commit is contained in:
parent
5cd670fea8
commit
15727562a6
@ -1791,6 +1791,7 @@ virISCSIScanTargets;
|
||||
|
||||
|
||||
# util/virjson.h
|
||||
virJSONStringReformat;
|
||||
virJSONValueArrayAppend;
|
||||
virJSONValueArrayForeachSteal;
|
||||
virJSONValueArrayGet;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -181,4 +181,6 @@ int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
|
||||
|
||||
virJSONValuePtr virJSONValueCopy(const virJSONValue *in);
|
||||
|
||||
char *virJSONStringReformat(const char *jsonstr, bool pretty);
|
||||
|
||||
#endif /* __VIR_JSON_H_ */
|
||||
|
Loading…
x
Reference in New Issue
Block a user