Skip to contentSkip to conversation

Add qapi-to-JSON output visitor

RFC PATCH Superseded Eric Blake 3 participants 19 messages Dec 10, 2015 – Dec 18, 2015

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

cover Add qapi-to-JSON output visitor

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

RFC because it conflicts with other pending qemu-img patches:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01756.html
and because I still have testsuite failures in qemu-iotests that must be addressed before the series can be applied (some output reordering occurs, but it also flushed out some clients that are abusing qapi visits by expecting NULL to turn into ""). Posting now to at least get feedback on whether the ideas make sense.

Pending prerequisites:
+ Markus' qapi-not-next branch (including my qapi subset D patches):
http://repo.or.cz/qemu/armbru.git/shortlog/refs/heads/qapi-not-next
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg00463.html
+ v8 or later of my qapi subset E patches (at the time of this mail, only v7 has been posted):
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01181.html

Also available as a tag at this location (with prerequisites applied):
git fetch git://repo.or.cz/qemu/ericb.git qapi-jsonv1

Eric Blake (11):
qapi: Rename 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
qapi: Add qobject_to_json_pretty_prefix()
qapi: Support pretty printing in JSON output visitor
RFC: qemu-img: Use new JSON output formatter

 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 +-
 hw/pci/pcie_aer.c                             |   1 +
 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 -
 monitor.c                                     |   8 +-
 qapi/Makefile.objs                            |   2 +-
 qapi/json-output-visitor.c                    | 221 ++++++++++++
 qapi/qmp-dispatch.c                           |   1 +
 qapi/qmp-event.c                              |   2 +-
 qemu-img.c                                    |  70 ++--
 qga/main.c                                    |   2 +-
 qjson.c                                       |  64 ++--
 qobject/Makefile.objs                         |   3 +-
 qobject/json-parser.c                         |  14 +-
 qobject/{qjson.c => qobject-json.c}           | 147 ++------
 qobject/qobject.c                             |   7 +-
 qobject/qstring.c                             | 119 ++++++-
 target-s390x/kvm.c                            |   2 +-
 tests/.gitignore                              |   3 +-
 tests/Makefile                                |  12 +-
 tests/{check-qjson.c => check-qobject-json.c} |  86 ++++-
 tests/libqtest.c                              |   2 +-
 tests/test-json-output-visitor.c              | 474 ++++++++++++++++++++++++++
 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 +-
 40 files changed, 1046 insertions(+), 261 deletions(-)
 create mode 100644 include/qapi/json-output-visitor.h
 rename include/qapi/qmp/{qjson.h => qobject-json.h} (90%)
 create mode 100644 qapi/json-output-visitor.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 1/11 qapi: Rename 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.

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

---
 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 e8cee1e..c943ff4 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 9971976..e611002 100644
@@ -29,7 +29,7 @@
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "qapi/qmp/qerror.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 cd6a587..9009f4f 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 d162459..1e3a278 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 b3ad467..a98304d 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 033011c..cb961e6 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 1e55722..6d3416d 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 053c1ae..17d74e2 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)
@@ -360,7 +360,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 \
@@ -387,7 +387,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 ed1203e..eaf1d0c 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
#3 lore raw

PATCH 2/11 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.

---
 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 9792277..dabeaf0 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 45d06f3..60deaa0 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 cb64500..691d906 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 9f67f9e..8ea1f43 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
author
#4 lore raw

PATCH 3/11 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.

---
 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
author
#5 lore raw

PATCH 4/11 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).

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.

---
 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..c83e63c 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; we append it anyways 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
author
#6 lore raw

PATCH 5/11 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
#7 lore raw

PATCH 6/11 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 an existing client of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation.

---
 include/qapi/qmp/qstring.h |  7 ++++++-
 qjson.c                    |  2 +-
 qobject/qobject-json.c     | 25 +++++--------------------
 qobject/qstring.c          | 23 +++++++++++++++++++----
 4 files changed, 31 insertions(+), 26 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 +19 -4
index c83e63c..a348ae3 100644
@@ -88,12 +88,27 @@ 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;
+    size_t len;
+
+    va_copy(ap2, ap);
+    len = vsnprintf(qstring->string + qstring->length, 0, fmt, ap2);
+    va_end(ap2);
+
+    capacity_increase(qstring, len);
+    vsnprintf(qstring->string + qstring->length, len + 1, fmt, ap);
+    qstring->length += len;
 }
 
 /**
Signature · 1 line
2.4.3
author
#10 lore raw

PATCH 8/11 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.

---
 qjson.c | 61 ++++++++++++++++++++++---------------------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)
qjson.c +22 -39
index 8c93c1b..6aac46d 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, NULL, 0, name, &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, NULL, 0, name, &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, &val, name, &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, (char **)&str, name, &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
#15 lore raw

PATCH 9/11 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).

---
 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
#8 lore raw

PATCH 7/11 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.

---
 include/qapi/json-output-visitor.h |  25 +++
 qapi/Makefile.objs                 |   2 +-
 qapi/json-output-visitor.c         | 202 ++++++++++++++++++
 tests/.gitignore                   |   1 +
 tests/Makefile                     |   4 +
 tests/test-json-output-visitor.c   | 410 +++++++++++++++++++++++++++++++++++++
 6 files changed, 643 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 +202 -0

202 lines not on this page · raw message

tests/.gitignore +1 -0
index 6d3416d..b4f371c 100644
@@ -24,6 +24,7 @@ test-cutils
 test-hbitmap
 test-int128
 test-iov
+test-json-output-visitor
 test-mul64
 test-opts-visitor
 test-qapi-event.[ch]
tests/Makefile +4 -0
index 17d74e2..f617e2d 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)
@@ -363,6 +365,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 \
@@ -447,6 +450,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 +410 -0

410 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#16 lore raw

PATCH 10/11 qapi: Support pretty printing in JSON output visitor

In reply to Eric Blake

Similar to pretty printing in the QObject visitor. Trickiest 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.

---
 include/qapi/json-output-visitor.h |   2 +-
 qapi/json-output-visitor.c         |  25 +++++-
 qjson.c                            |   2 +-
 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);
 
qapi/json-output-visitor.c +22 -3
index 0d759c4..a939af1 100644
@@ -18,6 +18,7 @@
 struct JsonOutputVisitor {
     Visitor visitor;
     QString *str;
+    bool pretty;
     bool comma;
     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;
 }
@@ -85,6 +92,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;
 }
@@ -134,7 +144,15 @@ static void json_output_type_any(Visitor *v, QObject **obj, const char *name,
                                  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));
@@ -178,11 +196,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;
qjson.c +1 -1
index 6aac46d..9debf1d 100644
@@ -84,7 +84,7 @@ void qjson_finish(QJSON *json)
 static void qjson_initfn(Object *obj)
 {
     QJSON *json = QJSON(obj);
-    json->jov = json_output_visitor_new();
+    json->jov = json_output_visitor_new(false);
 }
 
 static void qjson_finalizefn(Object *obj)
tests/test-json-output-visitor.c +112 -48

224 lines not on this page · raw message

Signature · 1 line
2.4.3
author
#17 lore raw

PATCH 11/11 RFC: 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.

RFC because at least qemu-iotests 043 has changed output, not included in this version of the patch. Conflicts with Fam's qemu-img edits, so one of the two of us will get to rebase on the other:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01756.html

---
 qemu-img.c | 70 ++++++++++++++++++++++++++------------------------------------
 1 file changed, 29 insertions(+), 41 deletions(-)
qemu-img.c +29 -41
index cb961e6..08dca98 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),
-                          &check, NULL, &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),
+                          &check, NULL, &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,36 +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),
-                             &list, NULL, &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),
+                             &list, NULL, &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),
-                         &info, NULL, &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),
+                         &info, NULL, &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)
Signature · 1 line
2.4.3
author
#9 lore raw

Re: [Qemu-devel] [PATCH 07/11] qapi: add json output visitor

In reply to Eric Blake
Quoted 22 lines · 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.

qapi/json-output-visitor.c line 131

Quoted 7 lines
qapi/json-output-visitor.c new file +7 -0
+static void json_output_type_number(Visitor *v, double *obj, const char *name,
+                                    Error **errp)
+{
+    JsonOutputVisitor *jov = to_jov(v);
+    json_output_name(jov, name);
+    qstring_append_json_number(jov->str, *obj, errp);
+}

Hmm. The generated qapi callers always pass a non-NULL local errp, and fail the overall iteration even if the upper-level caller passed NULL. My intent here was to allow the choice between diagnosing NaN, or ignoring the problem and producing output anyways; but for that to work, I may need to add a configuration parameter 'bool strict' at the creation of the visitor, that determines whether I pass errp or NULL on to qstring_append_json_number() here.

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

Re: [Qemu-devel] [PATCH 11/11] RFC: qemu-img: Use new JSON output formatter

In reply to Eric Blake
Quoted 9 lines · 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.

RFC because at least qemu-iotests 043 has changed output, not included in this version of the patch. Conflicts with Fam's qemu-img edits, so one of the two of us will get to rebase on the other:
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01756.html

I touched qemu-img map's output, this patch updates info and check, so the context conflict should be okay, but it would be better to get this patch in before mine so I can update my patch to make use of the new formatter, without code churn.

Thanks!

Fam

Quoted 110 lines
---
 qemu-img.c | 70 ++++++++++++++++++++++++++------------------------------------
 1 file changed, 29 insertions(+), 41 deletions(-)
qemu-img.c +29 -41
index cb961e6..08dca98 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),
-                          &check, NULL, &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),
+                          &check, NULL, &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,36 +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),
-                             &list, NULL, &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),
+                             &list, NULL, &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),
-                         &info, NULL, &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),
+                         &info, NULL, &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)
Signature · 1 line
2.4.3
maintainer Reviewed-by on v1 1/11
#2 lore raw

Re: [PATCH 01/11] qapi: Rename qjson.h to qobject-json.h

In reply to Eric Blake
Quoted 424 lines · Eric Blake

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

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

---
 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 e8cee1e..c943ff4 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 9971976..e611002 100644
@@ -29,7 +29,7 @@
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "qapi/qmp/qerror.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 cd6a587..9009f4f 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 d162459..1e3a278 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 b3ad467..a98304d 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 033011c..cb961e6 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 1e55722..6d3416d 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 053c1ae..17d74e2 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)
@@ -360,7 +360,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 \
@@ -387,7 +387,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 +0 -0
index ed1203e..eaf1d0c 100644

vl.c int main(int argc, char **argv) · line 98

Quoted 8 lines
vl.c +1 -1
@@ -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"

I'd rather get rid completely of the other qjson, but the rename makes sense anyway.

Paolo

maintainer
#11 lore raw

Re: [Qemu-devel] [PATCH 08/11] qjson: Simplify by using json-output-visitor

In reply to Eric Blake
Quoted 10 lines · 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.

---
 qjson.c | 61 ++++++++++++++++++++++---------------------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)

I didn't find out which tree your patches apply to, but this:

migration/savevm.c +28 -22
index 0ad1b93..26986d8 100644
@@ -646,7 +646,7 @@ static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
     return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
 }
 
-static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
+static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, Visitor *vmdesc)
 {
     int64_t old_offset, size;
 
@@ -655,14 +655,14 @@ static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdes
     size = qemu_ftell_fast(f) - old_offset;
 
     if (vmdesc) {
-        json_prop_int(vmdesc, "size", size);
-        json_start_array(vmdesc, "fields");
-        json_start_object(vmdesc, NULL);
-        json_prop_str(vmdesc, "name", "data");
-        json_prop_int(vmdesc, "size", size);
-        json_prop_str(vmdesc, "type", "buffer");
-        json_end_object(vmdesc);
-        json_end_array(vmdesc);
+        visit_type_int(vmdesc, size, "size", &error_abort);
+        visit_start_list(vmdesc, NULL, 0, "fields", &error_abort);
+        visit_start_struct(vmdesc, NULL, 0, NULL, &error_abort);
+        visit_type_str(vmdesc, (char **)&"data", "name", &error_abort);
+        visit_type_int(vmdesc, size, "size", &error_abort);
+        visit_type_str(vmdesc, (char **)&"buffer", "type", &error_abort);
+        visit_end_struct(vmdesc);
+        visit_end_list(vmdesc);
     }
 }
 
@@ -1028,7 +1028,9 @@ void qemu_savevm_state_complete_postcopy(QEMUFile *f)
 
 void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
 {
-    QJSON *vmdesc;
+    JsonOutputVisitor *vmdesc_jov;
+    Visitor *vmdesc;
+    char *vmdesc_str;
     int vmdesc_len;
     SaveStateEntry *se;
     int ret;
@@ -1068,9 +1070,11 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         return;
     }
 
-    vmdesc = qjson_new();
-    json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
-    json_start_array(vmdesc, "devices");
+    vmdesc_jov = json_output_visitor_new();
+    vmdesc = json_output_get_visitor(vmdesc_jov);
+    visit_start_struct(vmdesc, NULL, 0, NULL, &error_abort);
+    visit_type_int(vmdesc, TARGET_PAGE_SIZE, "page_size", &error_abort);
+    visit_start_list(vmdesc, NULL, 0, "devices", &error_abort);
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
 
         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
@@ -1083,15 +1087,15 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
 
         trace_savevm_section_start(se->idstr, se->section_id);
 
-        json_start_object(vmdesc, NULL);
-        json_prop_str(vmdesc, "name", se->idstr);
-        json_prop_int(vmdesc, "instance_id", se->instance_id);
+        visit_start_struct(vmdesc, NULL, 0, NULL, &error_abort);
+        visit_type_str(vmdesc, (char **)&se->idstr, "name", &error_abort);
+        visit_type_int(vmdesc, se->instance_id, "instance_id", &error_abort);
 
         save_section_header(f, se, QEMU_VM_SECTION_FULL);
 
         vmstate_save(f, se, vmdesc);
 
-        json_end_object(vmdesc);
+        visit_end_struct(vmdesc);
         trace_savevm_section_end(se->idstr, se->section_id, 0);
         save_section_footer(f, se);
     }
@@ -1101,16 +1105,18 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         qemu_put_byte(f, QEMU_VM_EOF);
     }
 
-    json_end_array(vmdesc);
-    qjson_finish(vmdesc);
-    vmdesc_len = strlen(qjson_get_str(vmdesc));
+    visit_end_list(vmdesc);
+    visit_end_struct(vmdesc);
+    vmdesc_str = json_output_get_string(vmdesc_jov);
+    vmdesc_len = strlen(vmdesc_str);
 
     if (should_send_vmdesc()) {
         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
         qemu_put_be32(f, vmdesc_len);
-        qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
+        qemu_put_buffer(f, (uint8_t *)vmdesc_str, vmdesc_len);
     }
-    object_unref(OBJECT(vmdesc));
+    g_free(vmdesc_str);
+    json_output_visitor_cleanup(vmdesc_jov);
 
     qemu_fflush(f);
 }

compiles and is pretty much a 1:1 translation from the qjson.c API to the visitor API (using this patch as a guide). Feel free to include it and remove qjson.c. Alternatively, you can leave out this patch and I'll test and submit the transition.

Paolo

author
#12 lore raw

Re: [Qemu-devel] [PATCH 08/11] qjson: Simplify by using json-output-visitor

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · 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.

Quoted 6 lines
Quoted 4 lines
---
 qjson.c | 61 ++++++++++++++++++++++---------------------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)

I didn't find out which tree your patches apply to, but this:

Mentioned in the cover letter:

+ v8 or later of my qapi subset E patches (at the time of this mail, only v7 has been posted):
https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg01181.html

Also available as a tag at this location (with prerequisites applied):
git fetch git://repo.or.cz/qemu/ericb.git qapi-jsonv1

(note that it is only a tag, not a branch, and you are right that it doesn't yet apply to any maintainer's tree).

migration/savevm.c void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) · line 1078

Quoted 13 lines
migration/savevm.c +5 -3
@@ -1068,9 +1070,11 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         return;
     }
 
-    vmdesc = qjson_new();
-    json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
-    json_start_array(vmdesc, "devices");
+    vmdesc_jov = json_output_visitor_new();
+    vmdesc = json_output_get_visitor(vmdesc_jov);
+    visit_start_struct(vmdesc, NULL, 0, NULL, &error_abort);
+    visit_type_int(vmdesc, TARGET_PAGE_SIZE, "page_size", &error_abort);
+    visit_start_list(vmdesc, NULL, 0, "devices", &error_abort);
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {

Nice idea.

Quoted 4 lines

compiles and is pretty much a 1:1 translation from the qjson.c API to the visitor API (using this patch as a guide). Feel free to include it and remove qjson.c. Alternatively, you can leave out this patch and I'll test and submit the transition.

Should I squash the two together, or keep my current patch and drop qjson.c as a separate patch?

Also, while reading this, I noticed that qjson.h has signatures that match JSON ('name' comes before 'value', when generating a "name":value pair), while visitor.h has signatures that are backwards ('obj' comes before 'name', even though the output will be "name":value-of-obj). I'm seriously debating about a tree-wide coccinelle patch to reverse the order or arguments of all the visit_type_* functions.

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

Re: [Qemu-devel] [PATCH 08/11] qjson: Simplify by using json-output-visitor

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 6 lines · Eric Blake
Quoted 4 lines
Quoted 4 lines

compiles and is pretty much a 1:1 translation from the qjson.c API to the visitor API (using this patch as a guide). Feel free to include it and remove qjson.c. Alternatively, you can leave out this patch and I'll test and submit the transition.

Should I squash the two together, or keep my current patch and drop qjson.c as a separate patch?

As you prefer.

Quoted 7 lines

Also, while reading this, I noticed that qjson.h has signatures that match JSON ('name' comes before 'value', when generating a "name":value pair), while visitor.h has signatures that are backwards ('obj' comes before 'name', even though the output will be "name":value-of-obj). I'm seriously debating about a tree-wide coccinelle patch to reverse the order or arguments of all the visit_type_* functions.

That would be doable, there aren't many users outside generated code.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJWatN5AAoJEL/70l94x66D8PQIAKEzIEUqjfhDMLhov4NwxGs3 LvyW+/w5AyDEZOJMayBuc3y7fSTbLp/eMQPsfBOIvBCMr/aEE4Tij0CzW3JE4FnH IFCW94F8Q7vq8jTYKFU6222Nk7vUGOLG9y3jQSnLX4RzpRizhWjk73DtkED7YVu3 5J0Ef3+ZEobCZA9zOwQT7+pqc9ah/M6tGsZodvML2FopCk/SsTBauPjoSeP0knHE 1MJsu/KVjn1508yn1SY9d0HJKQx2dM3nzQRtp0xbWx7x8o7u826axpVQieq7YBpb PaRW/vqiXvdnRkrCRHZAL6PJ4GLyquX42E7HjqCQjibA06fbtqmLoYQA2A/2Pyg= =EJIn
-----END PGP SIGNATURE-----

author
#14 lore raw

Re: [Qemu-devel] [PATCH 08/11] qjson: Simplify by using json-output-visitor

In reply to Paolo Bonzini
Quoted 18 lines · Paolo Bonzini
Quoted 10 lines · 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.

---
 qjson.c | 61 ++++++++++++++++++++++---------------------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)

I didn't find out which tree your patches apply to, but this:

     if (vmdesc) {

migration/savevm.c static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdes · line 658

Quoted 9 lines
migration/savevm.c +1 -8
-        json_prop_int(vmdesc, "size", size);
-        json_start_array(vmdesc, "fields");
-        json_start_object(vmdesc, NULL);
-        json_prop_str(vmdesc, "name", "data");
-        json_prop_int(vmdesc, "size", size);
-        json_prop_str(vmdesc, "type", "buffer");
-        json_end_object(vmdesc);
-        json_end_array(vmdesc);
+        visit_type_int(vmdesc, size, "size", &error_abort);

visit_type_int() takes 'int64_t*', not 'int'. In places, that means I'd have to add a temporary variable to cover the fact that the input is not the right type for writing '&size'

migration/savevm.c void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) · line 1076

Quoted 11 lines
migration/savevm.c +4 -3
@@ -1068,9 +1070,11 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         return;
     }
 
-    vmdesc = qjson_new();
-    json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
-    json_start_array(vmdesc, "devices");
+    vmdesc_jov = json_output_visitor_new();
+    vmdesc = json_output_get_visitor(vmdesc_jov);
+    visit_start_struct(vmdesc, NULL, 0, NULL, &error_abort);
+    visit_type_int(vmdesc, TARGET_PAGE_SIZE, "page_size", &error_abort);

...or even cover the case where it is a constant whose address does not even exist.

Quoted 5 lines

compiles and is pretty much a 1:1 translation from the qjson.c API to the visitor API (using this patch as a guide). Feel free to include it and remove qjson.c. Alternatively, you can leave out this patch and I'll test and submit the transition.

So the patch to avoid qjson.c is not quite 1:1. Still, I'll go ahead and complete the patch for my v2, to see if you still like it.

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