[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen master] libxl_json: allow basic JSON type objects generation



commit 1f7752978189640fb4725922e901233065d5a3d7
Author:     Wei Liu <wei.liu2@xxxxxxxxxx>
AuthorDate: Tue May 13 22:53:59 2014 +0100
Commit:     Ian Campbell <ian.campbell@xxxxxxxxxx>
CommitDate: Wed May 21 11:01:13 2014 +0100

    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>
    Acked-by: Ian Campbell <ian.campbell@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 5974f00..082749e 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 65d4966..ab964ab 100644
--- a/tools/libxl/libxl_json.c
+++ b/tools/libxl/libxl_json.c
@@ -234,36 +234,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;
 }
 
@@ -449,9 +456,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;
 }
@@ -466,9 +472,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;
 }
@@ -523,9 +528,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;
 }
@@ -547,9 +551,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;
 }
@@ -595,16 +598,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;
 }
@@ -635,16 +630,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;
 }
--
generated by git-patchbot for /home/xen/git/xen.git#master

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.