mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 19:41:16 +00:00
util: Move JSON object deflattening code to json utility file
The code will become more universal so it makes more sense for it to live with the rest of the JSON functions.
This commit is contained in:
parent
cadd96b3ea
commit
de75de7c97
@ -1933,6 +1933,7 @@ virJSONValueObjectAppendNumberUlong;
|
|||||||
virJSONValueObjectAppendString;
|
virJSONValueObjectAppendString;
|
||||||
virJSONValueObjectCreate;
|
virJSONValueObjectCreate;
|
||||||
virJSONValueObjectCreateVArgs;
|
virJSONValueObjectCreateVArgs;
|
||||||
|
virJSONValueObjectDeflatten;
|
||||||
virJSONValueObjectForeachKeyValue;
|
virJSONValueObjectForeachKeyValue;
|
||||||
virJSONValueObjectGet;
|
virJSONValueObjectGet;
|
||||||
virJSONValueObjectGetArray;
|
virJSONValueObjectGetArray;
|
||||||
|
@ -1965,3 +1965,64 @@ virJSONStringReformat(const char *jsonstr,
|
|||||||
virJSONValueFree(json);
|
virJSONValueFree(json);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
virJSONValueObjectDeflattenWorker(const char *key,
|
||||||
|
virJSONValuePtr value,
|
||||||
|
void *opaque)
|
||||||
|
{
|
||||||
|
virJSONValuePtr retobj = opaque;
|
||||||
|
virJSONValuePtr newval = NULL;
|
||||||
|
const char *newkey;
|
||||||
|
|
||||||
|
if (!(newkey = STRSKIP(key, "file."))) {
|
||||||
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
||||||
|
_("JSON object is neither nested nor flattened"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(newval = virJSONValueCopy(value)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (virJSONValueObjectAppend(retobj, newkey, newval) < 0) {
|
||||||
|
virJSONValueFree(newval);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virJSONValueObjectDeflatten:
|
||||||
|
*
|
||||||
|
* In some cases it's possible to nest JSON objects by prefixing object members
|
||||||
|
* with the parent object name followed by the dot and then the attribute name
|
||||||
|
* rather than directly using a nested value object (e.g qemu's JSON
|
||||||
|
* pseudo-protocol in backing file definition).
|
||||||
|
*
|
||||||
|
* This function will attempt to reverse the process and provide a nested json
|
||||||
|
* hierarchy so that the parsers can be kept simple and we still can use the
|
||||||
|
* weird syntax some users might use.
|
||||||
|
*
|
||||||
|
* Currently this function will flatten out just the 'file.' prefix into a new
|
||||||
|
* tree. Any other syntax will be rejected.
|
||||||
|
*/
|
||||||
|
virJSONValuePtr
|
||||||
|
virJSONValueObjectDeflatten(virJSONValuePtr json)
|
||||||
|
{
|
||||||
|
virJSONValuePtr ret;
|
||||||
|
|
||||||
|
if (!(ret = virJSONValueNewObject()))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (virJSONValueObjectForeachKeyValue(json,
|
||||||
|
virJSONValueObjectDeflattenWorker,
|
||||||
|
ret) < 0) {
|
||||||
|
virJSONValueFree(ret);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -186,4 +186,6 @@ virJSONValuePtr virJSONValueCopy(const virJSONValue *in);
|
|||||||
|
|
||||||
char *virJSONStringReformat(const char *jsonstr, bool pretty);
|
char *virJSONStringReformat(const char *jsonstr, bool pretty);
|
||||||
|
|
||||||
|
virJSONValuePtr virJSONValueObjectDeflatten(virJSONValuePtr json);
|
||||||
|
|
||||||
#endif /* __VIR_JSON_H_ */
|
#endif /* __VIR_JSON_H_ */
|
||||||
|
@ -3244,69 +3244,6 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
virStorageSourceParseBackingJSONDeflattenWorker(const char *key,
|
|
||||||
virJSONValuePtr value,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
virJSONValuePtr retobj = opaque;
|
|
||||||
virJSONValuePtr newval = NULL;
|
|
||||||
const char *newkey;
|
|
||||||
|
|
||||||
if (!(newkey = STRSKIP(key, "file."))) {
|
|
||||||
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
|
||||||
_("JSON backing file syntax is neither nested nor "
|
|
||||||
"flattened"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(newval = virJSONValueCopy(value)))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (virJSONValueObjectAppend(retobj, newkey, newval) < 0) {
|
|
||||||
virJSONValueFree(newval);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virStorageSourceParseBackingJSONDeflatten:
|
|
||||||
*
|
|
||||||
* The json: pseudo-protocol syntax in qemu allows multiple approaches to
|
|
||||||
* describe nesting of the values. This is due to the lax handling of the string
|
|
||||||
* in qemu and the fact that internally qemu is flattening the values using '.'.
|
|
||||||
*
|
|
||||||
* This allows to specify nested json strings either using nested json objects
|
|
||||||
* or prefixing object members with the parent object name followed by the dot.
|
|
||||||
*
|
|
||||||
* This function will attempt to reverse the process and provide a nested json
|
|
||||||
* hierarchy so that the parsers can be kept simple and we still can use the
|
|
||||||
* weird syntax some users might use.
|
|
||||||
*
|
|
||||||
* Currently this function will flatten out just the 'file.' prefix into a new
|
|
||||||
* tree. Any other syntax will be rejected.
|
|
||||||
*/
|
|
||||||
static virJSONValuePtr
|
|
||||||
virStorageSourceParseBackingJSONDeflatten(virJSONValuePtr json)
|
|
||||||
{
|
|
||||||
virJSONValuePtr ret;
|
|
||||||
|
|
||||||
if (!(ret = virJSONValueNewObject()))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (virJSONValueObjectForeachKeyValue(json,
|
|
||||||
virStorageSourceParseBackingJSONDeflattenWorker,
|
|
||||||
ret) < 0) {
|
|
||||||
virJSONValueFree(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
|
virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
|
||||||
@ -3320,7 +3257,7 @@ virStorageSourceParseBackingJSONInternal(virStorageSourcePtr src,
|
|||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
if (!(file = virJSONValueObjectGetObject(json, "file"))) {
|
if (!(file = virJSONValueObjectGetObject(json, "file"))) {
|
||||||
if (!(fixedroot = virStorageSourceParseBackingJSONDeflatten(json)))
|
if (!(fixedroot = virJSONValueObjectDeflatten(json)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
file = fixedroot;
|
file = fixedroot;
|
||||||
|
Loading…
Reference in New Issue
Block a user