[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH V4 10/24] libxl_json: allow basic JSON type objects generation
The original logic is that basic JSON types (number, string and null) must be an element of JSON map or array. This assumption doesn't hold true anymore when we need to return basic JSON types. Returning basic JSON types is required for parsing number, string and null objects back into libxl__json_object. Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx> --- tools/libxl/libxl_internal.h | 2 +- tools/libxl/libxl_json.c | 87 ++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 51 deletions(-) diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 9074d40..6e3d19d 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -1735,7 +1735,7 @@ _hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt, libxl__json_node_type type); _hidden int libxl__json_object_append_to(libxl__gc *gc_opt, libxl__json_object *obj, - libxl__json_object *dst); + libxl__yajl_ctx *ctx); _hidden libxl__json_object *libxl__json_array_get(const libxl__json_object *o, int i); _hidden diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c index 2cab951..cb8490b 100644 --- a/tools/libxl/libxl_json.c +++ b/tools/libxl/libxl_json.c @@ -415,36 +415,43 @@ libxl__json_object *libxl__json_object_alloc(libxl__gc *gc, return obj; } -int libxl__json_object_append_to(libxl__gc *gc, - libxl__json_object *obj, - libxl__json_object *dst) +int libxl__json_object_append_to(libxl__gc *gc, libxl__json_object *obj, + libxl__yajl_ctx *ctx) { - assert(dst != NULL); + libxl__json_object *dst = ctx->current; - switch (dst->type) { - case JSON_MAP: { - libxl__json_map_node *last; + if (dst) { + switch (dst->type) { + case JSON_MAP: { + libxl__json_map_node *last; - if (dst->u.map->count == 0) { + if (dst->u.map->count == 0) { + LIBXL__LOG(libxl__gc_owner(gc), LIBXL__LOG_ERROR, + "Try to add a value to an empty map (with no key)"); + return ERROR_FAIL; + } + flexarray_get(dst->u.map, dst->u.map->count - 1, (void**)&last); + last->obj = obj; + break; + } + case JSON_ARRAY: + flexarray_append(dst->u.array, obj); + break; + default: LIBXL__LOG(libxl__gc_owner(gc), LIBXL__LOG_ERROR, - "Try to add a value to an empty map (with no key)"); - return -1; + "Try append an object is not a map/array (%i)\n", + dst->type); + return ERROR_FAIL; } - flexarray_get(dst->u.map, dst->u.map->count - 1, (void**)&last); - last->obj = obj; - break; - } - case JSON_ARRAY: - flexarray_append(dst->u.array, obj); - break; - default: - LIBXL__LOG(libxl__gc_owner(gc), LIBXL__LOG_ERROR, - "Try append an object is not a map/array (%i)\n", - dst->type); - return -1; } obj->parent = dst; + + if (libxl__json_object_is_map(obj) || libxl__json_object_is_array(obj)) + ctx->current = obj; + if (ctx->head == NULL) + ctx->head = obj; + return 0; } @@ -630,9 +637,8 @@ static int json_callback_null(void *opaque) obj = libxl__json_object_alloc(ctx->gc, JSON_NULL); - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) return 0; - } return 1; } @@ -647,9 +653,8 @@ static int json_callback_boolean(void *opaque, int boolean) obj = libxl__json_object_alloc(ctx->gc, JSON_BOOL); obj->u.b = boolean; - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) return 0; - } return 1; } @@ -704,9 +709,8 @@ error: obj->u.string = t; out: - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) return 0; - } return 1; } @@ -728,9 +732,8 @@ static int json_callback_string(void *opaque, const unsigned char *str, obj = libxl__json_object_alloc(ctx->gc, JSON_STRING); obj->u.string = t; - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) return 0; - } return 1; } @@ -776,16 +779,8 @@ static int json_callback_start_map(void *opaque) obj = libxl__json_object_alloc(ctx->gc, JSON_MAP); - if (ctx->current) { - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { - return 0; - } - } - - ctx->current = obj; - if (ctx->head == NULL) { - ctx->head = obj; - } + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) + return 0; return 1; } @@ -816,16 +811,8 @@ static int json_callback_start_array(void *opaque) obj = libxl__json_object_alloc(ctx->gc, JSON_ARRAY); - if (ctx->current) { - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { - return 0; - } - } - - ctx->current = obj; - if (ctx->head == NULL) { - ctx->head = obj; - } + if (libxl__json_object_append_to(ctx->gc, obj, ctx)) + return 0; return 1; } -- 1.7.10.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |