Skip to contentSkip to conversation

Add qapi-to-JSON output visitor

PATCH Superseded Eric Blake 2 participants 23 messages Dec 22, 2015 – Dec 23, 2015

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

v2 cover Add qapi-to-JSON output visitor

Prerequisites:
+ my qapi cleanups subset E v8:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg03863.html

I wrote this series for several reasons:
1. I've been doing a lot of churn in the qapi visitor interfaces lately; adding a new visitor is good proof whether the changes still make sense
2. Alexander ended up writing his own simple JSON generator as qjson.c in his work for vmstate self-description, rather than reusing existing code, because the QObject JSON generator does not have an easy entry point
3. Fam commented while trying to enhance 'qemu-img map' that we are rather wasteful in that there is no way to go directly from a qapi type to JSON without an intermediate QObject creation

v2 notes:
Fix bugs so that 'make check' and qemu-iotests pass at all points. Rebase on top of qapi cleanups subset E v8.

Promote out of RFC. However, in this version, I'm still presenting two alternative solutions: either patch 8 (keep the qjson.h interface) or patches 10-11 (completely drop qjson.h, by inlining its contents but adding more glue in vmstate.c). Patch 9 is a revert to show that the two alternatives are orthogonal; the final series won't need a revert. I'd welcome opinions on which alternative to stick with.

Backport diffstat (renames of 1 and 14 confuse the stats):

001/14:[down] 'qapi: Rename (one) qjson.h to qobject-json.h' 002/14:[----] [--] 'qapi: Improve use of qmp/types.h' 003/14:[----] [--] 'qapi: Factor out JSON string escaping' 004/14:[0002] [FC] 'qapi: Factor out JSON number formatting' 005/14:[----] [--] 'qapi: Use qstring_append_chr() where appropriate' 006/14:[0016] [FC] 'qapi: Add qstring_append_format()' 007/14:[0063] [FC] 'qapi: add json output visitor' 008/14:[0008] [FC] 'qjson: Simplify by using json-output-visitor' 009/14:[down] 'Revert "qjson: Simplify by using json-output-visitor"' 010/14:[down] 'vmstate: use new JSON output visitor' 011/14:[down] 'qjson: Remove unused file'
012/14:[----] [--] 'qapi: Add qobject_to_json_pretty_prefix()' 013/14:[0004] [FC] 'qapi: Support pretty printing in JSON output visitor' 014/14:[down] 'qemu-img: Use new JSON output formatter'

v1 (RFC) notes:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01760.html

Eric Blake (14):
qapi: Rename (one) qjson.h to qobject-json.h
qapi: Improve use of qmp/types.h
qapi: Factor out JSON string escaping
qapi: Factor out JSON number formatting
qapi: Use qstring_append_chr() where appropriate
qapi: Add qstring_append_format()
qapi: add json output visitor
qjson: Simplify by using json-output-visitor Revert "qjson: Simplify by using json-output-visitor"
vmstate: use new JSON output visitor
qjson: Remove unused file
qapi: Add qobject_to_json_pretty_prefix()
qapi: Support pretty printing in JSON output visitor
qemu-img: Use new JSON output formatter

 MAINTAINERS                                   |   2 +-
 Makefile.objs                                 |   1 -
 balloon.c                                     |   2 +-
 block.c                                       |   2 +-
 block/archipelago.c                           |   2 +-
 block/nbd.c                                   |   2 +-
 block/quorum.c                                |   2 +-
 blockjob.c                                    |   2 +-
 hw/core/qdev.c                                |   2 +-
 hw/misc/pvpanic.c                             |   2 +-
 hw/net/virtio-net.c                           |   2 +-
 hw/pci/pcie_aer.c                             |   1 +
 include/migration/vmstate.h                   |   4 +-
 include/qapi/json-output-visitor.h            |  25 ++
 include/qapi/qmp/{qjson.h => qobject-json.h}  |   1 +
 include/qapi/qmp/qstring.h                    |  12 +-
 include/qapi/qmp/types.h                      |   1 -
 include/qjson.h                               |  29 --
 migration/savevm.c                            |  66 ++--
 migration/vmstate.c                           |  63 ++--
 monitor.c                                     |   8 +-
 qapi/Makefile.objs                            |   2 +-
 qapi/json-output-visitor.c                    | 222 ++++++++++++
 qapi/qmp-dispatch.c                           |   1 +
 qapi/qmp-event.c                              |   2 +-
 qemu-img.c                                    |  69 ++--
 qga/main.c                                    |   2 +-
 qjson.c                                       | 129 -------
 qobject/Makefile.objs                         |   3 +-
 qobject/json-parser.c                         |  14 +-
 qobject/{qjson.c => qobject-json.c}           | 147 ++------
 qobject/qobject.c                             |   7 +-
 qobject/qstring.c                             | 117 ++++++-
 target-s390x/kvm.c                            |   2 +-
 tests/.gitignore                              |   3 +-
 tests/Makefile                                |  14 +-
 tests/{check-qjson.c => check-qobject-json.c} |  86 ++++-
 tests/libqtest.c                              |   2 +-
 tests/qemu-iotests/043.out                    |  22 +-
 tests/test-json-output-visitor.c              | 482 ++++++++++++++++++++++++++
 tests/test-qmp-input-strict.c                 |   1 +
 tests/test-qmp-input-visitor.c                |   1 +
 tests/test-qmp-output-visitor.c               |   1 +
 tests/test-visitor-serialization.c            |   1 +
 ui/spice-core.c                               |   2 +-
 vl.c                                          |   2 +-
 46 files changed, 1126 insertions(+), 439 deletions(-)
 create mode 100644 include/qapi/json-output-visitor.h
 rename include/qapi/qmp/{qjson.h => qobject-json.h} (90%)
 delete mode 100644 include/qjson.h
 create mode 100644 qapi/json-output-visitor.c
 delete mode 100644 qjson.c
 rename qobject/{qjson.c => qobject-json.c} (50%)
 rename tests/{check-qjson.c => check-qobject-json.c} (95%)
 create mode 100644 tests/test-json-output-visitor.c
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH v2 1/14 qapi: Rename (one) qjson.h to qobject-json.h

We have two different JSON visitors in the tree; and having both named 'qjson.h' can cause include confusion. Rename the qapi version.

Why did I pick that one? A later patch plans on deleting the top-level qjson.c once we have a native JSON output visitor; we could have renamed that one for less overall churn. On the other hand, all of the QObject subtypes have their own qFOO.c file, but qjson.c makes it sound like we have a QTYPE_JSON subclass of QObject; the new name of qobject-json makes it obvious that the file is used for conversions between QObject and JSON, and not a QObject subtype.

Kill trailing whitespace in the renamed tests/check-qobject-json.c to keep checkpatch.pl happy.

---
v2: retitle, enhance commit message, rebase to master

---
 MAINTAINERS                                   |  2 +-
 balloon.c                                     |  2 +-
 block.c                                       |  2 +-
 block/archipelago.c                           |  2 +-
 block/nbd.c                                   |  2 +-
 block/quorum.c                                |  2 +-
 blockjob.c                                    |  2 +-
 hw/core/qdev.c                                |  2 +-
 hw/misc/pvpanic.c                             |  2 +-
 hw/net/virtio-net.c                           |  2 +-
 include/qapi/qmp/{qjson.h => qobject-json.h}  |  0
 include/qapi/qmp/types.h                      |  2 +-
 monitor.c                                     |  2 +-
 qapi/qmp-event.c                              |  2 +-
 qemu-img.c                                    |  2 +-
 qga/main.c                                    |  2 +-
 qobject/Makefile.objs                         |  3 ++-
 qobject/{qjson.c => qobject-json.c}           |  2 +-
 target-s390x/kvm.c                            |  2 +-
 tests/.gitignore                              |  2 +-
 tests/Makefile                                |  8 ++++----
 tests/{check-qjson.c => check-qobject-json.c} | 14 +++++++-------
 tests/libqtest.c                              |  2 +-
 ui/spice-core.c                               |  2 +-
 vl.c                                          |  2 +-
 25 files changed, 34 insertions(+), 33 deletions(-)
 rename include/qapi/qmp/{qjson.h => qobject-json.h} (100%)
 rename qobject/{qjson.c => qobject-json.c} (99%)
 rename tests/{check-qjson.c => check-qobject-json.c} (99%)
MAINTAINERS +1 -1
index 55a0fd8..81fd039 100644
@@ -1155,7 +1155,7 @@ X: include/qapi/qmp/dispatch.h
 F: tests/check-qdict.c
 F: tests/check-qfloat.c
 F: tests/check-qint.c
-F: tests/check-qjson.c
+F: tests/check-qobject-json.c
 F: tests/check-qlist.c
 F: tests/check-qstring.c
 T: git git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
balloon.c +1 -1
index 0f45d1b..5983b4f 100644
@@ -31,7 +31,7 @@
 #include "trace.h"
 #include "qmp-commands.h"
 #include "qapi/qmp/qerror.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 
 static QEMUBalloonEvent *balloon_event_fn;
 static QEMUBalloonStatus *balloon_stat_fn;
block.c +1 -1
index 411edbf..0b7eb00 100644
@@ -30,7 +30,7 @@
 #include "qemu/module.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qbool.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "sysemu/block-backend.h"
 #include "sysemu/sysemu.h"
 #include "qemu/notify.h"
block/archipelago.c +1 -1
index 855655c..80a1bb5 100644
@@ -56,7 +56,7 @@
 #include "qemu/thread.h"
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu/atomic.h"
 
 #include <inttypes.h>
block/nbd.c +1 -1
index 416f42b..ef53083 100644
@@ -32,7 +32,7 @@
 #include "qemu/module.h"
 #include "qemu/sockets.h"
 #include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qstring.h"
 
block/quorum.c +1 -1
index 6793f12..a64b40d 100644
@@ -18,7 +18,7 @@
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/qmp/qint.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi/qmp/qlist.h"
 #include "qapi/qmp/qstring.h"
 #include "qapi-event.h"
blockjob.c +1 -1
index 80adb9d..84361f7 100644
@@ -31,7 +31,7 @@
 #include "block/block_int.h"
 #include "sysemu/block-backend.h"
 #include "qapi/qmp/qerror.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu/coroutine.h"
 #include "qmp-commands.h"
 #include "qemu/timer.h"
hw/core/qdev.c +1 -1
index 3447f93..149028d 100644
@@ -31,7 +31,7 @@
 #include "qapi/error.h"
 #include "qapi/qmp/qerror.h"
 #include "qapi/visitor.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu/error-report.h"
 #include "hw/hotplug.h"
 #include "hw/boards.h"
hw/misc/pvpanic.c +1 -1
index 3709488..2571483 100644
@@ -13,7 +13,7 @@
  */
 
 #include "qapi/qmp/qobject.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "sysemu/sysemu.h"
 #include "qemu/log.h"
 
hw/net/virtio-net.c +1 -1
index a877614..5988bd1 100644
@@ -21,7 +21,7 @@
 #include "hw/virtio/virtio-net.h"
 #include "net/vhost_net.h"
 #include "hw/virtio/virtio-bus.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi-event.h"
 #include "hw/virtio/virtio-access.h"
 
include/qapi/qmp/qobject-json.h renamed from include/qapi/qmp/qjson.h +0 -0
similarity index 100%
rename from include/qapi/qmp/qjson.h
rename to include/qapi/qmp/qobject-json.h
include/qapi/qmp/types.h +1 -1
index 7782ec5..9109eda 100644
@@ -20,6 +20,6 @@
 #include "qapi/qmp/qstring.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 
 #endif /* QEMU_OBJECTS_H */
monitor.c +1 -1
index e7e7ae2..1dfd359 100644
@@ -55,7 +55,7 @@
 #include "qapi/qmp/qlist.h"
 #include "qapi/qmp/qbool.h"
 #include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi/qmp/json-streamer.h"
 #include "qapi/qmp/json-parser.h"
 #include <qom/object_interfaces.h>
qapi/qmp-event.c +1 -1
index c0e435f..800b1f3 100644
@@ -16,7 +16,7 @@
 #include "qemu-common.h"
 #include "qapi/qmp-event.h"
 #include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 
 static QMPEventFuncEmit qmp_emit;
 
qemu-img.c +1 -1
index e5c7bae..d9a2c74 100644
@@ -24,7 +24,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/qmp/qerror.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu-common.h"
 #include "qemu/option.h"
 #include "qemu/error-report.h"
qga/main.c +1 -1
index f83a97d..3cc0901 100644
@@ -24,7 +24,7 @@
 #include "qapi/qmp/json-streamer.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/qint.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qga/guest-agent-core.h"
 #include "qemu/module.h"
 #include "signal.h"
qobject/Makefile.objs +2 -1
index bed5508..16a48ec 100644
@@ -1,2 +1,3 @@
 util-obj-y = qnull.o qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
-util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
+util-obj-y += qobject-json.o qobject.o
+util-obj-y += json-lexer.o json-streamer.o json-parser.o
qobject/qobject-json.c renamed from qobject/qjson.c +1 -1
similarity index 99%
rename from qobject/qjson.c
rename to qobject/qobject-json.c
index 41d9d65..8fc65a4 100644
@@ -14,7 +14,7 @@
 #include "qapi/qmp/json-lexer.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/json-streamer.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qlist.h"
 #include "qapi/qmp/qbool.h"
target-s390x/kvm.c +1 -1
index 75a0e5d..40dd729 100644
@@ -36,7 +36,7 @@
 #include "hw/hw.h"
 #include "cpu.h"
 #include "sysemu/device_tree.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
 #include "trace.h"
tests/.gitignore +1 -1
index 787c95c..39bcdc0 100644
@@ -1,8 +1,8 @@
 check-qdict
 check-qfloat
 check-qint
-check-qjson
 check-qlist
+check-qobject-json
 check-qstring
 check-qom-interface
 check-qom-proplist
tests/Makefile +4 -4
index 7d2a0d3..1765d16 100644
@@ -16,8 +16,8 @@ check-unit-y += tests/check-qstring$(EXESUF)
 gcov-files-check-qstring-y = qobject/qstring.c
 check-unit-y += tests/check-qlist$(EXESUF)
 gcov-files-check-qlist-y = qobject/qlist.c
-check-unit-y += tests/check-qjson$(EXESUF)
-gcov-files-check-qjson-y = qobject/qjson.c
+check-unit-y += tests/check-qobject-json$(EXESUF)
+gcov-files-check-qobject-json-y = qobject/qobject-json.c
 check-unit-y += tests/test-qmp-output-visitor$(EXESUF)
 gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c
 check-unit-y += tests/test-qmp-input-visitor$(EXESUF)
@@ -368,7 +368,7 @@ GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h \
 	tests/test-qmp-introspect.h
 
 test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
-	tests/check-qlist.o tests/check-qfloat.o tests/check-qjson.o \
+	tests/check-qlist.o tests/check-qfloat.o tests/check-qobject-json.o \
 	tests/test-coroutine.o tests/test-string-output-visitor.o \
 	tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \
 	tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
@@ -396,7 +396,7 @@ tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
 tests/check-qfloat$(EXESUF): tests/check-qfloat.o $(test-util-obj-y)
-tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y)
+tests/check-qobject-json$(EXESUF): tests/check-qobject-json.o $(test-util-obj-y)
 tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y)
 tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y)
 tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y)
tests/check-qobject-json.c renamed from tests/check-qjson.c +7 -7
similarity index 99%
rename from tests/check-qjson.c
rename to tests/check-qobject-json.c
index 61e9bfb..9c4e53a 100644
@@ -18,7 +18,7 @@
 #include "qapi/qmp/qlist.h"
 #include "qapi/qmp/qfloat.h"
 #include "qapi/qmp/qbool.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 
 #include "qemu-common.h"
 
@@ -63,7 +63,7 @@ static void escaped_string(void)
 
         g_assert(obj != NULL);
         g_assert(qobject_type(obj) == QTYPE_QSTRING);
-        
+
         str = qobject_to_qstring(obj);
         g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
 
@@ -98,7 +98,7 @@ static void simple_string(void)
 
         g_assert(obj != NULL);
         g_assert(qobject_type(obj) == QTYPE_QSTRING);
-        
+
         str = qobject_to_qstring(obj);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
 
@@ -106,7 +106,7 @@ static void simple_string(void)
         g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
 
         qobject_decref(obj);
-        
+
         QDECREF(str);
     }
 }
@@ -132,7 +132,7 @@ static void single_quote_string(void)
 
         g_assert(obj != NULL);
         g_assert(qobject_type(obj) == QTYPE_QSTRING);
-        
+
         str = qobject_to_qstring(obj);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
 
@@ -880,7 +880,7 @@ static void vararg_string(void)
 
         g_assert(obj != NULL);
         g_assert(qobject_type(obj) == QTYPE_QSTRING);
-        
+
         str = qobject_to_qstring(obj);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
 
@@ -1144,7 +1144,7 @@ static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
         helper.index = 0;
         helper.objs = lhs->value.qlist;
         helper.result = 1;
-        
+
         qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
 
         return helper.result;
tests/libqtest.c +1 -1
index fa314e1..149eb4c 100644
@@ -32,7 +32,7 @@
 #include "qemu/osdep.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/json-streamer.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 
 #define MAX_IRQ 256
 #define SOCKET_TIMEOUT 5
ui/spice-core.c +1 -1
index 6a62d71..ceb18ae 100644
@@ -32,7 +32,7 @@
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qbool.h"
 #include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu/notify.h"
 #include "migration/migration.h"
 #include "hw/hw.h"
vl.c +1 -1
index 200f3d5..29737f7 100644
@@ -92,7 +92,7 @@ int main(int argc, char **argv)
 #include "audio/audio.h"
 #include "migration/migration.h"
 #include "sysemu/kvm.h"
-#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qemu/option.h"
 #include "qemu/config-file.h"
 #include "qemu-options.h"
Signature · 1 line
2.4.3
author
#2 lore raw

PATCH v2 2/14 qapi: Improve use of qmp/types.h

In reply to Eric Blake

'qobject-json.h' is not a QObject subtype; include this file directly in .c files that are using it, rather than abusing qmp/types.h for that purpose.

Meanwhile, for files that include a list of individual QObject subtypes, it's easier to just use qmp/types.h for that purpose.

---
v2: no change

---
 hw/pci/pcie_aer.c                  | 1 +
 include/qapi/qmp/types.h           | 1 -
 monitor.c                          | 6 +-----
 qapi/qmp-dispatch.c                | 1 +
 qobject/json-parser.c              | 7 +------
 qobject/qobject-json.c             | 6 +-----
 qobject/qobject.c                  | 7 +------
 tests/check-qobject-json.c         | 7 +------
 tests/test-qmp-input-strict.c      | 1 +
 tests/test-qmp-input-visitor.c     | 1 +
 tests/test-qmp-output-visitor.c    | 1 +
 tests/test-visitor-serialization.c | 1 +
 12 files changed, 11 insertions(+), 29 deletions(-)
hw/pci/pcie_aer.c +1 -0
index 98d2c18..dd5588c 100644
@@ -20,6 +20,7 @@
 
 #include "sysemu/sysemu.h"
 #include "qapi/qmp/types.h"
+#include "qapi/qmp/qobject-json.h"
 #include "monitor/monitor.h"
 #include "hw/pci/pci_bridge.h"
 #include "hw/pci/pcie.h"
include/qapi/qmp/types.h +0 -1
index 9109eda..f21ecf4 100644
@@ -20,6 +20,5 @@
 #include "qapi/qmp/qstring.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qobject-json.h"
 
 #endif /* QEMU_OBJECTS_H */
monitor.c +1 -5
index 1dfd359..ed6a548 100644
@@ -50,12 +50,8 @@
 #include "qemu/acl.h"
 #include "sysemu/tpm.h"
 #include "qapi/qmp/qerror.h"
-#include "qapi/qmp/qint.h"
-#include "qapi/qmp/qfloat.h"
-#include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qbool.h"
-#include "qapi/qmp/qstring.h"
 #include "qapi/qmp/qobject-json.h"
+#include "qapi/qmp/types.h"
 #include "qapi/qmp/json-streamer.h"
 #include "qapi/qmp/json-parser.h"
 #include <qom/object_interfaces.h>
qapi/qmp-dispatch.c +1 -0
index f36933d..4b79bf4 100644
@@ -14,6 +14,7 @@
 #include "qapi/qmp/types.h"
 #include "qapi/qmp/dispatch.h"
 #include "qapi/qmp/json-parser.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi-types.h"
 #include "qapi/error.h"
 #include "qapi/qmp/qerror.h"
qobject/json-parser.c +1 -6
index 6ab98a7..441c6e9 100644
@@ -14,12 +14,7 @@
 #include <stdarg.h>
 
 #include "qemu-common.h"
-#include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qint.h"
-#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qfloat.h"
-#include "qapi/qmp/qbool.h"
+#include "qapi/qmp/types.h"
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/json-lexer.h"
 #include "qapi/qmp/json-streamer.h"
qobject/qobject-json.c +1 -5
index 8fc65a4..cc96ff6 100644
@@ -15,11 +15,7 @@
 #include "qapi/qmp/json-parser.h"
 #include "qapi/qmp/json-streamer.h"
 #include "qapi/qmp/qobject-json.h"
-#include "qapi/qmp/qint.h"
-#include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qbool.h"
-#include "qapi/qmp/qfloat.h"
-#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/types.h"
 
 typedef struct JSONParsingState
 {
qobject/qobject.c +1 -6
index a3ef14e..0c468d8 100644
@@ -8,12 +8,7 @@
  */
 
 #include "qemu-common.h"
-#include "qapi/qmp/qbool.h"
-#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qfloat.h"
-#include "qapi/qmp/qint.h"
-#include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qstring.h"
+#include "qapi/qmp/types.h"
 
 static void (*qdestroy[QTYPE__MAX])(QObject *) = {
     [QTYPE_NONE] = NULL,               /* No such object exists */
tests/check-qobject-json.c +1 -6
index 9c4e53a..46d4edf 100644
@@ -12,12 +12,7 @@
  */
 #include <glib.h>
 
-#include "qapi/qmp/qstring.h"
-#include "qapi/qmp/qint.h"
-#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qlist.h"
-#include "qapi/qmp/qfloat.h"
-#include "qapi/qmp/qbool.h"
+#include "qapi/qmp/types.h"
 #include "qapi/qmp/qobject-json.h"
 
 #include "qemu-common.h"
tests/test-qmp-input-strict.c +1 -0
index 4db35dd..b929ec7 100644
@@ -19,6 +19,7 @@
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
 #include "qapi/qmp/types.h"
+#include "qapi/qmp/qobject-json.h"
 #include "test-qmp-introspect.h"
 #include "qmp-introspect.h"
 #include "qapi-visit.h"
tests/test-qmp-input-visitor.c +1 -0
index 7f9191c..5e30fd8 100644
@@ -18,6 +18,7 @@
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
 #include "qapi/qmp/types.h"
+#include "qapi/qmp/qobject-json.h"
 
 typedef struct TestInputVisitorData {
     QObject *obj;
tests/test-qmp-output-visitor.c +1 -0
index 74d0ac4..f1171e6 100644
@@ -17,6 +17,7 @@
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
 #include "qapi/qmp/types.h"
+#include "qapi/qmp/qobject-json.h"
 
 typedef struct TestOutputVisitorData {
     QmpOutputVisitor *qov;
tests/test-visitor-serialization.c +1 -0
index f74a6df..381e188 100644
@@ -20,6 +20,7 @@
 #include "test-qapi-types.h"
 #include "test-qapi-visit.h"
 #include "qapi/qmp/types.h"
+#include "qapi/qmp/qobject-json.h"
 #include "qapi/qmp-input-visitor.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/string-input-visitor.h"
Signature · 1 line
2.4.3
Reviewed-by on v2 2/14
#3 lore raw

Re: [Qemu-devel] [PATCH v2 02/14] qapi: Improve use of qmp/types.h

Quoted 8 lines · Eric Blake

'qobject-json.h' is not a QObject subtype; include this file directly in .c files that are using it, rather than abusing qmp/types.h for that purpose.

Meanwhile, for files that include a list of individual QObject subtypes, it's easier to just use qmp/types.h for that purpose.

author
#4 lore raw

PATCH v2 3/14 qapi: Factor out JSON string escaping

In reply to Eric Blake

Pull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules.

While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry.

---
v2: no change

---
 include/qapi/qmp/qstring.h |  1 +
 qjson.c                    |  9 +++----
 qobject/qobject-json.c     | 58 ++-------------------------------------------
 qobject/qstring.c          | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 62 deletions(-)
include/qapi/qmp/qstring.h +1 -0
index df7df55..1a938f6 100644
@@ -31,6 +31,7 @@ const char *qstring_get_str(const QString *qstring);
 void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
+void qstring_append_json_string(QString *qstring, const char *raw);
 QString *qobject_to_qstring(const QObject *obj);
 void qstring_destroy_obj(QObject *obj);
 
qjson.c +3 -6
index e478802..25afaff 100644
@@ -36,9 +36,8 @@ static void json_emit_element(QJSON *json, const char *name)
     }
 
     if (name) {
-        qstring_append(json->str, "\"");
-        qstring_append(json->str, name);
-        qstring_append(json->str, "\" : ");
+        qstring_append_json_string(json->str, name);
+        qstring_append(json->str, " : ");
     }
 }
 
@@ -77,9 +76,7 @@ void json_prop_int(QJSON *json, const char *name, int64_t val)
 void json_prop_str(QJSON *json, const char *name, const char *str)
 {
     json_emit_element(json, name);
-    qstring_append_chr(json->str, '"');
-    qstring_append(json->str, str);
-    qstring_append_chr(json->str, '"');
+    qstring_append_json_string(json->str, str);
 }
 
 const char *qjson_get_str(QJSON *json)
qobject/qobject-json.c +2 -56

76 lines not on this page · raw message

qobject/qstring.c +59 -0

65 lines not on this page · raw message

Signature · 1 line
2.4.3
Reviewed-by on v2 3/14
#5 lore raw

Re: [Qemu-devel] [PATCH v2 03/14] qapi: Factor out JSON string escaping

Quoted 8 lines · Eric Blake

Pull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules.

While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry.

author
#6 lore raw

PATCH v2 4/14 qapi: Factor out JSON number formatting

In reply to Eric Blake

Pull out a new qstring_append_json_number() helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle).

Address one FIXME by adding an Error parameter and warning the caller if the requested number cannot be represented in JSON; but add another FIXME in its place because we have no way to report the problem higher up the stack.

---
v2: minor wording tweaks

---
 include/qapi/qmp/qstring.h |  4 +++-
 qobject/qobject-json.c     | 24 +++---------------------
 qobject/qstring.c          | 37 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 42 insertions(+), 23 deletions(-)
include/qapi/qmp/qstring.h +3 -1
index 1a938f6..cab1faf 100644
@@ -1,7 +1,7 @@
 /*
  * QString Module
  *
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2015 Red Hat Inc.
  *
  * Authors:
  *  Luiz Capitulino <[email protected]>
@@ -15,6 +15,7 @@
 
 #include <stdint.h>
 #include "qapi/qmp/qobject.h"
+#include "qapi/error.h"
 
 typedef struct QString {
     QObject base;
@@ -32,6 +33,7 @@ void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
 void qstring_append_json_string(QString *qstring, const char *raw);
+void qstring_append_json_number(QString *qstring, double number, Error **errp);
 QString *qobject_to_qstring(const QObject *obj);
 void qstring_destroy_obj(QObject *obj);
 
qobject/qobject-json.c +3 -21
index 1557ec6..dbe0ab8 100644
@@ -176,27 +176,9 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
     }
     case QTYPE_QFLOAT: {
         QFloat *val = qobject_to_qfloat(obj);
-        char buffer[1024];
-        int len;
-
-        /* FIXME: snprintf() is locale dependent; but JSON requires
-         * numbers to be formatted as if in the C locale. */
-        /* FIXME: This risks printing Inf or NaN, which are not valid
-         * JSON values. */
-        /* FIXME: the default precision of %f may be insufficient to
-         * tell this number apart from others. */
-        len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
-        while (len > 0 && buffer[len - 1] == '0') {
-            len--;
-        }
-
-        if (len && buffer[len - 1] == '.') {
-            buffer[len - 1] = 0;
-        } else {
-            buffer[len] = 0;
-        }
-
-        qstring_append(str, buffer);
+        /* FIXME: no way to report invalid JSON to caller, so for now
+         * we just ignore it */
+        qstring_append_json_number(str, qfloat_get_double(val), NULL);
         break;
     }
     case QTYPE_QBOOL: {
qobject/qstring.c +36 -1
index 2882c47..b06f290 100644
@@ -1,7 +1,7 @@
 /*
  * QString Module
  *
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2015 Red Hat Inc.
  *
  * Authors:
  *  Luiz Capitulino <[email protected]>
@@ -13,6 +13,7 @@
 #include "qapi/qmp/qobject.h"
 #include "qapi/qmp/qstring.h"
 #include "qemu-common.h"
+#include <math.h>
 
 /**
  * qstring_new(): Create a new empty QString
@@ -165,6 +166,40 @@ void qstring_append_json_string(QString *qstring, const char *raw)
 }
 
 /**
+ * qstring_append_json_number(): Append a JSON number to a QString.
+ * Set @errp if the number is not representable in JSON, but append the
+ * output anyway (callers can then choose to ignore the warning).
+ */
+void qstring_append_json_number(QString *qstring, double number, Error **errp)
+{
+    char buffer[1024];
+    int len;
+
+    /* JSON does not allow Inf or NaN; append it but set errp */
+    if (!isfinite(number)) {
+        error_setg(errp, "Non-finite number %f is not valid JSON", number);
+    }
+
+    /* FIXME: snprintf() is locale dependent; but JSON requires
+     * numbers to be formatted as if in the C locale. */
+    /* FIXME: the default precision of %f may be insufficient to
+     * tell this number apart from others. */
+    len = snprintf(buffer, sizeof(buffer), "%f", number);
+    assert(len > 0 && len < sizeof(buffer));
+    while (len > 0 && buffer[len - 1] == '0') {
+        len--;
+    }
+
+    if (len && buffer[len - 1] == '.') {
+        buffer[len - 1] = 0;
+    } else {
+        buffer[len] = 0;
+    }
+
+    qstring_append(qstring, buffer);
+}
+
+/**
  * qobject_to_qstring(): Convert a QObject to a QString
  */
 QString *qobject_to_qstring(const QObject *obj)
Signature · 1 line
2.4.3
Reviewed-by on v2 4/14
#7 lore raw

Re: [Qemu-devel] [PATCH v2 04/14] qapi: Factor out JSON number formatting

Quoted 11 lines · Eric Blake

Pull out a new qstring_append_json_number() helper, so that all JSON output producers can use a consistent style for printing floating point without duplicating code (since we are doing more data massaging than a simple printf format can handle).

Address one FIXME by adding an Error parameter and warning the caller if the requested number cannot be represented in JSON; but add another FIXME in its place because we have no way to report the problem higher up the stack.

author
#8 lore raw

PATCH v2 5/14 qapi: Use qstring_append_chr() where appropriate

In reply to Eric Blake

No need to create a temporary buffer, when we already have a function available for our needs.

---
 qobject/json-parser.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
qobject/json-parser.c +1 -6
index 441c6e9..fc5510e 100644
@@ -204,12 +204,7 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
                 goto out;
             }
         } else {
-            char dummy[2];
-
-            dummy[0] = *ptr++;
-            dummy[1] = 0;
-
-            qstring_append(str, dummy);
+            qstring_append_chr(str, *ptr++);
         }
     }
 
Signature · 1 line
2.4.3
author
#10 lore raw

PATCH v2 6/14 qapi: Add qstring_append_format()

In reply to Eric Blake

Back in commit 764c1ca (Nov 2009), we added qstring_append_int(). However, it did not see any use until commit 190c882 (Jan 2015). Furthermore, it has a rather limited use case - to print anything else, callers still have to format into a temporary buffer, unless we want to introduce an explosion of new qstring_append_* methods for each useful type to print.

A much better approach is to add a wrapper that merges printf behavior onto qstring_append, via the new qstring_append_format() (and its vararg counterpart). In fact, with that in place, we no longer need qstring_append_int().

Other immediate uses for the new function include simplifying two existing clients of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation.

---
v2: also simplify qstring_append_json_string(), add assertion that format is well-formed

---
 include/qapi/qmp/qstring.h |  7 ++++++-
 qjson.c                    |  2 +-
 qobject/qobject-json.c     | 25 +++++--------------------
 qobject/qstring.c          | 37 +++++++++++++++++++++++++------------
 4 files changed, 37 insertions(+), 34 deletions(-)
include/qapi/qmp/qstring.h +6 -1
index cab1faf..1b1e130 100644
@@ -14,6 +14,8 @@
 #define QSTRING_H
 
 #include <stdint.h>
+#include <stdarg.h>
+#include "qemu/compiler.h"
 #include "qapi/qmp/qobject.h"
 #include "qapi/error.h"
 
@@ -29,9 +31,12 @@ QString *qstring_from_str(const char *str);
 QString *qstring_from_substr(const char *str, int start, int end);
 size_t qstring_get_length(const QString *qstring);
 const char *qstring_get_str(const QString *qstring);
-void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
+void qstring_append_format(QString *qstring, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
+void qstring_append_vformat(QString *qstring, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 void qstring_append_json_string(QString *qstring, const char *raw);
 void qstring_append_json_number(QString *qstring, double number, Error **errp);
 QString *qobject_to_qstring(const QObject *obj);
qjson.c +1 -1
index 25afaff..8c93c1b 100644
@@ -70,7 +70,7 @@ void json_end_array(QJSON *json)
 void json_prop_int(QJSON *json, const char *name, int64_t val)
 {
     json_emit_element(json, name);
-    qstring_append_int(json->str, val);
+    qstring_append_format(json->str, "%" PRId64, val);
 }
 
 void json_prop_str(QJSON *json, const char *name, const char *str)
qobject/qobject-json.c +5 -20
index dbe0ab8..3a7b26b 100644
@@ -79,16 +79,13 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent);
 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 {
     ToJsonIterState *s = opaque;
-    int j;
 
     if (s->count) {
         qstring_append(s->str, s->pretty ? "," : ", ");
     }
 
     if (s->pretty) {
-        qstring_append(s->str, "\n");
-        for (j = 0 ; j < s->indent ; j++)
-            qstring_append(s->str, "    ");
+        qstring_append_format(s->str, "\n%*s", 4 * s->indent, "");
     }
 
     qstring_append_json_string(s->str, key);
@@ -101,16 +98,13 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 static void to_json_list_iter(QObject *obj, void *opaque)
 {
     ToJsonIterState *s = opaque;
-    int j;
 
     if (s->count) {
         qstring_append(s->str, s->pretty ? "," : ", ");
     }
 
     if (s->pretty) {
-        qstring_append(s->str, "\n");
-        for (j = 0 ; j < s->indent ; j++)
-            qstring_append(s->str, "    ");
+        qstring_append_format(s->str, "\n%*s", 4 * s->indent, "");
     }
 
     to_json(obj, s->str, s->pretty, s->indent);
@@ -125,10 +119,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         break;
     case QTYPE_QINT: {
         QInt *val = qobject_to_qint(obj);
-        char buffer[1024];
-
-        snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
-        qstring_append(str, buffer);
+        qstring_append_format(str, "%" PRId64, qint_get_int(val));
         break;
     }
     case QTYPE_QSTRING: {
@@ -147,10 +138,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         qstring_append(str, "{");
         qdict_iter(val, to_json_dict_iter, &s);
         if (pretty) {
-            int j;
-            qstring_append(str, "\n");
-            for (j = 0 ; j < indent ; j++)
-                qstring_append(str, "    ");
+            qstring_append_format(str, "\n%*s", 4 * indent, "");
         }
         qstring_append(str, "}");
         break;
@@ -166,10 +154,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         qstring_append(str, "[");
         qlist_iter(val, (void *)to_json_list_iter, &s);
         if (pretty) {
-            int j;
-            qstring_append(str, "\n");
-            for (j = 0 ; j < indent ; j++)
-                qstring_append(str, "    ");
+            qstring_append_format(str, "\n%*s", 4 * indent, "");
         }
         qstring_append(str, "]");
         break;
qobject/qstring.c +25 -12
index b06f290..3c97302 100644
@@ -88,12 +88,28 @@ void qstring_append(QString *qstring, const char *str)
     qstring->string[qstring->length] = 0;
 }
 
-void qstring_append_int(QString *qstring, int64_t value)
+void qstring_append_format(QString *qstring, const char *fmt, ...)
 {
-    char num[32];
+    va_list ap;
 
-    snprintf(num, sizeof(num), "%" PRId64, value);
-    qstring_append(qstring, num);
+    va_start(ap, fmt);
+    qstring_append_vformat(qstring, fmt, ap);
+    va_end(ap);
+}
+
+void qstring_append_vformat(QString *qstring, const char *fmt, va_list ap)
+{
+    va_list ap2;
+    int len;
+
+    va_copy(ap2, ap);
+    len = vsnprintf(qstring->string + qstring->length, 0, fmt, ap2);
+    assert(len >= 0);
+    va_end(ap2);
+
+    capacity_increase(qstring, len);
+    vsnprintf(qstring->string + qstring->length, len + 1, fmt, ap);
+    qstring->length += len;
 }
 
 /**
@@ -114,7 +130,6 @@ void qstring_append_json_string(QString *qstring, const char *raw)
 {
     const char *ptr = raw;
     int cp;
-    char buf[16];
     char *end;
 
     qstring_append(qstring, "\"");
@@ -149,16 +164,14 @@ void qstring_append_json_string(QString *qstring, const char *raw)
             }
             if (cp > 0xFFFF) {
                 /* beyond BMP; need a surrogate pair */
-                snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
-                         0xD800 + ((cp - 0x10000) >> 10),
-                         0xDC00 + ((cp - 0x10000) & 0x3FF));
+                qstring_append_format(qstring, "\\u%04X\\u%04X",
+                                      0xD800 + ((cp - 0x10000) >> 10),
+                                      0xDC00 + ((cp - 0x10000) & 0x3FF));
             } else if (cp < 0x20 || cp >= 0x7F) {
-                snprintf(buf, sizeof(buf), "\\u%04X", cp);
+                qstring_append_format(qstring, "\\u%04X", cp);
             } else {
-                buf[0] = cp;
-                buf[1] = 0;
+                qstring_append_chr(qstring, cp);
             }
-            qstring_append(qstring, buf);
         }
     };
 
Signature · 1 line
2.4.3
Reviewed-by on v2 6/14
#11 lore raw

Re: [Qemu-devel] [PATCH v2 06/14] qapi: Add qstring_append_format()

Quoted 12 lines · Eric Blake

Back in commit 764c1ca (Nov 2009), we added qstring_append_int(). However, it did not see any use until commit 190c882 (Jan 2015). Furthermore, it has a rather limited use case - to print anything else, callers still have to format into a temporary buffer, unless we want to introduce an explosion of new qstring_append_* methods for each useful type to print.

A much better approach is to add a wrapper that merges printf behavior onto qstring_append, via the new qstring_append_format() (and its vararg counterpart). In fact, with that in place, we no longer need qstring_append_int().

Quoted 6 lines

Other immediate uses for the new function include simplifying two existing clients of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation.

author
#12 lore raw

PATCH v2 7/14 qapi: add json output visitor

In reply to Eric Blake

We have several places that want to go from qapi to JSON; right now, they have to create an intermediate QObject to do the work. That also has the drawback that the JSON formatting of a QDict will rearrange keys (according to a deterministic, but unpredictable, hash), when humans have an easier time if dicts are produced in the same order as the qapi type.

For these reasons, it is time to add a new JSON output visitor. This patch just adds the basic visitor and tests that it works; later patches will add pretty-printing, and convert clients to use the visitor.

Design choices: Unlike the QMP output visitor, the JSON visitor refuses to visit a required string with a NULL value (abort), as well as a non-finite number (raises an error message). Reusing QString to grow the contents means that we easily share code with both qobject-json.c and qjson.c; although it might be nice to enhance things to take an optional output callback function so that the output can truly be streamed instead of collected in memory.

---
v2: rebase to qapi subset E v8; add test of error outputting infinity; use unsigned depth

---
 include/qapi/json-output-visitor.h |  25 +++
 qapi/Makefile.objs                 |   2 +-
 qapi/json-output-visitor.c         | 203 ++++++++++++++++++
 tests/.gitignore                   |   1 +
 tests/Makefile                     |   4 +
 tests/test-json-output-visitor.c   | 418 +++++++++++++++++++++++++++++++++++++
 6 files changed, 652 insertions(+), 1 deletion(-)
 create mode 100644 include/qapi/json-output-visitor.h
 create mode 100644 qapi/json-output-visitor.c
 create mode 100644 tests/test-json-output-visitor.c
include/qapi/json-output-visitor.h new file +25 -0
new file mode 100644
index 0000000..5be5a13
@@ -0,0 +1,25 @@
+/*
+ * JSON Output Visitor
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef JSON_OUTPUT_VISITOR_H
+#define JSON_OUTPUT_VISITOR_H
+
+#include "qapi/visitor.h"
+
+typedef struct JsonOutputVisitor JsonOutputVisitor;
+
+JsonOutputVisitor *json_output_visitor_new(void);
+void json_output_visitor_cleanup(JsonOutputVisitor *v);
+void json_output_visitor_reset(JsonOutputVisitor *v);
+
+char *json_output_get_string(JsonOutputVisitor *v);
+Visitor *json_output_get_visitor(JsonOutputVisitor *v);
+
+#endif
qapi/Makefile.objs +1 -1
index 2278970..b60e11b 100644
@@ -1,6 +1,6 @@
 util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
-util-obj-y += opts-visitor.o
+util-obj-y += opts-visitor.o json-output-visitor.o
 util-obj-y += qmp-event.o
 util-obj-y += qapi-util.o
qapi/json-output-visitor.c new file +203 -0

203 lines not on this page · raw message

tests/.gitignore +1 -0
index 39bcdc0..7a791ed 100644
@@ -34,6 +34,7 @@ test-io-channel-file.txt
 test-io-channel-socket
 test-io-channel-tls
 test-io-task
+test-json-output-visitor
 test-mul64
 test-opts-visitor
 test-qapi-event.[ch]
tests/Makefile +4 -0
index 1765d16..19a9ee0 100644
@@ -20,6 +20,8 @@ check-unit-y += tests/check-qobject-json$(EXESUF)
 gcov-files-check-qobject-json-y = qobject/qobject-json.c
 check-unit-y += tests/test-qmp-output-visitor$(EXESUF)
 gcov-files-test-qmp-output-visitor-y = qapi/qmp-output-visitor.c
+check-unit-y += tests/test-json-output-visitor$(EXESUF)
+gcov-files-test-json-output-visitor-y = qapi/json-output-visitor.c
 check-unit-y += tests/test-qmp-input-visitor$(EXESUF)
 gcov-files-test-qmp-input-visitor-y = qapi/qmp-input-visitor.c
 check-unit-y += tests/test-qmp-input-strict$(EXESUF)
@@ -371,6 +373,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/check-qlist.o tests/check-qfloat.o tests/check-qobject-json.o \
 	tests/test-coroutine.o tests/test-string-output-visitor.o \
 	tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \
+	tests/test-json-output-visitor.o \
 	tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
 	tests/test-qmp-commands.o tests/test-visitor-serialization.o \
 	tests/test-x86-cpuid.o tests/test-mul64.o tests/test-int128.o \
@@ -458,6 +461,7 @@ tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(
 tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y)
 tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y)
 tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-qapi-obj-y)
+tests/test-json-output-visitor$(EXESUF): tests/test-json-output-visitor.o $(test-qapi-obj-y)
 tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y)
 tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y)
 tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y)
tests/test-json-output-visitor.c new file +418 -0

418 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#13 lore raw

PATCH v2 8/14 qjson: Simplify by using json-output-visitor

In reply to Eric Blake

Instead of rolling our own limited JSON outputter, we can just wrap the more full-featured JSON output Visitor.

This slightly changes the output (different spacing), but the result is still equivalent JSON contents.

---
v2: rebase to earlier changes

This is alternative 1. See also patches 10-11 for alternative 2.

---
 qjson.c | 61 ++++++++++++++++++++++---------------------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)
qjson.c +22 -39
index 8c93c1b..8874bad 100644
@@ -11,103 +11,86 @@
  *
  */
 
-#include <qapi/qmp/qstring.h>
 #include <stdbool.h>
-#include <glib.h>
+#include <stdint.h>
 #include <qjson.h>
 #include <qemu/module.h>
 #include <qom/object.h>
+#include "qapi/json-output-visitor.h"
 
 struct QJSON {
     Object obj;
-    QString *str;
-    bool omit_comma;
+    JsonOutputVisitor *jov;
+    char *str;
 };
 
 #define QJSON(obj) OBJECT_CHECK(QJSON, (obj), TYPE_QJSON)
 
-static void json_emit_element(QJSON *json, const char *name)
-{
-    /* Check whether we need to print a , before an element */
-    if (json->omit_comma) {
-        json->omit_comma = false;
-    } else {
-        qstring_append(json->str, ", ");
-    }
-
-    if (name) {
-        qstring_append_json_string(json->str, name);
-        qstring_append(json->str, " : ");
-    }
-}
-
 void json_start_object(QJSON *json, const char *name)
 {
-    json_emit_element(json, name);
-    qstring_append(json->str, "{ ");
-    json->omit_comma = true;
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_start_struct(v, name, NULL, 0, &error_abort);
 }
 
 void json_end_object(QJSON *json)
 {
-    qstring_append(json->str, " }");
-    json->omit_comma = false;
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_check_struct(v, &error_abort);
+    visit_end_struct(v);
 }
 
 void json_start_array(QJSON *json, const char *name)
 {
-    json_emit_element(json, name);
-    qstring_append(json->str, "[ ");
-    json->omit_comma = true;
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_start_list(v, name, NULL, 0, &error_abort);
 }
 
 void json_end_array(QJSON *json)
 {
-    qstring_append(json->str, " ]");
-    json->omit_comma = false;
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_end_list(v);
 }
 
 void json_prop_int(QJSON *json, const char *name, int64_t val)
 {
-    json_emit_element(json, name);
-    qstring_append_format(json->str, "%" PRId64, val);
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_type_int(v, name, &val, &error_abort);
 }
 
 void json_prop_str(QJSON *json, const char *name, const char *str)
 {
-    json_emit_element(json, name);
-    qstring_append_json_string(json->str, str);
+    Visitor *v = json_output_get_visitor(json->jov);
+    visit_type_str(v, name, (char **)&str, &error_abort);
 }
 
 const char *qjson_get_str(QJSON *json)
 {
-    return qstring_get_str(json->str);
+    return json->str;
 }
 
 QJSON *qjson_new(void)
 {
     QJSON *json = QJSON(object_new(TYPE_QJSON));
+    json_start_object(json, NULL);
     return json;
 }
 
 void qjson_finish(QJSON *json)
 {
     json_end_object(json);
+    json->str = json_output_get_string(json->jov);
 }
 
 static void qjson_initfn(Object *obj)
 {
     QJSON *json = QJSON(obj);
-
-    json->str = qstring_from_str("{ ");
-    json->omit_comma = true;
+    json->jov = json_output_visitor_new();
 }
 
 static void qjson_finalizefn(Object *obj)
 {
     QJSON *json = QJSON(obj);
-
-    qobject_decref(QOBJECT(json->str));
+    g_free(json->str);
 }
 
 static const TypeInfo qjson_type_info = {
Signature · 1 line
2.4.3
author
#14 lore raw

PATCH v2 9/14 Revert "qjson: Simplify by using json-output-visitor"

In reply to Eric Blake

This reverts commit 5859ad241516eed8cb9ba60889efa0ed47648b38.

The revert is here only to show the difference between two alternatives, the final series will have just one choice of patch 8, or of patches 10-11

---
 qjson.c | 61 +++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 39 insertions(+), 22 deletions(-)
qjson.c +39 -22
index 8874bad..8c93c1b 100644
@@ -11,86 +11,103 @@
  *
  */
 
+#include <qapi/qmp/qstring.h>
 #include <stdbool.h>
-#include <stdint.h>
+#include <glib.h>
 #include <qjson.h>
 #include <qemu/module.h>
 #include <qom/object.h>
-#include "qapi/json-output-visitor.h"
 
 struct QJSON {
     Object obj;
-    JsonOutputVisitor *jov;
-    char *str;
+    QString *str;
+    bool omit_comma;
 };
 
 #define QJSON(obj) OBJECT_CHECK(QJSON, (obj), TYPE_QJSON)
 
+static void json_emit_element(QJSON *json, const char *name)
+{
+    /* Check whether we need to print a , before an element */
+    if (json->omit_comma) {
+        json->omit_comma = false;
+    } else {
+        qstring_append(json->str, ", ");
+    }
+
+    if (name) {
+        qstring_append_json_string(json->str, name);
+        qstring_append(json->str, " : ");
+    }
+}
+
 void json_start_object(QJSON *json, const char *name)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_start_struct(v, name, NULL, 0, &error_abort);
+    json_emit_element(json, name);
+    qstring_append(json->str, "{ ");
+    json->omit_comma = true;
 }
 
 void json_end_object(QJSON *json)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_check_struct(v, &error_abort);
-    visit_end_struct(v);
+    qstring_append(json->str, " }");
+    json->omit_comma = false;
 }
 
 void json_start_array(QJSON *json, const char *name)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_start_list(v, name, NULL, 0, &error_abort);
+    json_emit_element(json, name);
+    qstring_append(json->str, "[ ");
+    json->omit_comma = true;
 }
 
 void json_end_array(QJSON *json)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_end_list(v);
+    qstring_append(json->str, " ]");
+    json->omit_comma = false;
 }
 
 void json_prop_int(QJSON *json, const char *name, int64_t val)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_type_int(v, name, &val, &error_abort);
+    json_emit_element(json, name);
+    qstring_append_format(json->str, "%" PRId64, val);
 }
 
 void json_prop_str(QJSON *json, const char *name, const char *str)
 {
-    Visitor *v = json_output_get_visitor(json->jov);
-    visit_type_str(v, name, (char **)&str, &error_abort);
+    json_emit_element(json, name);
+    qstring_append_json_string(json->str, str);
 }
 
 const char *qjson_get_str(QJSON *json)
 {
-    return json->str;
+    return qstring_get_str(json->str);
 }
 
 QJSON *qjson_new(void)
 {
     QJSON *json = QJSON(object_new(TYPE_QJSON));
-    json_start_object(json, NULL);
     return json;
 }
 
 void qjson_finish(QJSON *json)
 {
     json_end_object(json);
-    json->str = json_output_get_string(json->jov);
 }
 
 static void qjson_initfn(Object *obj)
 {
     QJSON *json = QJSON(obj);
-    json->jov = json_output_visitor_new();
+
+    json->str = qstring_from_str("{ ");
+    json->omit_comma = true;
 }
 
 static void qjson_finalizefn(Object *obj)
 {
     QJSON *json = QJSON(obj);
-    g_free(json->str);
+
+    qobject_decref(QOBJECT(json->str));
 }
 
 static const TypeInfo qjson_type_info = {
Signature · 1 line
2.4.3
author
#15 lore raw

PATCH v2 10/14 vmstate: use new JSON output visitor

In reply to Eric Blake

Rather than using a QJSON object and converting the QString result to a char *, we can use the new JSON output visitor and get directly to a char *.

The conversions are a bit tricky in place (in places, we have to copy an integer to an int64_t temporary to get the right pointer for visit_type_int(); and for several strings, we have to copy to a temporary variable so we can take an address (&char[] is not the same as &char*) and cast away const), but overall still fairly mechanical.

---
v2: new patch

This is part of alternative 2, along with patch 11. See also patch 8 for alternative 1.

---
 include/migration/vmstate.h |  4 +--
 migration/savevm.c          | 66 ++++++++++++++++++++++++++++-----------------
 migration/vmstate.c         | 63 ++++++++++++++++++++++++++-----------------
 tests/Makefile              |  2 +-
 4 files changed, 84 insertions(+), 51 deletions(-)
include/migration/vmstate.h +2 -2
index 7267e38..101742e 100644
@@ -29,7 +29,7 @@
 #ifndef CONFIG_USER_ONLY
 #include <migration/qemu-file.h>
 #endif
-#include <qjson.h>
+#include "qapi/json-output-visitor.h"
 
 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
@@ -814,7 +814,7 @@ void loadvm_free_handlers(MigrationIncomingState *mis);
 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                        void *opaque, int version_id);
 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
-                        void *opaque, QJSON *vmdesc);
+                        void *opaque, Visitor *vmdesc);
 
 bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque);
 
migration/savevm.c +42 -24

127 lines not on this page · raw message

migration/vmstate.c +39 -24

161 lines not on this page · raw message

tests/Makefile +1 -1
index 19a9ee0..e363720 100644
@@ -424,7 +424,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
 	$(test-qapi-obj-y)
 tests/test-vmstate$(EXESUF): tests/test-vmstate.o \
 	migration/vmstate.o migration/qemu-file.o migration/qemu-file-buf.o \
-        migration/qemu-file-unix.o qjson.o \
+        migration/qemu-file-unix.o qapi/json-output-visitor.o \
 	$(test-qom-obj-y)
 tests/test-timed-average$(EXESUF): tests/test-timed-average.o qemu-timer.o \
 	$(test-util-obj-y)
Signature · 1 line
2.4.3
author
#16 lore raw

PATCH v2 11/14 qjson: Remove unused file

In reply to Eric Blake

Now that we have a JSON output visitor, and the previous patch fixed the only client of vmstate to use it, we no longer need the simpler QJSON object doing the same thing.

---
v2: new patch

This is part of alternative 2, along with patch 10. See also patch 8 for alternative 1.

---
 Makefile.objs   |   1 -
 include/qjson.h |  29 -------------
 qjson.c         | 126 --------------------------------------------------------
 3 files changed, 156 deletions(-)
 delete mode 100644 include/qjson.h
 delete mode 100644 qjson.c
Makefile.objs +0 -1
index dac2c02..dbae7f8 100644
@@ -51,7 +51,6 @@ common-obj-$(CONFIG_LINUX) += fsdev/
 common-obj-y += migration/
 common-obj-y += qemu-char.o #aio.o
 common-obj-y += page_cache.o
-common-obj-y += qjson.o
 
 common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
 
include/qjson.h deleted +0 -29
deleted file mode 100644
index 7c54fdf..0000000
@@ -1,29 +0,0 @@
-/*
- * QEMU JSON writer
- *
- * Copyright Alexander Graf
- *
- * Authors:
- *  Alexander Graf <[email protected]>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-#ifndef QEMU_QJSON_H
-#define QEMU_QJSON_H
-
-#define TYPE_QJSON "QJSON"
-typedef struct QJSON QJSON;
-
-QJSON *qjson_new(void);
-void json_prop_str(QJSON *json, const char *name, const char *str);
-void json_prop_int(QJSON *json, const char *name, int64_t val);
-void json_end_array(QJSON *json);
-void json_start_array(QJSON *json, const char *name);
-void json_end_object(QJSON *json);
-void json_start_object(QJSON *json, const char *name);
-const char *qjson_get_str(QJSON *json);
-void qjson_finish(QJSON *json);
-
-#endif /* QEMU_QJSON_H */
qjson.c deleted +0 -126

126 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#17 lore raw

PATCH v2 12/14 qapi: Add qobject_to_json_pretty_prefix()

In reply to Eric Blake

The next patch will add pretty indentation to the JSON visitor. But in order to support pretty output in the type_any() callback, we need to prefix every line of the QObject visitor by the current indentation in the JSON visitor. Hence, a new function qobject_to_json_pretty_indent(), and the old function becomes a thin wrapper to the expanded behavior.

While at it, change 'pretty' to be a bool, to match its usage.

Note that the new simple_pretty() test is a bit sensitive to our current notion of prettiness, as well as to the hash ordering in QDict (most of the tests in check-qobject-json intentionally do not compare the original string to the round-trip string, because we liberally accept more input forms than the canonical form that we output).

---
v2: no change

---
 include/qapi/qmp/qobject-json.h |  1 +
 qobject/qobject-json.c          | 40 ++++++++++++++++++-------
 tests/check-qobject-json.c      | 65 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 11 deletions(-)
include/qapi/qmp/qobject-json.h +1 -0
index ee4d31a..120266b 100644
@@ -25,5 +25,6 @@ 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);
+QString *qobject_to_json_pretty_prefix(const QObject *obj, const char *prefix);
 
 #endif /* QJSON_H */
qobject/qobject-json.c +29 -11
index 3a7b26b..6d0030e 100644
@@ -69,12 +69,14 @@ QObject *qobject_from_jsonf(const char *string, ...)
 typedef struct ToJsonIterState
 {
     int indent;
-    int pretty;
+    bool pretty;
     int count;
     QString *str;
+    const char *prefix;
 } 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,
+                    const char *prefix);
 
 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
 {
@@ -85,13 +87,13 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
     }
 
     if (s->pretty) {
-        qstring_append_format(s->str, "\n%*s", 4 * s->indent, "");
+        qstring_append_format(s->str, "\n%s%*s", s->prefix, 4 * s->indent, "");
     }
 
     qstring_append_json_string(s->str, key);
 
     qstring_append(s->str, ": ");
-    to_json(obj, s->str, s->pretty, s->indent);
+    to_json(obj, s->str, s->pretty, s->indent, s->prefix);
     s->count++;
 }
 
@@ -104,14 +106,15 @@ static void to_json_list_iter(QObject *obj, void *opaque)
     }
 
     if (s->pretty) {
-        qstring_append_format(s->str, "\n%*s", 4 * s->indent, "");
+        qstring_append_format(s->str, "\n%s%*s", s->prefix, 4 * s->indent, "");
     }
 
-    to_json(obj, s->str, s->pretty, s->indent);
+    to_json(obj, s->str, s->pretty, s->indent, s->prefix);
     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,
+                    const char *prefix)
 {
     switch (qobject_type(obj)) {
     case QTYPE_QNULL:
@@ -135,10 +138,11 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.str = str;
         s.indent = indent + 1;
         s.pretty = pretty;
+        s.prefix = prefix;
         qstring_append(str, "{");
         qdict_iter(val, to_json_dict_iter, &s);
         if (pretty) {
-            qstring_append_format(str, "\n%*s", 4 * indent, "");
+            qstring_append_format(str, "\n%s%*s", prefix, 4 * indent, "");
         }
         qstring_append(str, "}");
         break;
@@ -151,10 +155,11 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.str = str;
         s.indent = indent + 1;
         s.pretty = pretty;
+        s.prefix = prefix;
         qstring_append(str, "[");
         qlist_iter(val, (void *)to_json_list_iter, &s);
         if (pretty) {
-            qstring_append_format(str, "\n%*s", 4 * indent, "");
+            qstring_append_format(str, "\n%s%*s", prefix, 4 * indent, "");
         }
         qstring_append(str, "]");
         break;
@@ -185,7 +190,7 @@ QString *qobject_to_json(const QObject *obj)
 {
     QString *str = qstring_new();
 
-    to_json(obj, str, 0, 0);
+    to_json(obj, str, false, 0, "");
 
     return str;
 }
@@ -194,7 +199,20 @@ QString *qobject_to_json_pretty(const QObject *obj)
 {
     QString *str = qstring_new();
 
-    to_json(obj, str, 1, 0);
+    to_json(obj, str, true, 0, "");
+
+    return str;
+}
+
+/*
+ * Pretty-print JSON representing obj, inserting prefix after every newline.
+ * Note that prefix is NOT applied before the first character.
+ */
+QString *qobject_to_json_pretty_prefix(const QObject *obj, const char *prefix)
+{
+    QString *str = qstring_new();
+
+    to_json(obj, str, true, 0, prefix);
 
     return str;
 }
tests/check-qobject-json.c +65 -0

77 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#18 lore raw

PATCH v2 13/14 qapi: Support pretty printing in JSON output visitor

In reply to Eric Blake

Similar to pretty printing in the QObject visitor. The rickiest parts are the fact that during type_any(), we have to coordinate with QObject to also print pretty; and the fact that the testsuite now has to honor parameterization on whether pretty printing is enabled.

---
v2: rebase to earlier changes

---
 include/qapi/json-output-visitor.h |   2 +-
 migration/savevm.c                 |   2 +-
 qapi/json-output-visitor.c         |  25 +++++-
 tests/test-json-output-visitor.c   | 160 ++++++++++++++++++++++++++-----------
 4 files changed, 136 insertions(+), 53 deletions(-)
include/qapi/json-output-visitor.h +1 -1
index 5be5a13..e5e16e6 100644
@@ -15,7 +15,7 @@
 
 typedef struct JsonOutputVisitor JsonOutputVisitor;
 
-JsonOutputVisitor *json_output_visitor_new(void);
+JsonOutputVisitor *json_output_visitor_new(bool pretty);
 void json_output_visitor_cleanup(JsonOutputVisitor *v);
 void json_output_visitor_reset(JsonOutputVisitor *v);
 
migration/savevm.c +1 -1
index a739b8b..9a05a3d 100644
@@ -1077,7 +1077,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         return;
     }
 
-    vmdesc_jov = json_output_visitor_new();
+    vmdesc_jov = json_output_visitor_new(false);
     vmdesc = json_output_get_visitor(vmdesc_jov);
     visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort);
     tmp_i = TARGET_PAGE_SIZE;
qapi/json-output-visitor.c +22 -3
index 8183b85..0c5be38 100644
@@ -18,6 +18,7 @@
 struct JsonOutputVisitor {
     Visitor visitor;
     QString *str;
+    bool pretty;
     bool comma;
     unsigned int depth;
 };
@@ -33,10 +34,13 @@ static void json_output_name(JsonOutputVisitor *jov, const char *name)
         jov->str = qstring_new();
     }
     if (jov->comma) {
-        qstring_append(jov->str, ", ");
+        qstring_append(jov->str, jov->pretty ? "," : ", ");
     } else {
         jov->comma = true;
     }
+    if (jov->pretty && jov->depth) {
+        qstring_append_format(jov->str, "\n%*s", 4 * jov->depth, "");
+    }
     if (name && jov->depth) {
         qstring_append_json_string(jov->str, name);
         qstring_append(jov->str, ": ");
@@ -59,6 +63,9 @@ static void json_output_end_struct(Visitor *v)
     JsonOutputVisitor *jov = to_jov(v);
     assert(jov->depth);
     jov->depth--;
+    if (jov->pretty) {
+        qstring_append_format(jov->str, "\n%*s", 4 * jov->depth, "");
+    }
     qstring_append(jov->str, "}");
     jov->comma = true;
 }
@@ -86,6 +93,9 @@ static void json_output_end_list(Visitor *v)
     JsonOutputVisitor *jov = to_jov(v);
     assert(jov->depth);
     jov->depth--;
+    if (jov->pretty) {
+        qstring_append_format(jov->str, "\n%*s", 4 * jov->depth, "");
+    }
     qstring_append(jov->str, "]");
     jov->comma = true;
 }
@@ -135,7 +145,15 @@ static void json_output_type_any(Visitor *v, const char *name, QObject **obj,
                                  Error **errp)
 {
     JsonOutputVisitor *jov = to_jov(v);
-    QString *str = qobject_to_json(*obj);
+    QString *str;
+
+    if (jov->pretty) {
+        char *prefix = g_strdup_printf("%*s", 4 * jov->depth, "");
+        str = qobject_to_json_pretty_prefix(*obj, prefix);
+        g_free(prefix);
+    } else {
+        str = qobject_to_json(*obj);
+    }
     assert(str);
     json_output_name(jov, name);
     qstring_append(jov->str, qstring_get_str(str));
@@ -179,11 +197,12 @@ void json_output_visitor_cleanup(JsonOutputVisitor *v)
     g_free(v);
 }
 
-JsonOutputVisitor *json_output_visitor_new(void)
+JsonOutputVisitor *json_output_visitor_new(bool pretty)
 {
     JsonOutputVisitor *v;
 
     v = g_malloc0(sizeof(*v));
+    v->pretty = pretty;
 
     v->visitor.start_struct = json_output_start_struct;
     v->visitor.end_struct = json_output_end_struct;
tests/test-json-output-visitor.c +112 -48

224 lines not on this page · raw message

Signature · 1 line
2.4.3
#19 lore raw

Re: [Qemu-devel] [PATCH v2 13/14] qapi: Support pretty printing in JSON output visitor

Quoted 1 line · Eric Blake

Similar to pretty printing in the QObject visitor. The rickiest

"trickiest"?

Quoted 6 lines

parts are the fact that during type_any(), we have to coordinate with QObject to also print pretty; and the fact that the testsuite now has to honor parameterization on whether pretty printing is enabled.

author
#20 lore raw

Re: [Qemu-devel] [PATCH v2 13/14] qapi: Support pretty printing in JSON output visitor

Quoted 4 lines · Fam Zheng
Quoted 1 line · Eric Blake

Similar to pretty printing in the QObject visitor. The rickiest

"trickiest"?

Yep. Fixed in my local tree.

Quoted 8 lines
Quoted 7 lines

parts are the fact that during type_any(), we have to coordinate with QObject to also print pretty; and the fact that the testsuite now has to honor parameterization on whether pretty printing is enabled.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#21 lore raw

PATCH v2 14/14 qemu-img: Use new JSON output formatter

In reply to Eric Blake

Now that we can pretty-print straight to JSON from a visitor, we can eliminate the temporary conversion into QObject inside qemu-img.

The changes to qemu-iotests 043 expected output demonstrates the fact that output is now done in qapi declaration order, rather than QDict hash order.

---
v2: Drop RFC, adjust expected output of 043; rebase to 'name' motion

Overlaps with Fam's qemu-img edits, although he has expressed interest in getting this one in first.
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01756.html
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01806.html

---
 qemu-img.c                 | 69 +++++++++++++++++++---------------------------
 tests/qemu-iotests/043.out | 22 +++++++--------
 2 files changed, 40 insertions(+), 51 deletions(-)
qemu-img.c +29 -40
index d9a2c74..59d3944 100644
@@ -22,9 +22,9 @@
  * THE SOFTWARE.
  */
 #include "qapi-visit.h"
-#include "qapi/qmp-output-visitor.h"
+#include "qapi/json-output-visitor.h"
 #include "qapi/qmp/qerror.h"
-#include "qapi/qmp/qobject-json.h"
+#include "qapi/qmp/qstring.h"
 #include "qemu-common.h"
 #include "qemu/option.h"
 #include "qemu/error-report.h"
@@ -375,19 +375,15 @@ fail:
 
 static void dump_json_image_check(ImageCheck *check, bool quiet)
 {
-    Error *local_err = NULL;
-    QString *str;
-    QmpOutputVisitor *ov = qmp_output_visitor_new();
-    QObject *obj;
-    visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check,
-                          &local_err);
-    obj = qmp_output_get_qobject(ov);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
-    qprintf(quiet, "%s\n", qstring_get_str(str));
-    qobject_decref(obj);
-    qmp_output_visitor_cleanup(ov);
-    QDECREF(str);
+    char *str;
+    JsonOutputVisitor *ov = json_output_visitor_new(true);
+    visit_type_ImageCheck(json_output_get_visitor(ov), NULL,
+                          &check, &error_abort);
+    str = json_output_get_string(ov);
+    assert(str);
+    qprintf(quiet, "%s\n", str);
+    g_free(str);
+    json_output_visitor_cleanup(ov);
 }
 
 static void dump_human_image_check(ImageCheck *check, bool quiet)
@@ -1926,35 +1922,28 @@ static void dump_snapshots(BlockDriverState *bs)
 
 static void dump_json_image_info_list(ImageInfoList *list)
 {
-    Error *local_err = NULL;
-    QString *str;
-    QmpOutputVisitor *ov = qmp_output_visitor_new();
-    QObject *obj;
-    visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list,
-                             &local_err);
-    obj = qmp_output_get_qobject(ov);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
-    printf("%s\n", qstring_get_str(str));
-    qobject_decref(obj);
-    qmp_output_visitor_cleanup(ov);
-    QDECREF(str);
+    char *str;
+    JsonOutputVisitor *ov = json_output_visitor_new(true);
+    visit_type_ImageInfoList(json_output_get_visitor(ov), NULL,
+                             &list, &error_abort);
+    str = json_output_get_string(ov);
+    assert(str);
+    printf("%s\n", str);
+    json_output_visitor_cleanup(ov);
+    g_free(str);
 }
 
 static void dump_json_image_info(ImageInfo *info)
 {
-    Error *local_err = NULL;
-    QString *str;
-    QmpOutputVisitor *ov = qmp_output_visitor_new();
-    QObject *obj;
-    visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err);
-    obj = qmp_output_get_qobject(ov);
-    str = qobject_to_json_pretty(obj);
-    assert(str != NULL);
-    printf("%s\n", qstring_get_str(str));
-    qobject_decref(obj);
-    qmp_output_visitor_cleanup(ov);
-    QDECREF(str);
+    char *str;
+    JsonOutputVisitor *ov = json_output_visitor_new(true);
+    visit_type_ImageInfo(json_output_get_visitor(ov), NULL, &info,
+                         &error_abort);
+    str = json_output_get_string(ov);
+    assert(str);
+    printf("%s\n", str);
+    json_output_visitor_cleanup(ov);
+    g_free(str);
 }
 
 static void dump_human_image_info_list(ImageInfoList *list)
tests/qemu-iotests/043.out +11 -11
index b37d2a3..41697a2 100644
@@ -40,29 +40,29 @@ cluster_size: 65536
 == finite chain of length 3 (json) ==
 [
     {
-        "virtual-size": 134217728,
         "filename": "TEST_DIR/t.IMGFMT",
-        "cluster-size": 65536,
         "format": "IMGFMT",
-        "full-backing-filename": "TEST_DIR/t.IMGFMT.2.base",
+        "dirty-flag": false,
+        "virtual-size": 134217728,
+        "cluster-size": 65536,
         "backing-filename": "TEST_DIR/t.IMGFMT.2.base",
-        "dirty-flag": false
+        "full-backing-filename": "TEST_DIR/t.IMGFMT.2.base",
     },
     {
-        "virtual-size": 134217728,
         "filename": "TEST_DIR/t.IMGFMT.2.base",
-        "cluster-size": 65536,
         "format": "IMGFMT",
-        "full-backing-filename": "TEST_DIR/t.IMGFMT.1.base",
+        "dirty-flag": false,
+        "virtual-size": 134217728,
+        "cluster-size": 65536,
         "backing-filename": "TEST_DIR/t.IMGFMT.1.base",
-        "dirty-flag": false
+        "full-backing-filename": "TEST_DIR/t.IMGFMT.1.base",
     },
     {
-        "virtual-size": 134217728,
         "filename": "TEST_DIR/t.IMGFMT.1.base",
-        "cluster-size": 65536,
         "format": "IMGFMT",
-        "dirty-flag": false
+        "dirty-flag": false,
+        "virtual-size": 134217728,
+        "cluster-size": 65536,
     }
 ]
 *** done
Signature · 1 line
2.4.3
author
#22 lore raw
In reply to Eric Blake
Quoted 3 lines · Eric Blake

Prerequisites:
+ my qapi cleanups subset E v8:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg03863.html

Also available as a tag at this location:
git fetch git://repo.or.cz/qemu/ericb.git qapi-jsonv2

and part of my branch of pending qapi changes, at:
http://repo.or.cz/qemu/ericb.git/shortlog/refs/heads/qapi

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org