From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: Luiz Capitulino <lcapitulino@redhat.com>
Subject: [Qemu-devel] [PATCH 04/11] qapi: Factor out JSON number formatting
Date: Thu, 10 Dec 2015 16:53:34 -0700 [thread overview]
Message-ID: <1449791621-16185-5-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1449791621-16185-1-git-send-email-eblake@redhat.com>
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.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
include/qapi/qmp/qstring.h | 4 +++-
qobject/qobject-json.c | 24 +++---------------------
qobject/qstring.c | 37 ++++++++++++++++++++++++++++++++++++-
3 files changed, 42 insertions(+), 23 deletions(-)
diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h
index 1a938f6..cab1faf 100644
--- a/include/qapi/qmp/qstring.h
+++ b/include/qapi/qmp/qstring.h
@@ -1,7 +1,7 @@
/*
* QString Module
*
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2015 Red Hat Inc.
*
* Authors:
* Luiz Capitulino <lcapitulino@redhat.com>
@@ -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);
diff --git a/qobject/qobject-json.c b/qobject/qobject-json.c
index 1557ec6..dbe0ab8 100644
--- a/qobject/qobject-json.c
+++ b/qobject/qobject-json.c
@@ -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: {
diff --git a/qobject/qstring.c b/qobject/qstring.c
index 2882c47..c83e63c 100644
--- a/qobject/qstring.c
+++ b/qobject/qstring.c
@@ -1,7 +1,7 @@
/*
* QString Module
*
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2015 Red Hat Inc.
*
* Authors:
* Luiz Capitulino <lcapitulino@redhat.com>
@@ -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)
--
2.4.3
next prev parent reply other threads:[~2015-12-10 23:53 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-10 23:53 [Qemu-devel] [RFC PATCH 00/11] Add qapi-to-JSON output visitor Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 01/11] qapi: Rename qjson.h to qobject-json.h Eric Blake
2015-12-11 11:00 ` Paolo Bonzini
2015-12-10 23:53 ` [Qemu-devel] [PATCH 02/11] qapi: Improve use of qmp/types.h Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 03/11] qapi: Factor out JSON string escaping Eric Blake
2015-12-10 23:53 ` Eric Blake [this message]
2015-12-10 23:53 ` [Qemu-devel] [PATCH 05/11] qapi: Use qstring_append_chr() where appropriate Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 06/11] qapi: Add qstring_append_format() Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 07/11] qapi: add json output visitor Eric Blake
2015-12-11 0:24 ` Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 08/11] qjson: Simplify by using json-output-visitor Eric Blake
2015-12-11 11:10 ` Paolo Bonzini
2015-12-11 13:42 ` Eric Blake
2015-12-11 13:45 ` Paolo Bonzini
2015-12-18 23:06 ` Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 09/11] qapi: Add qobject_to_json_pretty_prefix() Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 10/11] qapi: Support pretty printing in JSON output visitor Eric Blake
2015-12-10 23:53 ` [Qemu-devel] [PATCH 11/11] RFC: qemu-img: Use new JSON output formatter Eric Blake
2015-12-11 1:36 ` Fam Zheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1449791621-16185-5-git-send-email-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).