Patchwork [10/11] qjson: add vararg format for embedded qobjects

login
register
mail settings
Submitter Anthony Liguori
Date Oct. 17, 2009, 1:36 p.m.
Message ID <1255786571-3528-11-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/36311/
State New
Headers show

Comments

Anthony Liguori - Oct. 17, 2009, 1:36 p.m.
%p lets you specify an already constructed qobject.  It takes a weak reference.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 qjson.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

Patch

diff --git a/qjson.c b/qjson.c
index 9279bfb..7bedaf3 100644
--- a/qjson.c
+++ b/qjson.c
@@ -648,6 +648,28 @@  static QObject *parse_keyword(JSONParserContext *ctxt, const char *data, size_t
     return obj;
 }
 
+static QObject *parse_qobject(JSONParserContext *ctxt, const char *data, size_t *length, va_list *ap)
+{
+    const char *ptr = data;
+    QObject *obj = NULL;
+
+    ptr += parse_skip(ctxt, ptr);
+
+    if (ap && *ptr == '%') {
+        ptr++;
+        if (*ptr == 'p') {
+            ptr++;
+
+            obj = va_arg(*ap, QObject *);
+            qobject_incref(obj);
+            *length = (ptr - data);
+            return obj;
+        }
+    }
+
+    return NULL;
+}
+
 static QObject *parse_value(JSONParserContext *ctxt, const char *string, size_t *length, va_list *ap)
 {
     QObject *obj;
@@ -665,6 +687,9 @@  static QObject *parse_value(JSONParserContext *ctxt, const char *string, size_t
     if (obj == NULL) {
         obj = parse_keyword(ctxt, string, length);
     }
+    if (obj == NULL) {
+        obj = parse_qobject(ctxt, string, length, ap);
+    }
 
     return obj;
 }