[Asterisk-code-review] func_json: Enhance parsing capabilities of JSON_DECODE (asterisk[master])

N A asteriskteam at digium.com
Sat Feb 12 16:03:25 CST 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18012 )


Change subject: func_json: Enhance parsing capabilities of JSON_DECODE
......................................................................

func_json: Enhance parsing capabilities of JSON_DECODE

Adds support for arrays to JSON_DECODE by allowing the
user to print out entire arrays or index a particular
key or print the number of keys in a JSON array.

Additionally, adds support for recursively iterating a
JSON tree in a single function call, making it easier
to parse JSON results with multiple levels.

Also fixes a bug with the unit tests causing an empty
string to be printed instead of the actual test result.

ASTERISK-29913 #close

Change-Id: I603940b216a3911b498fc6583b18934011ef5d5b
---
A doc/CHANGES-staging/func_json_additions.txt
M funcs/func_json.c
2 files changed, 157 insertions(+), 42 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/12/18012/1

diff --git a/doc/CHANGES-staging/func_json_additions.txt b/doc/CHANGES-staging/func_json_additions.txt
new file mode 100644
index 0000000..963f0b1
--- /dev/null
+++ b/doc/CHANGES-staging/func_json_additions.txt
@@ -0,0 +1,5 @@
+Subject: func_json
+
+Additional parsing capabilities have been added to the
+JSON_DECODE function, including support for arrays
+and recursive indexing.
diff --git a/funcs/func_json.c b/funcs/func_json.c
index a48b85c..fd7ba69 100644
--- a/funcs/func_json.c
+++ b/funcs/func_json.c
@@ -49,6 +49,21 @@
 			</parameter>
 			<parameter name="item" required="true">
 				<para>The name of the key whose value to return.</para>
+				<para>Multiple keys can be listed separated by a hierarchy delimeter, which will recursively index into a nested JSON string to retrieve a specific subkey's value.</para>
+			</parameter>
+			<parameter name="separator" required="false">
+				<para>A single character that delimits a key hierarchy for nested indexing. Default is a period (.)</para>
+				<para>This value should not appear in the key or hierarchy of keys itself, except to delimit the hierarchy of keys.</para>
+			</parameter>
+			<parameter name="options" required="no">
+				<optionlist>
+					<option name="c">
+						<para>For keys that reference a JSON array, return
+						the number of items in the array.</para>
+						<para>This option has no effect on any other type
+						of value.</para>
+					</option>
+				</optionlist>
 			</parameter>
 		</syntax>
 		<description>
@@ -64,19 +79,40 @@
 
 AST_THREADSTORAGE(result_buf);
 
+enum say_option_flags {
+	OPT_COUNT = (1 << 0),
+};
+
+AST_APP_OPTIONS(json_options, {
+	AST_APP_OPTION('c', OPT_COUNT),
+});
+
 static int json_decode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
+	int count = 0;
+	struct ast_flags flags = {0};
 	struct ast_json *json, *jsonval;
+	char *nestchar = "."; /* default delimeter for nesting key indexing is . */
+
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(varname);
 		AST_APP_ARG(key);
+		AST_APP_ARG(nestchar);
+		AST_APP_ARG(options);
 	);
-	char *varsubst, *result2;
+	char *varsubst, *result2, *key, *currentkey, *previouskey;
 	const char *result = NULL;
 	struct ast_str *str = ast_str_thread_get(&result_buf, 16);
 
 	AST_STANDARD_APP_ARGS(args, data);
 
+	if (!ast_strlen_zero(args.options)) {
+		ast_app_parse_options(json_options, &flags, NULL, args.options);
+		if (ast_test_flag(&flags, OPT_COUNT)) {
+			count = 1;
+		}
+	}
+
 	if (ast_strlen_zero(args.varname)) {
 		ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);
 		return -1;
@@ -85,6 +121,15 @@
 		ast_log(LOG_WARNING, "%s requires a key\n", cmd);
 		return -1;
 	}
+	key = args.key;
+	if (!ast_strlen_zero(args.nestchar)) {
+		int seplen = strlen(args.nestchar);
+		if (seplen != 1) {
+			ast_log(LOG_WARNING, "Nesting separator '%s' has length %d and is invalid (must be a single character)\n", args.nestchar, seplen);
+		} else {
+			nestchar = args.nestchar;
+		}
+	}
 
 	varsubst = ast_alloca(strlen(args.varname) + 4); /* +4 for ${} and null terminator */
 	if (!varsubst) {
@@ -93,43 +138,93 @@
 	}
 	sprintf(varsubst, "${%s}", args.varname); /* safe, because of the above allocation */
 	ast_str_substitute_variables(&str, 0, chan, varsubst);
-	if (ast_str_strlen(str) == 0) {
-		ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname);
-		return -1; /* empty json string */
-	}
 
-	ast_debug(1, "Parsing JSON: %s\n", ast_str_buffer(str));
+	ast_debug(1, "Parsing JSON using nesting delimeter '%s'\n", nestchar);
 
-	json = ast_json_load_str(str, NULL);
+	/* allow for multiple key nesting */
+	while ((currentkey = strsep(&key, nestchar))) {
+		if (ast_str_strlen(str) == 0) {
+			ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname);
+			return -1; /* empty json string */
+		}
+		ast_debug(1, "Parsing JSON: %s\n", ast_str_buffer(str));
 
-	if (!json) {
-		ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str));
-		return -1;
-	}
+		json = ast_json_load_str(str, NULL);
 
-	jsonval = ast_json_object_get(json, args.key);
-	if (!jsonval) { /* no error or warning should be thrown */
-		ast_debug(1, "Could not find key '%s' in parsed JSON\n", args.key);
+		if (!json) {
+			ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str));
+			return -1;
+		}
+
+		jsonval = ast_json_object_get(json, currentkey);
+		if (!jsonval) { /* no error or warning should be thrown */
+			ast_debug(1, "Could not find key '%s' in parsed JSON\n", currentkey);
+			ast_json_free(json);
+			return -1;
+		}
+		snprintf(buf, len, "%s", ""); /* clear the buffer from previous round if necessary */
+		switch(ast_json_typeof(jsonval)) {
+			char *buf2;
+			unsigned long int size;
+			int r, buflen2;
+			case AST_JSON_STRING:
+				result = ast_json_string_get(jsonval);
+				snprintf(buf, len, "%s", result);
+				break;
+			case AST_JSON_INTEGER:
+				r = ast_json_integer_get(jsonval);
+				snprintf(buf, len, "%d", r); /* the snprintf below is mutually exclusive with this one */
+				break;
+			case AST_JSON_ARRAY:
+				previouskey = currentkey;
+				currentkey = strsep(&key, nestchar); /* retrieve the desired index */
+				size = ast_json_array_size(jsonval);
+				ast_debug(1, "Parsed JSON array of size %lu\n", size);
+				if (!currentkey) { /* this is the end, so just dump the array */
+					if (count) {
+						ast_debug(1, "No key on which to index in the array, so returning count: %lu\n", size);
+						snprintf(buf, len, "%lu", size);
+					} else {
+						ast_debug(1, "No key on which to index in the array, so dumping '%s' array\n", previouskey);
+						buf2 = buf;
+						buflen2 = len;
+						snprintf(buf2, buflen2, "%s", "[");
+						buf2++;
+						buflen2--;
+						for (r = 0; r < size; r++) {
+							int rlen;
+							snprintf(buf2, buflen2, "\"%s\",", ast_json_string_get(ast_json_array_get(jsonval, r)));
+							rlen = strlen(ast_json_string_get(ast_json_array_get(jsonval, r)));
+							buf2 += rlen + 3; /* add 3 for the surrounding quotes and trailing comma */
+							buflen2 -= (buflen2 + 3);
+						}
+						/* no trailing comma after last item */
+						buf2--;
+						buflen2++;
+						snprintf(buf2, buflen2, "%s", "]");
+					}
+					break;
+				} else if (ast_str_to_int(currentkey, &r) || r < 0) {
+					ast_debug(1, "Requested index '%s' is not numeric or is invalid\n", currentkey);
+				} else if (r >= size) {
+					ast_debug(1, "Requested index '%d' does not exist in parsed array\n", r);
+				} else {
+					result = ast_json_string_get(ast_json_array_get(jsonval, r));
+					if (result) {
+						ast_debug(1, "Index %d in array contains '%s'\n", r, result);
+						snprintf(buf, len, "%s", result);
+					}
+				}
+				break;
+			default:
+				result2 = ast_json_dump_string(jsonval);
+				snprintf(buf, len, "%s", result2);
+				ast_json_free(result2);
+				break;
+		}
 		ast_json_free(json);
-		return -1;
+		ast_str_set(&str, 0, "%s", buf); /* recurse on this node if necessary */
 	}
-	switch(ast_json_typeof(jsonval)) {
-		int r;
-		case AST_JSON_STRING:
-			result = ast_json_string_get(jsonval);
-			snprintf(buf, len, "%s", result);
-			break;
-		case AST_JSON_INTEGER:
-			r = ast_json_integer_get(jsonval);
-			snprintf(buf, len, "%d", r); /* the snprintf below is mutually exclusive with this one */
-			break;
-		default:
-			result2 = ast_json_dump_string(jsonval);
-			snprintf(buf, len, "%s", result2);
-			ast_json_free(result2);
-			break;
-	}
-	ast_json_free(json);
 
 	return 0;
 }
@@ -146,12 +241,23 @@
 	struct ast_channel *chan; /* dummy channel */
 	struct ast_str *str; /* fancy string for holding comparing value */
 
-	const char *test_strings[][5] = {
-		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "city", "Anytown"},
-		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "state", "USA"},
-		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "blah", ""},
-		{"{\"key1\": \"123\", \"key2\": \"456\"}", "key1", "123"},
-		{"{\"key1\": 123, \"key2\": 456}", "key1", "123"},
+	const char *test_strings[][6] = {
+		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "city", "Anytown"},
+		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "state", "USA"},
+		{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "blah", ""},
+		{"{\"key1\": \"123\", \"key2\": \"456\"}", "", "key1", "123"},
+		{"{\"key1\": 123, \"key2\": 456}", "", "key1", "123"},
+		{"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "/", "path/to/elem", "someVar"},
+		{"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "", "path.to.elem2", ""},
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ",c", "path.to.arr", "2"}, /* test count */
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr", "[\"item0\",\"item1\"]"},
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ".", "path.to.arr.1", "item1"},
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr", "[\"item0\",\"item1\"]"},
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/1", "item1"},
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/2", ""}, /* nonexistent index */
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/-1", ""}, /* bogus index */
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/test", ""}, /* bogus index */
+		{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr.test.test2.subkey", ""}, /* bogus index */
 	};
 
 	switch (cmd) {
@@ -177,7 +283,7 @@
 	}
 
 	for (i = 0; i < ARRAY_LEN(test_strings); i++) {
-		char tmp[512], tmp2[512] = "";
+		char tmp[512];
 
 		struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]);
 		if (!var) {
@@ -189,11 +295,11 @@
 
 		AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);
 
-		snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s)}", "test_string", test_strings[i][1]);
+		snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s,%s)}", "test_string", test_strings[i][2], test_strings[i][1]);
 
 		ast_str_substitute_variables(&str, 0, chan, tmp);
-		if (strcmp(test_strings[i][2], ast_str_buffer(str))) {
-			ast_test_status_update(test, "Format string '%s' substituted to '%s'.  Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][2]);
+		if (strcmp(test_strings[i][3], ast_str_buffer(str))) {
+			ast_test_status_update(test, "Format string '%s' substituted to '%s'.  Expected '%s'.\n", test_strings[i][0], ast_str_buffer(str), test_strings[i][3]);
 			res = AST_TEST_FAIL;
 		}
 	}
@@ -209,7 +315,9 @@
 {
 	int res;
 
+#ifdef TEST_FRAMEWORK
 	AST_TEST_UNREGISTER(test_JSON_DECODE);
+#endif
 	res = ast_custom_function_unregister(&json_decode_function);
 
 	return res;
@@ -219,7 +327,9 @@
 {
 	int res;
 
+#ifdef TEST_FRAMEWORK
 	AST_TEST_REGISTER(test_JSON_DECODE);
+#endif
 	res = ast_custom_function_register(&json_decode_function);
 
 	return res;

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18012
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I603940b216a3911b498fc6583b18934011ef5d5b
Gerrit-Change-Number: 18012
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220212/6da49818/attachment-0001.html>


More information about the asterisk-code-review mailing list