json: add spdk_json_write_named_double
Signed-off-by: Thanos Makatos <thanos.makatos@nutanix.com> Suggested-by: Jim Harris <james.r.harris@intel.com> Change-Id: I2439cd739240fb2d95c5cdaccc557ba9a8f6501b Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15490 Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com> Reviewed-by: John Levon <levon@movementarian.org> Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
parent
4475295e15
commit
70f185ea51
@ -60,6 +60,11 @@ It can now be found inside `examples/bdev/bdevperf`.
|
||||
New API `spdk_fd_group_get_epoll_event` that returns the epoll(7) event that
|
||||
caused a function callback in file descriptor group to execute.
|
||||
|
||||
### json
|
||||
|
||||
Added API `spdk_json_write_double` and `spdk_json_write_named_double` to allow
|
||||
for writing and decoding of the the double data type.
|
||||
|
||||
## v22.09
|
||||
|
||||
### accel
|
||||
|
@ -188,6 +188,7 @@ int spdk_json_write_uint32(struct spdk_json_write_ctx *w, uint32_t val);
|
||||
int spdk_json_write_int64(struct spdk_json_write_ctx *w, int64_t val);
|
||||
int spdk_json_write_uint64(struct spdk_json_write_ctx *w, uint64_t val);
|
||||
int spdk_json_write_uint128(struct spdk_json_write_ctx *w, uint64_t low_val, uint64_t high_val);
|
||||
int spdk_json_write_double(struct spdk_json_write_ctx *w, double val);
|
||||
int spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val);
|
||||
int spdk_json_write_string_raw(struct spdk_json_write_ctx *w, const char *val, size_t len);
|
||||
int spdk_json_write_bytearray(struct spdk_json_write_ctx *w, const void *val, size_t len);
|
||||
@ -243,6 +244,8 @@ int spdk_json_write_named_int64(struct spdk_json_write_ctx *w, const char *name,
|
||||
int spdk_json_write_named_uint64(struct spdk_json_write_ctx *w, const char *name, uint64_t val);
|
||||
int spdk_json_write_named_uint128(struct spdk_json_write_ctx *w, const char *name,
|
||||
uint64_t low_val, uint64_t high_val);
|
||||
int spdk_json_write_named_double(struct spdk_json_write_ctx *w, const char *name, double val);
|
||||
|
||||
int spdk_json_write_named_string(struct spdk_json_write_ctx *w, const char *name, const char *val);
|
||||
int spdk_json_write_named_string_fmt(struct spdk_json_write_ctx *w, const char *name,
|
||||
const char *fmt, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||
|
@ -7,7 +7,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
|
||||
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
|
||||
|
||||
SO_VER := 4
|
||||
SO_MINOR := 0
|
||||
SO_MINOR := 1
|
||||
|
||||
C_SRCS = json_parse.c json_util.c json_write.c
|
||||
LIBNAME = json
|
||||
|
@ -309,6 +309,18 @@ spdk_json_write_named_uint128(struct spdk_json_write_ctx *w, const char *name,
|
||||
return rc ? rc : spdk_json_write_uint128(w, low_val, high_val);
|
||||
}
|
||||
|
||||
int
|
||||
spdk_json_write_double(struct spdk_json_write_ctx *w, double val)
|
||||
{
|
||||
char buf[32];
|
||||
int count;
|
||||
|
||||
if (begin_value(w)) { return fail(w); }
|
||||
count = snprintf(buf, sizeof(buf), "%.20e", val);
|
||||
if (count <= 0 || (size_t)count >= sizeof(buf)) { return fail(w); }
|
||||
return emit(w, buf, count);
|
||||
}
|
||||
|
||||
static void
|
||||
write_hex_2(void *dest, uint8_t val)
|
||||
{
|
||||
@ -724,6 +736,14 @@ spdk_json_write_named_uint64(struct spdk_json_write_ctx *w, const char *name, ui
|
||||
return rc ? rc : spdk_json_write_uint64(w, val);
|
||||
}
|
||||
|
||||
int
|
||||
spdk_json_write_named_double(struct spdk_json_write_ctx *w, const char *name, double val)
|
||||
{
|
||||
int rc = spdk_json_write_name(w, name);
|
||||
|
||||
return rc ? rc : spdk_json_write_double(w, val);
|
||||
}
|
||||
|
||||
int
|
||||
spdk_json_write_named_string(struct spdk_json_write_ctx *w, const char *name, const char *val)
|
||||
{
|
||||
|
@ -37,6 +37,7 @@
|
||||
spdk_json_write_int64;
|
||||
spdk_json_write_uint64;
|
||||
spdk_json_write_uint128;
|
||||
spdk_json_write_double;
|
||||
spdk_json_write_string;
|
||||
spdk_json_write_string_raw;
|
||||
spdk_json_write_string_utf16le;
|
||||
@ -62,6 +63,7 @@
|
||||
spdk_json_write_named_int64;
|
||||
spdk_json_write_named_uint64;
|
||||
spdk_json_write_named_uint128;
|
||||
spdk_json_write_named_double;
|
||||
spdk_json_write_named_string;
|
||||
spdk_json_write_named_string_fmt;
|
||||
spdk_json_write_named_string_fmt_v;
|
||||
|
@ -49,6 +49,10 @@ write_cb(void *cb_ctx, const void *data, size_t size)
|
||||
#define END_NOCMP() \
|
||||
CU_ASSERT(spdk_json_write_end(w) == 0)
|
||||
|
||||
#define END_SIZE_NOCMP(size) \
|
||||
CU_ASSERT(spdk_json_write_end(w) == 0); \
|
||||
CU_ASSERT(g_write_pos - g_buf == size)
|
||||
|
||||
#define END_FAIL() \
|
||||
CU_ASSERT(spdk_json_write_end(w) < 0)
|
||||
|
||||
@ -94,6 +98,8 @@ write_cb(void *cb_ctx, const void *data, size_t size)
|
||||
#define VAL_NAME_UINT128(name, low, high) \
|
||||
CU_ASSERT(spdk_json_write_named_uint128(w, name, low, high) == 0);
|
||||
|
||||
#define VAL_DOUBLE(d) CU_ASSERT(spdk_json_write_double(w, d) == 0);
|
||||
|
||||
#define VAL_ARRAY_BEGIN() CU_ASSERT(spdk_json_write_array_begin(w) == 0)
|
||||
#define VAL_ARRAY_END() CU_ASSERT(spdk_json_write_array_end(w) == 0)
|
||||
|
||||
@ -521,6 +527,29 @@ test_write_number_uint64(void)
|
||||
END("18446744073709551615");
|
||||
}
|
||||
|
||||
static void
|
||||
test_write_number_double(void)
|
||||
{
|
||||
struct spdk_json_write_ctx *w;
|
||||
|
||||
BEGIN();
|
||||
VAL_DOUBLE(0);
|
||||
END_SIZE("0.00000000000000000000e+00", 26);
|
||||
|
||||
BEGIN();
|
||||
VAL_DOUBLE(1.2);
|
||||
END_SIZE("1.19999999999999995559e+00", 26);
|
||||
|
||||
|
||||
BEGIN();
|
||||
VAL_DOUBLE(1234.5678);
|
||||
END_SIZE("1.23456780000000003383e+03", 26);
|
||||
|
||||
BEGIN();
|
||||
VAL_DOUBLE(-1234.5678);
|
||||
END_SIZE("-1.23456780000000003383e+03", 27);
|
||||
}
|
||||
|
||||
static void
|
||||
test_write_array(void)
|
||||
{
|
||||
@ -837,6 +866,7 @@ main(int argc, char **argv)
|
||||
CU_ADD_TEST(suite, test_write_string_number_uint128);
|
||||
CU_ADD_TEST(suite, test_write_number_int64);
|
||||
CU_ADD_TEST(suite, test_write_number_uint64);
|
||||
CU_ADD_TEST(suite, test_write_number_double);
|
||||
CU_ADD_TEST(suite, test_write_array);
|
||||
CU_ADD_TEST(suite, test_write_object);
|
||||
CU_ADD_TEST(suite, test_write_nesting);
|
||||
|
Loading…
Reference in New Issue
Block a user