Skip to contentSkip to conversation

Add JSON enconding and formatted printing to QObject

PATCH In review Paolo Bonzini 2 participants 13 messages Oct 17, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Add JSON enconding and formatted printing to QObject

This patch adds to the QObject system a method to encode JSON objects. It also adds printf-like capabilities where you can write %{a.b} in a format string and the method will walk a QDict to find the "a" first and "b" in a QDict inside that one (and then print what's found).

Since QEMU does not have (or I could not find...) a variable-length string buffer object, I added that to QString, thus making it mutable. I talked to Luiz and he mentioned that QString were immutable not by design, but rather due to not having the need so far.

Paolo Bonzini (7):
add qemu_memdup
allow passing NULL to qobject_type
forward declare all QObject subclasses in qobject.h add mutable qstring functions
add json encoder for qobjects
add testsuite for qobject json encoder
add formatted printing of QObjects

#1 lore raw

PATCH 1/7 add qemu_memdup

A nice-to-have utility function, it helps avoiding useless strlen+strdup pairs.

Cc Luiz Capitulino <[email protected]>
---
 qemu-common.h |    1 +
 qemu-malloc.c |    8 ++++++++
 2 files changed, 9 insertions(+), 0 deletions(-)
qemu-common.h +1 -0
index 820dd37..351f1ac 100644
@@ -141,6 +141,7 @@ void *qemu_realloc(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
 void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
+void *qemu_memdup(const void *str, size_t size);
 char *qemu_strndup(const char *str, size_t size);
 
 void *get_mmap_addr(unsigned long size);
qemu-malloc.c +8 -0
index 295d185..9e11e4b 100644
@@ -70,6 +70,14 @@ void *qemu_mallocz(size_t size)
     return ptr;
 }
 
+void *qemu_memdup(const void *str, size_t n)
+{
+    char *ptr;
+    ptr = qemu_malloc(n);
+    memcpy(ptr, str, n);
+    return ptr;
+}
+
 char *qemu_strdup(const char *str)
 {
     char *ptr;
Signature · 1 line
1.6.2.5
#2 lore raw

PATCH 2/7 allow passing NULL to qobject_type

In reply to Paolo Bonzini
Cc Luiz Capitulino <[email protected]>
---
 qobject.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
qobject.h +2 -0
index 4cc9287..200644d 100644
@@ -102,6 +102,8 @@ static inline void qobject_decref(QObject *obj)
  */
 static inline qtype_code qobject_type(const QObject *obj)
 {
+    if (!obj)
+        return QTYPE_NONE;
     assert(obj->type != NULL);
     return obj->type->code;
 }
Signature · 1 line
1.6.2.5
#3 lore raw

PATCH 3/7 forward declare all QObject subclasses in qobject.h

In reply to Paolo Bonzini

This gives more freedom to define prototypes without dragging in the whole header files.

Cc Luiz Capitulino <[email protected]>
---
 qobject.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
qobject.h +4 -0
index 200644d..f5c78b2 100644
@@ -44,6 +44,10 @@ typedef enum {
 } qtype_code;
 
 struct QObject;
+struct QInt;
+struct QString;
+struct QDict;
+struct QList;
 
 typedef struct QType {
     qtype_code code;
Signature · 1 line
1.6.2.5
#4 lore raw

Re: [Qemu-devel] [PATCH 3/7] forward declare all QObject subclasses in qobject.h

Quoted 14 lines · Paolo Bonzini

This gives more freedom to define prototypes without dragging in the whole header files.

Cc Luiz Capitulino <[email protected]>
---
 qobject.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
qobject.h +0 -0
index 200644d..f5c78b2 100644

qobject.h typedef enum { · line 51

Quoted 8 lines
qobject.h +4 -0
@@ -44,6 +44,10 @@ typedef enum {
 } qtype_code;
 
 struct QObject;
+struct QInt;
+struct QString;
+struct QDict;
+struct QList;
 

This seems like a layering violation to me.

Regards,

Anthony Liguori

#5 lore raw

PATCH 4/7 add mutable qstring functions

In reply to Paolo Bonzini

qemu does not have a string buffer object, so I added this capability to QString.

Cc Luiz Capitulino <[email protected]>
---
 check-qstring.c |   79 ++++++++++++++++++++++++++++++++++++++++-
 qstring.c       |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 qstring.h       |    5 +++
 3 files changed, 186 insertions(+), 3 deletions(-)
check-qstring.c +78 -1
index ea4dfd0..842cd7f 100644
@@ -17,6 +17,23 @@
  * (with some violations to access 'private' data)
  */
 
+START_TEST(qstring_new_test)
+{
+    QString *qstring;
+
+    qstring = qstring_new();
+    fail_unless(qstring != NULL);
+    fail_unless(qstring->base.refcnt == 1);
+    fail_unless(qstring->n == 0);
+    fail_unless(qstring->alloc < 1000);
+    fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
+
+    // destroy doesn't exit yet
+    qemu_free(qstring->string);
+    qemu_free(qstring);
+}
+END_TEST
+
 START_TEST(qstring_from_str_test)
 {
     QString *qstring;
@@ -25,7 +42,9 @@ START_TEST(qstring_from_str_test)
     qstring = qstring_from_str(str);
     fail_unless(qstring != NULL);
     fail_unless(qstring->base.refcnt == 1);
-    fail_unless(strcmp(str, qstring->string) == 0);
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp(str, qstring->string, 4) == 0);
     fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
 
     // destroy doesn't exit yet
@@ -55,6 +74,60 @@ START_TEST(qstring_get_str_test)
 }
 END_TEST
 
+START_TEST(qstring_append_test)
+{
+    QString *qstring;
+    const char *str = "QEM";
+    const char *longstr = "QEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMU";
+
+    qstring = qstring_from_str(str);
+    qstring_append(qstring, "U");
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp(longstr, qstring->string, 4) == 0);
+
+    qstring_append(qstring, "Q");
+    fail_unless(qstring->n == 5);
+    fail_unless(qstring->alloc >= 5);
+    fail_unless(memcmp(longstr, qstring->string, 5) == 0);
+
+    qstring_append(qstring, longstr + 5);
+    fail_unless(qstring->n == strlen (longstr));
+    fail_unless(qstring->alloc >= qstring->n);
+    fail_unless(memcmp(longstr, qstring->string, qstring->n) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
+START_TEST(qstring_append_ch_test)
+{
+    QString *qstring;
+    const char *str = "QEM";
+
+    qstring = qstring_from_str(str);
+    qstring_append_ch(qstring, 'U');
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp("QEMU", qstring->string, 4) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
+START_TEST(qstring_append_escaped_test)
+{
+    QString *qstring;
+    const char *str = "\"Q\x0EMU\t";
+    const char *result = "\\\"Q\\u000eMU\\t";
+
+    qstring = qstring_new();
+    qstring_append_escaped(qstring, str);
+    fail_unless(qstring->n == strlen (result));
+    fail_unless(qstring->alloc >= qstring->n);
+    fail_unless(memcmp(result, qstring->string, strlen (result)) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
 START_TEST(qobject_to_qstring_test)
 {
     QString *qstring;
@@ -75,9 +148,13 @@ static Suite *qstring_suite(void)
 
     qstring_public_tcase = tcase_create("Public Interface");
     suite_add_tcase(s, qstring_public_tcase);
+    tcase_add_test(qstring_public_tcase, qstring_new_test);
     tcase_add_test(qstring_public_tcase, qstring_from_str_test);
     tcase_add_test(qstring_public_tcase, qstring_destroy_test);
     tcase_add_test(qstring_public_tcase, qstring_get_str_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_ch_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_escaped_test);
     tcase_add_test(qstring_public_tcase, qobject_to_qstring_test);
 
     return s;
qstring.c +103 -2

134 lines not on this page · raw message

qstring.h +5 -0
index e012cb7..6e16d58 100644
@@ -5,11 +5,16 @@
 
 typedef struct QString {
     QObject_HEAD;
+    size_t n, alloc;
     char *string;
 } QString;
 
+QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 const char *qstring_get_str(const QString *qstring);
+void qstring_append(QString *qstring, const char *str);
+void qstring_append_escaped(QString *qstring, const char *str);
+void qstring_append_ch(QString *qstring, char c);
 QString *qobject_to_qstring(const QObject *obj);
 
 #endif /* QSTRING_H */
Signature · 1 line
1.6.2.5
#6 lore raw

Re: [Qemu-devel] [PATCH 4/7] add mutable qstring functions

Quoted 223 lines · Paolo Bonzini

qemu does not have a string buffer object, so I added this capability to QString.

Cc Luiz Capitulino <[email protected]>
---
 check-qstring.c |   79 ++++++++++++++++++++++++++++++++++++++++-
 qstring.c       |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 qstring.h       |    5 +++
 3 files changed, 186 insertions(+), 3 deletions(-)
check-qstring.c +78 -1
index ea4dfd0..842cd7f 100644
@@ -17,6 +17,23 @@
  * (with some violations to access 'private' data)
  */
 
+START_TEST(qstring_new_test)
+{
+    QString *qstring;
+
+    qstring = qstring_new();
+    fail_unless(qstring != NULL);
+    fail_unless(qstring->base.refcnt == 1);
+    fail_unless(qstring->n == 0);
+    fail_unless(qstring->alloc < 1000);
+    fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
+
+    // destroy doesn't exit yet
+    qemu_free(qstring->string);
+    qemu_free(qstring);
+}
+END_TEST
+
 START_TEST(qstring_from_str_test)
 {
     QString *qstring;
@@ -25,7 +42,9 @@ START_TEST(qstring_from_str_test)
     qstring = qstring_from_str(str);
     fail_unless(qstring != NULL);
     fail_unless(qstring->base.refcnt == 1);
-    fail_unless(strcmp(str, qstring->string) == 0);
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp(str, qstring->string, 4) == 0);
     fail_unless(qobject_type(QOBJECT(qstring)) == QTYPE_QSTRING);
 
     // destroy doesn't exit yet
@@ -55,6 +74,60 @@ START_TEST(qstring_get_str_test)
 }
 END_TEST
 
+START_TEST(qstring_append_test)
+{
+    QString *qstring;
+    const char *str = "QEM";
+    const char *longstr = "QEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMUQEMU";
+
+    qstring = qstring_from_str(str);
+    qstring_append(qstring, "U");
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp(longstr, qstring->string, 4) == 0);
+
+    qstring_append(qstring, "Q");
+    fail_unless(qstring->n == 5);
+    fail_unless(qstring->alloc >= 5);
+    fail_unless(memcmp(longstr, qstring->string, 5) == 0);
+
+    qstring_append(qstring, longstr + 5);
+    fail_unless(qstring->n == strlen (longstr));
+    fail_unless(qstring->alloc >= qstring->n);
+    fail_unless(memcmp(longstr, qstring->string, qstring->n) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
+START_TEST(qstring_append_ch_test)
+{
+    QString *qstring;
+    const char *str = "QEM";
+
+    qstring = qstring_from_str(str);
+    qstring_append_ch(qstring, 'U');
+    fail_unless(qstring->n == 4);
+    fail_unless(qstring->alloc >= 4);
+    fail_unless(memcmp("QEMU", qstring->string, 4) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
+START_TEST(qstring_append_escaped_test)
+{
+    QString *qstring;
+    const char *str = "\"Q\x0EMU\t";
+    const char *result = "\\\"Q\\u000eMU\\t";
+
+    qstring = qstring_new();
+    qstring_append_escaped(qstring, str);
+    fail_unless(qstring->n == strlen (result));
+    fail_unless(qstring->alloc >= qstring->n);
+    fail_unless(memcmp(result, qstring->string, strlen (result)) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
 START_TEST(qobject_to_qstring_test)
 {
     QString *qstring;
@@ -75,9 +148,13 @@ static Suite *qstring_suite(void)
 
     qstring_public_tcase = tcase_create("Public Interface");
     suite_add_tcase(s, qstring_public_tcase);
+    tcase_add_test(qstring_public_tcase, qstring_new_test);
     tcase_add_test(qstring_public_tcase, qstring_from_str_test);
     tcase_add_test(qstring_public_tcase, qstring_destroy_test);
     tcase_add_test(qstring_public_tcase, qstring_get_str_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_ch_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_escaped_test);
     tcase_add_test(qstring_public_tcase, qobject_to_qstring_test);
 
     return s;
qstring.c +74 -1
index 6d411da..ab77fba 100644
@@ -21,6 +21,29 @@ static const QType qstring_type = {
 };
 
 /**
+ * Invariant: all strings have an empty byte at the end so that
+ * it is easy to convert them to C strings.
+ */
+
+
+/**
+ * qstring_new(): Create a new empty QString
+ *
+ * Return strong reference.
+ */
+QString *qstring_new(void)
+{
+    QString *qstring;
+    qstring = qemu_malloc(sizeof(*qstring));
+    qstring->n = 0;
+    qstring->alloc = 16;
+    qstring->string = qemu_malloc(qstring->alloc);
+    QOBJECT_INIT(qstring, &qstring_type);
+
+    return qstring;
+}
+
+/**
  * qstring_from_str(): Create a new QString from a regular C string
  *
  * Return strong reference.
@@ -28,15 +51,91 @@ static const QType qstring_type = {
 QString *qstring_from_str(const char *str)
 {
     QString *qstring;
+    size_t n = strlen(str);
 
     qstring = qemu_malloc(sizeof(*qstring));
-    qstring->string = qemu_strdup(str);
+    qstring->n = n;
+    qstring->alloc = n + 1;
+    qstring->string = qemu_memdup(str, qstring->alloc);
     QOBJECT_INIT(qstring, &qstring_type);
 
     return qstring;
 }
 
 /**
+ * qstring_append(): Append a regular C string to a QString
+ */
+void qstring_append(QString *qstring, const char *str)
+{
+    size_t n = strlen(str);
+    size_t total = qstring->n + n + 1;
+
+    if (total > qstring->alloc) {
+        if (qstring->alloc * 2 < total) {
+            qstring->alloc = total;
+        } else {
+            qstring->alloc *= 2;
+        }
+        qstring->string = qemu_realloc (qstring->string, qstring->alloc);
+    }
+    memcpy (qstring->string + qstring->n, str, n + 1);
+    qstring->n += n;
+}
+
+/**
+ * qstring_append(): Append a regular C string to a QString, escaping it
+ * according to JSON syntax.
+ */
+void qstring_append_escaped(QString *qstring, const char *str)
+{
+    for (; *str; str++) {
+        unsigned char ch = *str;
+        switch (*str) {
+        case '\f': ch = 'f'; goto backslash;
+        case '\n': ch = 'n'; goto backslash;
+        case '\r': ch = 'r'; goto backslash;
+        case '\t': ch = 't'; goto backslash;
+        case '\b': ch = 'b'; goto backslash;
+
+        backslash:
+        case '\\':
+        case '\"':
+            qstring_append_ch (qstring, '\\');
+            break;
+
+        default:
+            if (ch < 0x20) {
+                    qstring_append_ch (qstring, '\\');
+                    qstring_append_ch (qstring, 'u');
+                    qstring_append_ch (qstring, '0');
+                    qstring_append_ch (qstring, '0');
+                    qstring_append_ch (qstring, '0' + (ch >> 4));

qstring.c static const QType qstring_type = { · line 122

Quoted 10 lines
qstring.c +8 -0
+                    ch = (ch & 15) + ((ch & 15) > 9 ? 'a' - 10 : '0');
+            }
+            break;
+        }
+
+        qstring_append_ch (qstring, ch);
+    }
+}
 
 

The json parser should do the escaping.

Regards,

Anthony Liguori

#7 lore raw

PATCH 5/7 add json encoder for qobjects

In reply to Paolo Bonzini

The JSON encoder is just a virtual method on QObject.

Cc Luiz Capitulino <[email protected]>
---
 Makefile  |    4 ++--
 qdict.c   |   32 ++++++++++++++++++++++++++++++++
 qint.c    |   16 ++++++++++++++++
 qlist.c   |   28 ++++++++++++++++++++++++++++
 qobject.h |   10 ++++++++++
 qstring.c |   32 ++++++++++++++++++++++++++++++++
 qstring.h |    5 +++++
 7 files changed, 125 insertions(+), 2 deletions(-)
Makefile +2 -2
index 7d4d75c..2447d23 100644
@@ -210,10 +210,10 @@ qemu-io$(EXESUF):  qemu-io.o qemu-tool.o cmd.o $(block-obj-y)
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
 
-check-qint: check-qint.o qint.o qemu-malloc.o
+check-qint: check-qint.o qint.o qstring.o qemu-malloc.o
 check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
-check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
+check-qlist: check-qlist.o qlist.o qint.o qstring.o qemu-malloc.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
qdict.c +32 -0
index a302f4c..efbc53d 100644
@@ -18,10 +18,12 @@
 #include "qemu-common.h"
 
 static void qdict_destroy_obj(QObject *obj);
+static void qdict_encode_json(const QObject *obj, QString *str);
 
 static const QType qdict_type = {
     .code = QTYPE_QDICT,
     .destroy = qdict_destroy_obj,
+    .encode_json = qdict_encode_json,
 };
 
 /**
@@ -295,3 +297,33 @@ static void qdict_destroy_obj(QObject *obj)
 
     qemu_free(qdict);
 }
+
+/**
+ * qdict_encode_json(): Encode the dictionary to JSON on a QString.
+ */
+static void qdict_encode_json(const QObject *obj, QString *str)
+{
+    int i, first;
+    const QDict *qdict;
+
+    assert(obj != NULL);
+    qdict = qobject_to_qdict((QObject *) obj);
+
+    qstring_append_ch (str, '{');
+    for (first = 1, i = 0; i < QDICT_HASH_SIZE; i++) {
+        QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
+        while (entry) {
+            if (!first)
+                qstring_append_ch (str, ',');
+            qstring_append_ch (str, '"');
+            qstring_append_escaped (str, entry->key);
+            qstring_append_ch (str, '"');
+            qstring_append_ch (str, ':');
+            qobject_encode_json (entry->value, str);
+            entry = QLIST_NEXT(entry, next);
+            first = 0;
+        }
+    }
+
+    qstring_append_ch (str, '}');
+}
qint.c +16 -0
index 447e847..b5ceea3 100644
@@ -10,14 +10,17 @@
  * the COPYING file in the top-level directory.
  */
 #include "qint.h"
+#include "qstring.h"
 #include "qobject.h"
 #include "qemu-common.h"
 
 static void qint_destroy_obj(QObject *obj);
+static void qint_encode_json(const QObject *obj, QString *str);
 
 static const QType qint_type = {
     .code = QTYPE_QINT,
     .destroy = qint_destroy_obj,
+    .encode_json = qint_encode_json,
 };
 
 /**
@@ -64,3 +67,16 @@ static void qint_destroy_obj(QObject *obj)
     assert(obj != NULL);
     qemu_free(qobject_to_qint(obj));
 }
+
+/**
+ * qint_encode_json(): Encode the integer to JSON on a QString.
+ */
+static void qint_encode_json(const QObject *obj, QString *str)
+{
+    char buf[32];
+    QInt *qint;
+
+    qint = qobject_to_qint((QObject *) obj);
+    sprintf (buf, "%" PRId64, qint->value);
+    qstring_append (str, buf);
+}
qlist.c +28 -0
index ba2c66c..4c7e1b2 100644
@@ -10,15 +10,18 @@
  * the COPYING file in the top-level directory.
  */
 #include "qlist.h"
+#include "qstring.h"
 #include "qobject.h"
 #include "qemu-queue.h"
 #include "qemu-common.h"
 
 static void qlist_destroy_obj(QObject *obj);
+static void qlist_encode_json(const QObject *obj, QString *str);
 
 static const QType qlist_type = {
     .code = QTYPE_QLIST,
     .destroy = qlist_destroy_obj,
+    .encode_json = qlist_encode_json,
 };
  
 /**
@@ -98,3 +101,28 @@ static void qlist_destroy_obj(QObject *obj)
 
     qemu_free(qlist);
 }
+
+/**
+ * qlist_encode_json(): Encode the list to JSON on a QString.
+ */
+static void qlist_encode_json(const QObject *obj, QString *str)
+{
+    const QList *qlist;
+    QListEntry *entry;
+    int first;
+
+    assert(obj != NULL);
+    qlist = qobject_to_qlist((QObject *) obj);
+
+    first = 1;
+    qstring_append_ch (str, '[');
+    QTAILQ_FOREACH(entry, &qlist->head, next) {
+        if (!first)
+            qstring_append_ch (str, ',');
+        qobject_encode_json (entry->value, str);
+        first = 0;
+    }
+
+    qstring_append_ch (str, ']');
+}
+
qobject.h +10 -0
index f5c78b2..d941a73 100644
@@ -52,6 +52,7 @@ struct QList;
 typedef struct QType {
     qtype_code code;
     void (*destroy)(struct QObject *);
+    void (*encode_json)(const struct QObject *, struct QString *);
 } QType;
 
 typedef struct QObject {
@@ -112,4 +113,13 @@ static inline qtype_code qobject_type(const QObject *obj)
     return obj->type->code;
 }
 
+/**
+ * qobject_type(): Return the QObject's type
+ */
+static inline void qobject_encode_json(const QObject *obj, struct QString *str)
+{
+    assert(obj->type != NULL);
+    obj->type->encode_json (obj, str);
+}
+
 #endif /* QOBJECT_H */
qstring.c +32 -0
index ab77fba..10c1952 100644
@@ -14,10 +14,12 @@
 #include "qemu-common.h"
 
 static void qstring_destroy_obj(QObject *obj);
+static void qstring_encode_json(const QObject *obj, QString *str);
 
 static const QType qstring_type = {
     .code = QTYPE_QSTRING,
     .destroy = qstring_destroy_obj,
+    .encode_json = qstring_encode_json,
 };
 
 /**
@@ -63,6 +65,22 @@ QString *qstring_from_str(const char *str)
 }
 
 /**
+ * qstring_json_from_qobject_obj(): Encode a QObject as JSON and return
+ * a QString with the result.
+ *
+ * Return strong reference.
+ */
+QString *qstring_json_from_qobject_obj(const QObject *qobject)
+{
+    QString *qstring;
+
+    qstring = qstring_new();
+    qobject_encode_json(qobject, qstring);
+    return qstring;
+}
+
+
+/**
  * qstring_append(): Append a regular C string to a QString
  */
 void qstring_append(QString *qstring, const char *str)
@@ -172,3 +190,17 @@ static void qstring_destroy_obj(QObject *obj)
     qemu_free(qs->string);
     qemu_free(qs);
 }
+
+/**
+ * qstring_encode_json(): Encode the string to JSON on a QString.
+ */
+static void qstring_encode_json(const QObject *obj, QString *str)
+{
+    QString *qstring;
+
+    assert(obj != NULL);
+    qstring = qobject_to_qstring((QObject *) obj);
+    qstring_append_ch (str, '"');
+    qstring_append_escaped (str, qstring_get_str(qstring));
+    qstring_append_ch (str, '"');
+}
qstring.h +5 -0
index 6e16d58..efc1d7a 100644
@@ -11,6 +11,11 @@ typedef struct QString {
 
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
+QString *qstring_json_from_qobject_obj(const QObject *obj);
+
+#define qstring_json_from_qobject(obj) \
+        qstring_json_from_qobject_obj(QOBJECT(obj))
+
 const char *qstring_get_str(const QString *qstring);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_escaped(QString *qstring, const char *str);
Signature · 1 line
1.6.2.5
#8 lore raw

Re: [Qemu-devel] [PATCH 5/7] add json encoder for qobjects

Quoted 2 lines · Paolo Bonzini

The JSON encoder is just a virtual method on QObject.

I think it makes more sense as an external entity.

Regards,

Anthony Liguori

#9 lore raw

Re: [Qemu-devel] [PATCH 5/7] add json encoder for qobjects

Quoted 2 lines · Anthony Liguori

I think it makes more sense as an external entity.

Maybe... I just thought of it as a toString method that happens to emit JSON.

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH 5/7] add json encoder for qobjects

Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Anthony Liguori

I think it makes more sense as an external entity.

Maybe... I just thought of it as a toString method that happens to emit JSON.

json is one representation of the monitor protocol. I think we should attempt to design things so that there is a clear separate of representation from the implementation.

BTW, the next thing I'd like to do this weekend is write a QObject decoded based on the json parser. So that you can do something like:

int locked, readonly;
char *file;

err = qobject_unmarshal(obj, "{'locked': %i, 'file': %s, 'readonly':
%i}", &locked, &file, &readonly);

I think this would tremendously simplify the monitor command implementations.

Regards,

Anthony Liguori

#11 lore raw

PATCH 6/7 add testsuite for qobject json encoder

In reply to Paolo Bonzini

And finally, a small testsuite using check as the rest of the QObject framework.

Cc Luiz Capitulino <[email protected]>
---
 Makefile     |    1 +
 check-json.c |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure    |    2 +-
 3 files changed, 125 insertions(+), 1 deletions(-)
 create mode 100644 check-json.c
Makefile +1 -0
index 2447d23..2483057 100644
@@ -210,6 +210,7 @@ qemu-io$(EXESUF):  qemu-io.o qemu-tool.o cmd.o $(block-obj-y)
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
 
+check-json: check-json.o qint.o qstring.o qdict.o qlist.o qemu-malloc.o
 check-qint: check-qint.o qint.o qstring.o qemu-malloc.o
 check-qstring: check-qstring.o qstring.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
check-json.c new file +123 -0

123 lines not on this page · raw message

configure +1 -1
index 7c7804b..3d00f2e 100755
@@ -2024,7 +2024,7 @@ if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then
   if [ "$linux" = "yes" ] ; then
       tools="qemu-nbd\$(EXESUF) qemu-io\$(EXESUF) $tools"
     if [ "$check_utests" = "yes" ]; then
-      tools="check-qint check-qstring check-qdict check-qlist $tools"
+      tools="check-qint check-qstring check-qdict check-qlist check-json $tools"
     fi
   elif test "$mingw32" = "yes" ; then
       tools="qemu-io\$(EXESUF) $tools"
Signature · 1 line
1.6.2.5
#12 lore raw

PATCH 7/7 add formatted printing of QObjects

In reply to Paolo Bonzini

This patch adds a function to format a QDict according to a string that is printf like but specifies how to navigate the QDict. All parts are printed as JSON, but this is fine since this will auto-escape strings and integers print naturally.

More sophisticated flags can be added later, e.g. %#{foo} would print a string without escaping.

Cc Luiz Capitulino <[email protected]>
---
 Makefile     |    6 +++---
 check-json.c |   39 +++++++++++++++++++++++++++++++++++++++
 qstring.c    |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 qstring.h    |    3 +++
 4 files changed, 96 insertions(+), 3 deletions(-)
Makefile +3 -3
index 2483057..f98b70c 100644
@@ -211,10 +211,10 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
 
 check-json: check-json.o qint.o qstring.o qdict.o qlist.o qemu-malloc.o
-check-qint: check-qint.o qint.o qstring.o qemu-malloc.o
-check-qstring: check-qstring.o qstring.o qemu-malloc.o
+check-qint: check-qint.o qint.o qstring.o qdict.o qemu-malloc.o
+check-qstring: check-qstring.o qint.o qstring.o qdict.o qemu-malloc.o
 check-qdict: check-qdict.o qdict.o qint.o qstring.o qemu-malloc.o
-check-qlist: check-qlist.o qlist.o qint.o qstring.o qemu-malloc.o
+check-qlist: check-qlist.o qlist.o qint.o qstring.o qdict.o qemu-malloc.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-json.c +39 -0
index 9f4cfbe..eaff53b 100644
@@ -21,6 +21,13 @@
     QDECREF(result);                                            \
   } while (0)
 
+#define format_expect(qobj, format, expected)                   \
+  do {                                                          \
+    QString *result = qstring_format (format, qobj);            \
+    fail_unless (!strcmp (qstring_get_str (result), expected)); \
+    QDECREF(result);                                            \
+  } while (0)
+
 START_TEST(qint_json_test)
 {
     QInt *qi;
@@ -88,6 +95,37 @@ START_TEST(nested_json_test)
 }
 END_TEST
 
+START_TEST(format_test)
+{
+    QDict *qdict_in, *qdict_out;
+    QList *qlist;
+
+    format_expect(NULL, "test", "test");
+    format_expect(NULL, "test %%", "test %");
+
+    qdict_in = qdict_new();
+    format_expect(qdict_in, "test %%", "test %");
+    format_expect(qdict_in, "test %{} test", "test {} test");
+    format_expect(qdict_in, "%%%{}%%", "%{}%");
+
+    qdict_put_obj(qdict_in, "in", QOBJECT(qint_from_int(42)));
+    format_expect(qdict_in, "%{}", "{\"in\":42}");
+    format_expect(qdict_in, "test %{in} test", "test 42 test");
+
+    qlist = qlist_new();
+    qlist_append(qlist, qdict_in);
+    QINCREF(qlist);
+
+    qdict_out = qdict_new();
+    qdict_put_obj(qdict_out, "list", QOBJECT(qlist));
+    qdict_put_obj(qdict_out, "in", QOBJECT(qdict_in));
+    format_expect(qdict_out, "%{list}", "[{\"in\":42}]");
+    format_expect(qdict_out, "%{in.in}", "42");
+    format_expect(qdict_out, "%{in.in}%{in.in}", "4242");
+    QDECREF(qdict_out);
+}
+END_TEST
+
 static Suite *QObject_json_suite(void)
 {
     Suite *s;
@@ -102,6 +140,7 @@ static Suite *QObject_json_suite(void)
     tcase_add_test(qobject_json_tcase, qdict_json_test);
     tcase_add_test(qobject_json_tcase, qlist_json_test);
     tcase_add_test(qobject_json_tcase, nested_json_test);
+    tcase_add_test(qobject_json_tcase, format_test);
 
     return s;
 }
qstring.c +51 -0

69 lines not on this page · raw message

qstring.h +3 -0
index efc1d7a..2d1fec9 100644
@@ -11,6 +11,7 @@ typedef struct QString {
 
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
+QString *qstring_format(const char *format, const struct QDict *qdict);
 QString *qstring_json_from_qobject_obj(const QObject *obj);
 
 #define qstring_json_from_qobject(obj) \
@@ -18,6 +19,8 @@ QString *qstring_json_from_qobject_obj(const QObject *obj);
 
 const char *qstring_get_str(const QString *qstring);
 void qstring_append(QString *qstring, const char *str);
+void qstring_append_format(QString *qstring, const char *format,
+                           const struct QDict *qdict);
 void qstring_append_escaped(QString *qstring, const char *str);
 void qstring_append_ch(QString *qstring, char c);
 QString *qobject_to_qstring(const QObject *obj);
Signature · 1 line
1.6.2.5