From: Wei Liu Subject: [PATCH RFC 6/9] libxl_json: allow basic JSON type objects generation Date: Thu, 10 Apr 2014 16:40:19 +0100 Message-ID: <1397144422-18906-8-git-send-email-wei.liu2@citrix.com> References: <1397144422-18906-1-git-send-email-wei.liu2@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1397144422-18906-1-git-send-email-wei.liu2@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Wei Liu , ian.jackson@eu.citrix.com, ian.campbell@citrix.com List-Id: xen-devel@lists.xenproject.org The original logis 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 --- tools/libxl/libxl_json.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c index 34ac035..675abe3 100644 --- a/tools/libxl/libxl_json.c +++ b/tools/libxl/libxl_json.c @@ -629,8 +629,14 @@ 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) { - return 0; + if (ctx->current) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + return 0; + } + } + + if (ctx->head == NULL) { + ctx->head = obj; } return 1; @@ -646,8 +652,14 @@ 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) { - return 0; + if (ctx->current) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + return 0; + } + } + + if (ctx->head == NULL) { + ctx->head = obj; } return 1; @@ -703,8 +715,14 @@ error: obj->u.string = t; out: - if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { - return 0; + if (ctx->current) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + return 0; + } + } + + if (ctx->head == NULL) { + ctx->head = obj; } return 1; @@ -727,8 +745,14 @@ 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) { - return 0; + if (ctx->current) { + if (libxl__json_object_append_to(ctx->gc, obj, ctx->current) == -1) { + return 0; + } + } + + if (ctx->head == NULL) { + ctx->head = obj; } return 1; -- 1.7.10.4