lib/json: add utility function for writeing key and value at once.

Change-Id: Ibbcf8cebff33159eff8345a5f0f5166cd18ea4b3
Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/400600
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Pawel Wodkowski 2018-02-14 17:29:46 +01:00 committed by Jim Harris
parent a26050b3e6
commit d6408c0dd1
2 changed files with 100 additions and 0 deletions

View File

@ -241,6 +241,19 @@ int spdk_json_write_val(struct spdk_json_write_ctx *w, const struct spdk_json_va
*/
int spdk_json_write_val_raw(struct spdk_json_write_ctx *w, const void *data, size_t len);
/* 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_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_uint64(struct spdk_json_write_ctx *w, const char *name, uint64_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_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)));
int spdk_json_write_named_array_begin(struct spdk_json_write_ctx *w, const char *name);
int spdk_json_write_named_object_begin(struct spdk_json_write_ctx *w, const char *name);
#ifdef __cplusplus
}
#endif

View File

@ -579,3 +579,90 @@ spdk_json_write_val(struct spdk_json_write_ctx *w, const struct spdk_json_val *v
return fail(w);
}
int spdk_json_write_named_null(struct spdk_json_write_ctx *w, const char *name)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_null(w);
}
int spdk_json_write_named_bool(struct spdk_json_write_ctx *w, const char *name, bool val)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_bool(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);
return rc ? rc : spdk_json_write_int32(w, val);
}
int spdk_json_write_named_uint32(struct spdk_json_write_ctx *w, const char *name, uint32_t val)
{
int rc = spdk_json_write_name(w, 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);
return rc ? rc : spdk_json_write_int64(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);
return rc ? rc : spdk_json_write_string(w, val);
}
int spdk_json_write_named_string_fmt(struct spdk_json_write_ctx *w, const char *name,
const char *fmt, ...)
{
char *s;
va_list args;
int rc;
rc = spdk_json_write_name(w, name);
if (rc) {
return rc;
}
va_start(args, fmt);
s = spdk_vsprintf_alloc(fmt, args);
va_end(args);
if (s == NULL) {
return -1;
}
rc = spdk_json_write_string(w, s);
free(s);
return rc;
}
int spdk_json_write_named_array_begin(struct spdk_json_write_ctx *w, const char *name)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_array_begin(w);
}
int spdk_json_write_named_object_begin(struct spdk_json_write_ctx *w, const char *name)
{
int rc = spdk_json_write_name(w, name);
return rc ? rc : spdk_json_write_object_begin(w);
}