Commit c56b53a8 authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

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: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent fc411e38
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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,
+58 −3
Original line number Diff line number Diff line
@@ -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