json: add the spdk_json_write_uint8|16 function

Add the paired spdk_json_write_named_uint8|16 function

Signed-off-by: Jacek Kalwas <jacek.kalwas@intel.com>
Change-Id: Ib7ee9ae4dbe9a4e9cfa28750f0b9a0af597d260c
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9788
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Jacek Kalwas 2021-10-07 10:27:08 -04:00 committed by Tomasz Zawadzki
parent 58410e6c79
commit 8f8f3f8703
3 changed files with 55 additions and 9 deletions

View File

@ -209,6 +209,8 @@ struct spdk_json_write_ctx *spdk_json_write_begin(spdk_json_write_cb write_cb, v
int spdk_json_write_end(struct spdk_json_write_ctx *w);
int spdk_json_write_null(struct spdk_json_write_ctx *w);
int spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val);
int spdk_json_write_uint8(struct spdk_json_write_ctx *w, uint8_t val);
int spdk_json_write_uint16(struct spdk_json_write_ctx *w, uint16_t val);
int spdk_json_write_int32(struct spdk_json_write_ctx *w, int32_t val);
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);
@ -260,12 +262,14 @@ int spdk_json_write_val_raw(struct spdk_json_write_ctx *w, const void *data, siz
/* Utility functions */
int spdk_json_write_named_null(struct spdk_json_write_ctx *w, const char *name);
int spdk_json_write_named_bool(struct spdk_json_write_ctx *w, const char *name, bool val);
int spdk_json_write_named_uint8(struct spdk_json_write_ctx *w, const char *name, uint8_t val);
int spdk_json_write_named_uint16(struct spdk_json_write_ctx *w, const char *name, uint16_t val);
int spdk_json_write_named_int32(struct spdk_json_write_ctx *w, const char *name, int32_t val);
int spdk_json_write_named_uint32(struct spdk_json_write_ctx *w, const char *name, uint32_t val);
int spdk_json_write_named_int64(struct spdk_json_write_ctx *w, const char *name, int64_t val);
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_int64(struct spdk_json_write_ctx *w, const char *name, int64_t 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)));

View File

@ -217,6 +217,30 @@ spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val)
}
}
int
spdk_json_write_uint8(struct spdk_json_write_ctx *w, uint8_t val)
{
char buf[32];
int count;
if (begin_value(w)) { return fail(w); }
count = snprintf(buf, sizeof(buf), "%" PRIu8, val);
if (count <= 0 || (size_t)count >= sizeof(buf)) { return fail(w); }
return emit(w, buf, count);
}
int
spdk_json_write_uint16(struct spdk_json_write_ctx *w, uint16_t val)
{
char buf[32];
int count;
if (begin_value(w)) { return fail(w); }
count = snprintf(buf, sizeof(buf), "%" PRIu16, val);
if (count <= 0 || (size_t)count >= sizeof(buf)) { return fail(w); }
return emit(w, buf, count);
}
int
spdk_json_write_int32(struct spdk_json_write_ctx *w, int32_t val)
{
@ -650,6 +674,20 @@ int spdk_json_write_named_bool(struct spdk_json_write_ctx *w, const char *name,
return rc ? rc : spdk_json_write_bool(w, val);
}
int spdk_json_write_named_uint8(struct spdk_json_write_ctx *w, const char *name, uint8_t val)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_uint8(w, val);
}
int spdk_json_write_named_uint16(struct spdk_json_write_ctx *w, const char *name, uint16_t val)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_uint16(w, val);
}
int spdk_json_write_named_int32(struct spdk_json_write_ctx *w, const char *name, int32_t val)
{
int rc = spdk_json_write_name(w, name);
@ -664,13 +702,6 @@ int spdk_json_write_named_uint32(struct spdk_json_write_ctx *w, const char *name
return rc ? rc : spdk_json_write_uint32(w, val);
}
int spdk_json_write_named_uint64(struct spdk_json_write_ctx *w, const char *name, uint64_t val)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_uint64(w, val);
}
int spdk_json_write_named_int64(struct spdk_json_write_ctx *w, const char *name, int64_t val)
{
int rc = spdk_json_write_name(w, name);
@ -678,6 +709,13 @@ int spdk_json_write_named_int64(struct spdk_json_write_ctx *w, const char *name,
return rc ? rc : spdk_json_write_int64(w, val);
}
int spdk_json_write_named_uint64(struct spdk_json_write_ctx *w, const char *name, uint64_t val)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_uint64(w, val);
}
int spdk_json_write_named_string(struct spdk_json_write_ctx *w, const char *name, const char *val)
{
int rc = spdk_json_write_name(w, name);

View File

@ -30,6 +30,8 @@
spdk_json_write_end;
spdk_json_write_null;
spdk_json_write_bool;
spdk_json_write_uint8;
spdk_json_write_uint16;
spdk_json_write_int32;
spdk_json_write_uint32;
spdk_json_write_int64;
@ -52,10 +54,12 @@
spdk_json_write_named_null;
spdk_json_write_named_bool;
spdk_json_write_named_uint8;
spdk_json_write_named_uint16;
spdk_json_write_named_int32;
spdk_json_write_named_uint32;
spdk_json_write_named_uint64;
spdk_json_write_named_int64;
spdk_json_write_named_uint64;
spdk_json_write_named_uint128;
spdk_json_write_named_string;
spdk_json_write_named_string_fmt;