diff options
author | Marc-André Lureau <marcandre.lureau@redhat.com> | 2017-06-07 20:35:58 +0400 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2017-06-20 14:31:31 +0200 |
commit | 01b2ffcedd94ad7b42bc870e4c6936c87ad03429 (patch) | |
tree | 39bbadfbbaa229bfbda245840a256fe0132b2390 /qobject/qjson.c | |
parent | 58634047b7deeab36e4b07c4744e44d698975561 (diff) | |
download | qemu-01b2ffcedd94ad7b42bc870e4c6936c87ad03429.zip qemu-01b2ffcedd94ad7b42bc870e4c6936c87ad03429.tar.gz qemu-01b2ffcedd94ad7b42bc870e4c6936c87ad03429.tar.bz2 |
qapi: merge QInt and QFloat in QNum
We would like to use a same QObject type to represent numbers, whether
they are int, uint, or floats. Getters will allow some compatibility
between the various types if the number fits other representations.
Add a few more tests while at it.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20170607163635.17635-7-marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[parse_stats_intervals() simplified a bit, comment in
test_visitor_in_int_overflow() tidied up, suppress bogus warnings]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Diffstat (limited to 'qobject/qjson.c')
-rw-r--r-- | qobject/qjson.c | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/qobject/qjson.c b/qobject/qjson.c index b2f3bfe..2e09308 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -132,12 +132,11 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) case QTYPE_QNULL: qstring_append(str, "null"); break; - case QTYPE_QINT: { - QInt *val = qobject_to_qint(obj); - char buffer[1024]; - - snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val)); + case QTYPE_QNUM: { + QNum *val = qobject_to_qnum(obj); + char *buffer = qnum_to_string(val); qstring_append(str, buffer); + g_free(buffer); break; } case QTYPE_QSTRING: { @@ -234,34 +233,6 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) qstring_append(str, "]"); break; } - 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. Dependence - * on C locale is a pervasive issue in QEMU. */ - /* FIXME: This risks printing Inf or NaN, which are not valid - * JSON values. */ - /* FIXME: the default precision of 6 for %f often causes - * rounding errors; we should be using DBL_DECIMAL_DIG (17), - * and only rounding to a shorter number if the result would - * still produce the same floating point value. */ - 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); - break; - } case QTYPE_QBOOL: { QBool *val = qobject_to_qbool(obj); |