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

json: fix spdk_json_write_val with nested objects



When recursively calling spdk_json_write_val() on another object or
array, the child call will handle printing out the whole
subobject/array, so the parent call should skip over all of its values.

Also fix the return value for the array/object case - if we get to the
end of the array or object, we should return 0 for success.

Change-Id: I1da80c88ab8759620114c1ab141baaaaf9f0023a
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 0f805b36
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -343,12 +343,18 @@ spdk_json_write_val(struct spdk_json_write_ctx *w, const struct spdk_json_val *v
		}

		// Loop up to and including the _END value
		for (i = 0; i < num_values + 1; i++) {
		for (i = 0; i < num_values + 1;) {
			if (spdk_json_write_val(w, &val[i + 1])) {
				return fail(w);
			}
			if (val[i + 1].type == SPDK_JSON_VAL_ARRAY_BEGIN ||
			    val[i + 1].type == SPDK_JSON_VAL_OBJECT_BEGIN) {
				i += val[i + 1].len + 2;
			} else {
				i++;
			}
		break;
		}
		return 0;

	case SPDK_JSON_VAL_ARRAY_END:
		return spdk_json_write_array_end(w);
+1 −0
Original line number Diff line number Diff line
@@ -34,5 +34,6 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)

TEST_FILE = json_write_ut.c
OTHER_FILES = json_parse.c

include $(SPDK_ROOT_DIR)/mk/json.unittest.mk
+20 −1
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@ write_cb(void *cb_ctx, const void *data, size_t size)
#define VAL_OBJECT_BEGIN() CU_ASSERT(spdk_json_write_object_begin(w) == 0)
#define VAL_OBJECT_END() CU_ASSERT(spdk_json_write_object_end(w) == 0)

#define VAL(v) CU_ASSERT(spdk_json_write_val(w, v) == 0)

static void
test_write_literal(void)
{
@@ -580,6 +582,22 @@ test_write_nesting(void)
		"}");
}

/* Round-trip parse and write test */
static void
test_write_val(void)
{
	struct spdk_json_write_ctx *w;
	struct spdk_json_val values[100];
	char src[] = "{\"a\":[1,2,3],\"b\":{\"c\":\"d\"},\"e\":true,\"f\":false,\"g\":null}";

	CU_ASSERT(spdk_json_parse(src, strlen(src), values, sizeof(values) / sizeof(*values), NULL,
				  SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE) == 19);

	BEGIN();
	VAL(values);
	END("{\"a\":[1,2,3],\"b\":{\"c\":\"d\"},\"e\":true,\"f\":false,\"g\":null}");
}

int main(int argc, char **argv)
{
	CU_pSuite	suite = NULL;
@@ -603,7 +621,8 @@ int main(int argc, char **argv)
		CU_add_test(suite, "write_number_uint32", test_write_number_uint32) == NULL ||
		CU_add_test(suite, "write_array", test_write_array) == NULL ||
		CU_add_test(suite, "write_object", test_write_object) == NULL ||
		CU_add_test(suite, "write_nesting", test_write_nesting) == NULL) {
		CU_add_test(suite, "write_nesting", test_write_nesting) == NULL ||
		CU_add_test(suite, "write_val", test_write_val) == NULL) {
		CU_cleanup_registry();
		return CU_get_error();
	}