[Asterisk-code-review] func_json: Adds JSON_DECODE function (asterisk[master])

N A asteriskteam at digium.com
Mon Oct 25 16:21:32 CDT 2021


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


Change subject: func_json: Adds JSON_DECODE function
......................................................................

func_json: Adds JSON_DECODE function

Adds the JSON_DECODE function for parsing JSON in the
dialplan. JSON parsing already exists in the Asterisk
core and is used for many different things. This
function exposes the basic parsing capability to
the user in the dialplan, for instance, in conjunction
with CURL for using API responses.

Change-Id: Iea60c49a7358dfdc2db60803cdc9a742f808ba2c
---
A doc/CHANGES-staging/func_json.txt
A funcs/func_json.c
2 files changed, 197 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/34/16634/1

diff --git a/doc/CHANGES-staging/func_json.txt b/doc/CHANGES-staging/func_json.txt
new file mode 100644
index 0000000..79bb2da
--- /dev/null
+++ b/doc/CHANGES-staging/func_json.txt
@@ -0,0 +1,5 @@
+Subject: func_json
+
+The JSON_DECODE dialplan function can now be used
+to parse JSON strings, such as in conjunction with
+CURL for using API responses.
diff --git a/funcs/func_json.c b/funcs/func_json.c
new file mode 100644
index 0000000..52090f4
--- /dev/null
+++ b/funcs/func_json.c
@@ -0,0 +1,192 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Naveen Albert
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief JSON parsing function
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/test.h"
+#include "asterisk/app.h"
+#include "asterisk/conversions.h"
+
+/*** DOCUMENTATION
+	<function name="JSON_DECODE" language="en_US">
+		<synopsis>
+			Returns the contents of a JSON array at a specified key.
+		</synopsis>
+		<syntax>
+			<parameter name="varname" required="true" />
+			<parameter name="item" required="true" />
+		</syntax>
+		<description>
+			<para>The JSON_DECODE function parses a JSON string and returns a value by key.
+			If the key cannot be found, an empty string is returned.</para>
+		</description>
+		<see-also>
+			<ref type="function">CURL</ref>
+		</see-also>
+	</function>
+ ***/
+
+AST_THREADSTORAGE(result_buf);
+
+static int json_decode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_free);
+	RAII_VAR(struct ast_json *, jsonresult, NULL, ast_json_free);
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(varname);
+		AST_APP_ARG(key);
+	);
+	char *varsubst, *jsonstring;
+	const char *result;
+	struct ast_str *str = ast_str_thread_get(&result_buf, 16);
+
+	AST_STANDARD_APP_ARGS(args, data);
+
+	varsubst = ast_alloca(strlen(args.varname) + 4);
+	sprintf(varsubst, "${%s}", args.varname);
+	ast_str_substitute_variables(&str, 0, chan, varsubst);
+	if (ast_str_strlen(str) == 0) {
+		ast_debug(1, "String was empty\n");
+		return -1; /* empty json string */
+	}
+	jsonstring = ast_str_buffer(str);
+
+	ast_debug(1, "Parsing JSON: %s\n", jsonstring);
+
+	json = ast_json_load_string(jsonstring, NULL);
+
+	if (!json) {
+		ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", jsonstring);
+		return -1;
+	}
+
+	result = ast_json_object_string_get(json, args.key);
+
+	if (result) {
+		snprintf(buf, len, "%s", result);
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function json_decode_function = {
+	.name = "JSON_DECODE",
+	.read = json_decode_read,
+};
+
+#ifdef TEST_FRAMEWORK
+AST_TEST_DEFINE(test_JSON_DECODE)
+{
+	int i, res = AST_TEST_PASS;
+	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", ""},
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "func_JSON_DECODE";
+		info->category = "/funcs/func_json/";
+		info->summary = "Test JSON_DECODE function";
+		info->description = "Verify JSON_DECODE behavior";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!(chan = ast_dummy_channel_alloc())) {
+		ast_test_status_update(test, "Unable to allocate dummy channel\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (!(str = ast_str_create(64))) {
+		ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
+		ast_channel_release(chan);
+		return AST_TEST_FAIL;
+	}
+
+	for (i = 0; i < ARRAY_LEN(test_strings); i++) {
+		char tmp[512], tmp2[512] = "";
+
+		struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]);
+		if (!var) {
+			ast_test_status_update(test, "Unable to allocate variable\n");
+			ast_free(str);
+			ast_channel_release(chan);
+			return AST_TEST_FAIL;
+		}
+
+		AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);
+
+		snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s)}", "test_string", 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]);
+			res = AST_TEST_FAIL;
+		}
+	}
+
+	ast_free(str);
+	ast_channel_release(chan);
+
+	return res;
+}
+#endif
+
+static int unload_module(void)
+{
+	int res;
+
+	res = AST_TEST_UNREGISTER(test_JSON_DECODE);
+	res |= ast_custom_function_unregister(&json_decode_function);
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res;
+
+	res = AST_TEST_REGISTER(test_JSON_DECODE);
+	res |= ast_custom_function_register(&json_decode_function);
+
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JSON decoding function");

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Iea60c49a7358dfdc2db60803cdc9a742f808ba2c
Gerrit-Change-Number: 16634
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/20211025/e2933a70/attachment-0001.html>


More information about the asterisk-code-review mailing list