From c56b53a89e9b70f110d39cc42c68751a8abf5f22 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 26 Oct 2016 14:52:52 -0700 Subject: [PATCH] json: add flag to format when writing Specify SPDK_JSON_WRITE_FLAG_FORMATTED when creating a write context to output more human-readable JSON. Change-Id: Ie1f0451496aae7e36e4cdb1f05edb4bc4963be17 Signed-off-by: Daniel Verkamp --- include/spdk/json.h | 2 ++ lib/json/json_write.c | 61 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/include/spdk/json.h b/include/spdk/json.h index 2e5a2c493..259ce6c49 100644 --- a/include/spdk/json.h +++ b/include/spdk/json.h @@ -185,6 +185,8 @@ int spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num); struct spdk_json_write_ctx; +#define SPDK_JSON_WRITE_FLAG_FORMATTED 0x00000001 + typedef int (*spdk_json_write_cb)(void *cb_ctx, const void *data, size_t size); struct spdk_json_write_ctx *spdk_json_write_begin(spdk_json_write_cb write_cb, void *cb_ctx, diff --git a/lib/json/json_write.c b/lib/json/json_write.c index 017c35885..913e161c4 100644 --- a/lib/json/json_write.c +++ b/lib/json/json_write.c @@ -36,6 +36,9 @@ struct spdk_json_write_ctx { spdk_json_write_cb write_cb; void *cb_ctx; + uint32_t flags; + uint32_t indent; + bool new_indent; bool first_value; bool failed; }; @@ -52,6 +55,9 @@ spdk_json_write_begin(spdk_json_write_cb write_cb, void *cb_ctx, uint32_t flags) w->write_cb = write_cb; w->cb_ctx = cb_ctx; + w->flags = flags; + w->indent = 0; + w->new_indent = false; w->first_value = true; w->failed = false; @@ -93,14 +99,43 @@ emit(struct spdk_json_write_ctx *w, const void *data, size_t size) return 0; } +static int +emit_fmt(struct spdk_json_write_ctx *w, const void *data, size_t size) +{ + if (w->flags & SPDK_JSON_WRITE_FLAG_FORMATTED) { + return emit(w, data, size); + } + return 0; +} + +static int +emit_indent(struct spdk_json_write_ctx *w) +{ + uint32_t i; + + if (w->flags & SPDK_JSON_WRITE_FLAG_FORMATTED) { + for (i = 0; i < w->indent; i++) { + if (emit(w, " ", 2)) return fail(w); + } + } + return 0; +} + static int begin_value(struct spdk_json_write_ctx *w) { // TODO: check for value state + if (w->new_indent) { + if (emit_fmt(w, "\n", 1)) return fail(w); + if (emit_indent(w)) return fail(w); + } if (!w->first_value) { if (emit(w, ",", 1)) return fail(w); + if (emit_fmt(w, "\n", 1)) return fail(w); + if (emit_indent(w)) return fail(w); } w->first_value = false; + w->new_indent = false; return 0; } @@ -263,13 +298,23 @@ spdk_json_write_array_begin(struct spdk_json_write_ctx *w) { if (begin_value(w)) return fail(w); w->first_value = true; - return emit(w, "[", 1); + w->new_indent = true; + w->indent++; + if (emit(w, "[", 1)) return fail(w); + return 0; } int spdk_json_write_array_end(struct spdk_json_write_ctx *w) { w->first_value = false; + if (w->indent == 0) return fail(w); + w->indent--; + if (!w->new_indent) { + if (emit_fmt(w, "\n", 1)) return fail(w); + if (emit_indent(w)) return fail(w); + } + w->new_indent = false; return emit(w, "]", 1); } @@ -278,13 +323,22 @@ spdk_json_write_object_begin(struct spdk_json_write_ctx *w) { if (begin_value(w)) return fail(w); w->first_value = true; - return emit(w, "{", 1); + w->new_indent = true; + w->indent++; + if (emit(w, "{", 1)) return fail(w); + return 0; } int spdk_json_write_object_end(struct spdk_json_write_ctx *w) { w->first_value = false; + w->indent--; + if (!w->new_indent) { + if (emit_fmt(w, "\n", 1)) return fail(w); + if (emit_indent(w)) return fail(w); + } + w->new_indent = false; return emit(w, "}", 1); } @@ -295,7 +349,8 @@ spdk_json_write_name_raw(struct spdk_json_write_ctx *w, const char *name, size_t if (begin_value(w)) return fail(w); if (write_string_or_name(w, name, len)) return fail(w); w->first_value = true; - return emit(w, ":", 1); + if (emit(w, ":", 1)) return fail(w); + return emit_fmt(w, " ", 1); } int