qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH v4 22/28] qobject: Consolidate qobject_to_json() cal


From: Eric Blake
Subject: [Qemu-devel] [PATCH v4 22/28] qobject: Consolidate qobject_to_json() calls
Date: Wed, 18 May 2016 22:41:08 -0600

It's simpler to have a single conversion function that takes a
bool parameter, rather than two functions where the choice of
function determines an internal bool.  Similar to commit fc471c18.

While at it, the conversion currently cannot fail (maybe it SHOULD
be possible to choose to fail, when encountering invalid UTF-8
encoding or an Infinity or NaN valued double, but that's a story
for another day), so clean up callers to avoid a needless assert.
And use bool rather than int for 'pretty'.

Signed-off-by: Eric Blake <address@hidden>

---
v4: new patch
---
 include/qapi/qmp/qobject-json.h    |  4 ++--
 block.c                            |  2 +-
 monitor.c                          |  4 +---
 qemu-img.c                         |  9 +++------
 qga/main.c                         |  5 +----
 qobject/qobject-json.c             | 19 +++++--------------
 tests/check-qobject-json.c         | 27 +++++++++++----------------
 tests/libqtest.c                   |  2 +-
 tests/test-visitor-serialization.c |  2 +-
 9 files changed, 26 insertions(+), 48 deletions(-)

diff --git a/include/qapi/qmp/qobject-json.h b/include/qapi/qmp/qobject-json.h
index 0749e7e..e4d11cf 100644
--- a/include/qapi/qmp/qobject-json.h
+++ b/include/qapi/qmp/qobject-json.h
@@ -21,8 +21,8 @@ QObject *qobject_from_json(const char *string);
 QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
 QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 
0);

-QString *qobject_to_json(const QObject *obj);
-QString *qobject_to_json_pretty(const QObject *obj);
+/* Convert the object to QString; does not fail. */
+QString *qobject_to_json(const QObject *obj, bool pretty);

 int qstring_append_json_string(QString *qstring, const char *str);
 int qstring_append_json_number(QString *qstring, double number);
diff --git a/block.c b/block.c
index 32d06b5..7f80d31 100644
--- a/block.c
+++ b/block.c
@@ -4006,7 +4006,7 @@ void bdrv_refresh_filename(BlockDriverState *bs)
     if (bs->exact_filename[0]) {
         pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
     } else if (bs->full_open_options) {
-        QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
+        QString *json = qobject_to_json(QOBJECT(bs->full_open_options), false);
         snprintf(bs->filename, sizeof(bs->filename), "json:%s",
                  qstring_get_str(json));
         QDECREF(json);
diff --git a/monitor.c b/monitor.c
index 934d539..f381673 100644
--- a/monitor.c
+++ b/monitor.c
@@ -389,9 +389,7 @@ static void monitor_json_emitter(Monitor *mon, const 
QObject *data)
 {
     QString *json;

-    json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
-                                             qobject_to_json(data);
-    assert(json != NULL);
+    json = qobject_to_json(data, mon->flags & MONITOR_USE_PRETTY);

     qstring_append_chr(json, '\n');
     monitor_puts(mon, qstring_get_str(json));
diff --git a/qemu-img.c b/qemu-img.c
index 39bff77..086530f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -485,8 +485,7 @@ static void dump_json_image_check(ImageCheck *check, bool 
quiet)

     visit_type_ImageCheck(v, NULL, &check, &error_abort);
     visit_complete(v, &obj);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
+    str = qobject_to_json(obj, true);
     qprintf(quiet, "%s\n", qstring_get_str(str));
     qobject_decref(obj);
     visit_free(v);
@@ -2175,8 +2174,7 @@ static void dump_json_image_info_list(ImageInfoList *list)

     visit_type_ImageInfoList(v, NULL, &list, &error_abort);
     visit_complete(v, &obj);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
+    str = qobject_to_json(obj, true);
     printf("%s\n", qstring_get_str(str));
     qobject_decref(obj);
     visit_free(v);
@@ -2191,8 +2189,7 @@ static void dump_json_image_info(ImageInfo *info)

     visit_type_ImageInfo(v, NULL, &info, &error_abort);
     visit_complete(v, &obj);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
+    str = qobject_to_json(obj, true);
     printf("%s\n", qstring_get_str(str));
     qobject_decref(obj);
     visit_free(v);
diff --git a/qga/main.c b/qga/main.c
index 1c0315e..13545b2 100644
--- a/qga/main.c
+++ b/qga/main.c
@@ -523,10 +523,7 @@ static int send_response(GAState *s, QObject *payload)

     g_assert(payload && s->channel);

-    payload_qstr = qobject_to_json(payload);
-    if (!payload_qstr) {
-        return -EINVAL;
-    }
+    payload_qstr = qobject_to_json(payload, false);

     if (s->delimit_response) {
         s->delimit_response = false;
diff --git a/qobject/qobject-json.c b/qobject/qobject-json.c
index 95de587..9ace92b 100644
--- a/qobject/qobject-json.c
+++ b/qobject/qobject-json.c
@@ -72,12 +72,12 @@ QObject *qobject_from_jsonf(const char *string, ...)
 typedef struct ToJsonIterState
 {
     int indent;
-    int pretty;
+    bool pretty;
     int count;
     QString *str;
 } ToJsonIterState;

-static void to_json(const QObject *obj, QString *str, int pretty, int indent);
+static void to_json(const QObject *obj, QString *str, bool pretty, int indent);

 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 {
@@ -114,7 +114,7 @@ static void to_json_list_iter(QObject *obj, void *opaque)
     s->count++;
 }

-static void to_json(const QObject *obj, QString *str, int pretty, int indent)
+static void to_json(const QObject *obj, QString *str, bool pretty, int indent)
 {
     switch (qobject_type(obj)) {
     case QTYPE_QNULL:
@@ -185,20 +185,11 @@ static void to_json(const QObject *obj, QString *str, int 
pretty, int indent)
     }
 }

-QString *qobject_to_json(const QObject *obj)
+QString *qobject_to_json(const QObject *obj, bool pretty)
 {
     QString *str = qstring_new();

-    to_json(obj, str, 0, 0);
-
-    return str;
-}
-
-QString *qobject_to_json_pretty(const QObject *obj)
-{
-    QString *str = qstring_new();
-
-    to_json(obj, str, 1, 0);
+    to_json(obj, str, pretty, 0);

     return str;
 }
diff --git a/tests/check-qobject-json.c b/tests/check-qobject-json.c
index d889501..9814282 100644
--- a/tests/check-qobject-json.c
+++ b/tests/check-qobject-json.c
@@ -64,7 +64,7 @@ static void escaped_string(void)
         g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);

         if (test_cases[i].skip == 0) {
-            str = qobject_to_json(obj);
+            str = qobject_to_json(obj, false);
             g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
             qobject_decref(obj);
         }
@@ -98,7 +98,7 @@ static void simple_string(void)
         str = qobject_to_qstring(obj);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);

-        str = qobject_to_json(obj);
+        str = qobject_to_json(obj, false);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);

         qobject_decref(obj);
@@ -832,13 +832,8 @@ static void utf8_string(void)
         qobject_decref(obj);

         obj = QOBJECT(qstring_from_str(utf8_in));
-        str = qobject_to_json(obj);
-        if (json_out) {
-            g_assert(str);
-            g_assert_cmpstr(qstring_get_str(str), ==, json_out);
-        } else {
-            g_assert(!str);
-        }
+        str = qobject_to_json(obj, false);
+        g_assert_cmpstr(qstring_get_str(str), ==, json_out);
         QDECREF(str);
         qobject_decref(obj);

@@ -913,7 +908,7 @@ static void simple_number(void)
         if (test_cases[i].skip == 0) {
             QString *str;

-            str = qobject_to_json(obj);
+            str = qobject_to_json(obj, false);
             g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
             QDECREF(str);
         }
@@ -951,7 +946,7 @@ static void float_number(void)
         if (test_cases[i].skip == 0) {
             QString *str;

-            str = qobject_to_json(obj);
+            str = qobject_to_json(obj, false);
             g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
             QDECREF(str);
         }
@@ -1011,7 +1006,7 @@ static void keyword_literal(void)
     qbool = qobject_to_qbool(obj);
     g_assert(qbool_get_bool(qbool) == true);

-    str = qobject_to_json(obj);
+    str = qobject_to_json(obj, false);
     g_assert(strcmp(qstring_get_str(str), "true") == 0);
     QDECREF(str);

@@ -1024,7 +1019,7 @@ static void keyword_literal(void)
     qbool = qobject_to_qbool(obj);
     g_assert(qbool_get_bool(qbool) == false);

-    str = qobject_to_json(obj);
+    str = qobject_to_json(obj, false);
     g_assert(strcmp(qstring_get_str(str), "false") == 0);
     QDECREF(str);

@@ -1191,7 +1186,7 @@ static void simple_dict(void)

         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);

-        str = qobject_to_json(obj);
+        str = qobject_to_json(obj, false);
         qobject_decref(obj);

         obj = qobject_from_json(qstring_get_str(str));
@@ -1306,7 +1301,7 @@ static void simple_list(void)

         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);

-        str = qobject_to_json(obj);
+        str = qobject_to_json(obj, false);
         qobject_decref(obj);

         obj = qobject_from_json(qstring_get_str(str));
@@ -1374,7 +1369,7 @@ static void simple_whitespace(void)

         g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);

-        str = qobject_to_json(obj);
+        str = qobject_to_json(obj, false);
         qobject_decref(obj);

         obj = qobject_from_json(qstring_get_str(str));
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 2f42bc9..5371fc8 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -421,7 +421,7 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
     /* No need to send anything for an empty QObject.  */
     if (qobj) {
         int log = getenv("QTEST_LOG") != NULL;
-        QString *qstr = qobject_to_json(qobj);
+        QString *qstr = qobject_to_json(qobj, false);
         const char *str = qstring_get_str(qstr);
         size_t size = qstring_get_length(qstr);

diff --git a/tests/test-visitor-serialization.c 
b/tests/test-visitor-serialization.c
index 4da4b8e..7a1c071 100644
--- a/tests/test-visitor-serialization.c
+++ b/tests/test-visitor-serialization.c
@@ -1037,7 +1037,7 @@ static void qmp_deserialize(void **native_out, void 
*datap,

     visit_complete(d->qov, &d->obj);
     obj_orig = d->obj;
-    output_json = qobject_to_json(obj_orig);
+    output_json = qobject_to_json(obj_orig, false);
     obj = qobject_from_json(qstring_get_str(output_json));

     QDECREF(output_json);
-- 
2.5.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]