<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18012">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_json: Enhance parsing capabilities of JSON_DECODE<br><br>Adds support for arrays to JSON_DECODE by allowing the<br>user to print out entire arrays or index a particular<br>key or print the number of keys in a JSON array.<br><br>Additionally, adds support for recursively iterating a<br>JSON tree in a single function call, making it easier<br>to parse JSON results with multiple levels.<br><br>Also fixes a bug with the unit tests causing an empty<br>string to be printed instead of the actual test result.<br><br>ASTERISK-29913 #close<br><br>Change-Id: I603940b216a3911b498fc6583b18934011ef5d5b<br>---<br>A doc/CHANGES-staging/func_json_additions.txt<br>M funcs/func_json.c<br>2 files changed, 157 insertions(+), 42 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/12/18012/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/func_json_additions.txt b/doc/CHANGES-staging/func_json_additions.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..963f0b1</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/func_json_additions.txt</span><br><span>@@ -0,0 +1,5 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: func_json</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Additional parsing capabilities have been added to the</span><br><span style="color: hsl(120, 100%, 40%);">+JSON_DECODE function, including support for arrays</span><br><span style="color: hsl(120, 100%, 40%);">+and recursive indexing.</span><br><span>diff --git a/funcs/func_json.c b/funcs/func_json.c</span><br><span>index a48b85c..fd7ba69 100644</span><br><span>--- a/funcs/func_json.c</span><br><span>+++ b/funcs/func_json.c</span><br><span>@@ -49,6 +49,21 @@</span><br><span> </parameter></span><br><span> <parameter name="item" required="true"></span><br><span> <para>The name of the key whose value to return.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <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></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="separator" required="false"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>A single character that delimits a key hierarchy for nested indexing. Default is a period (.)</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>This value should not appear in the key or hierarchy of keys itself, except to delimit the hierarchy of keys.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="options" required="no"></span><br><span style="color: hsl(120, 100%, 40%);">+ <optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="c"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>For keys that reference a JSON array, return</span><br><span style="color: hsl(120, 100%, 40%);">+ the number of items in the array.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>This option has no effect on any other type</span><br><span style="color: hsl(120, 100%, 40%);">+ of value.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ </optionlist></span><br><span> </parameter></span><br><span> </syntax></span><br><span> <description></span><br><span>@@ -64,19 +79,40 @@</span><br><span> </span><br><span> AST_THREADSTORAGE(result_buf);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+enum say_option_flags {</span><br><span style="color: hsl(120, 100%, 40%);">+ OPT_COUNT = (1 << 0),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AST_APP_OPTIONS(json_options, {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_OPTION('c', OPT_COUNT),</span><br><span style="color: hsl(120, 100%, 40%);">+});</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int json_decode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ int count = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags flags = {0};</span><br><span> struct ast_json *json, *jsonval;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *nestchar = "."; /* default delimeter for nesting key indexing is . */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> AST_DECLARE_APP_ARGS(args,</span><br><span> AST_APP_ARG(varname);</span><br><span> AST_APP_ARG(key);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(nestchar);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(options);</span><br><span> );</span><br><span style="color: hsl(0, 100%, 40%);">- char *varsubst, *result2;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *varsubst, *result2, *key, *currentkey, *previouskey;</span><br><span> const char *result = NULL;</span><br><span> struct ast_str *str = ast_str_thread_get(&result_buf, 16);</span><br><span> </span><br><span> AST_STANDARD_APP_ARGS(args, data);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.options)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_app_parse_options(json_options, &flags, NULL, args.options);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(&flags, OPT_COUNT)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ count = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (ast_strlen_zero(args.varname)) {</span><br><span> ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);</span><br><span> return -1;</span><br><span>@@ -85,6 +121,15 @@</span><br><span> ast_log(LOG_WARNING, "%s requires a key\n", cmd);</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ key = args.key;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.nestchar)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int seplen = strlen(args.nestchar);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (seplen != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Nesting separator '%s' has length %d and is invalid (must be a single character)\n", args.nestchar, seplen);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ nestchar = args.nestchar;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> varsubst = ast_alloca(strlen(args.varname) + 4); /* +4 for ${} and null terminator */</span><br><span> if (!varsubst) {</span><br><span>@@ -93,43 +138,93 @@</span><br><span> }</span><br><span> sprintf(varsubst, "${%s}", args.varname); /* safe, because of the above allocation */</span><br><span> ast_str_substitute_variables(&str, 0, chan, varsubst);</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_str_strlen(str) == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1; /* empty json string */</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Parsing JSON: %s\n", ast_str_buffer(str));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Parsing JSON using nesting delimeter '%s'\n", nestchar);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- json = ast_json_load_str(str, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* allow for multiple key nesting */</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((currentkey = strsep(&key, nestchar))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_str_strlen(str) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1; /* empty json string */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Parsing JSON: %s\n", ast_str_buffer(str));</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!json) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str));</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ json = ast_json_load_str(str, NULL);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- jsonval = ast_json_object_get(json, args.key);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!jsonval) { /* no error or warning should be thrown */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "Could not find key '%s' in parsed JSON\n", args.key);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!json) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str));</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ jsonval = ast_json_object_get(json, currentkey);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!jsonval) { /* no error or warning should be thrown */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Could not find key '%s' in parsed JSON\n", currentkey);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_free(json);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%s", ""); /* clear the buffer from previous round if necessary */</span><br><span style="color: hsl(120, 100%, 40%);">+ switch(ast_json_typeof(jsonval)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char *buf2;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned long int size;</span><br><span style="color: hsl(120, 100%, 40%);">+ int r, buflen2;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_JSON_STRING:</span><br><span style="color: hsl(120, 100%, 40%);">+ result = ast_json_string_get(jsonval);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%s", result);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_JSON_INTEGER:</span><br><span style="color: hsl(120, 100%, 40%);">+ r = ast_json_integer_get(jsonval);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%d", r); /* the snprintf below is mutually exclusive with this one */</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case AST_JSON_ARRAY:</span><br><span style="color: hsl(120, 100%, 40%);">+ previouskey = currentkey;</span><br><span style="color: hsl(120, 100%, 40%);">+ currentkey = strsep(&key, nestchar); /* retrieve the desired index */</span><br><span style="color: hsl(120, 100%, 40%);">+ size = ast_json_array_size(jsonval);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Parsed JSON array of size %lu\n", size);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!currentkey) { /* this is the end, so just dump the array */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (count) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "No key on which to index in the array, so returning count: %lu\n", size);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%lu", size);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "No key on which to index in the array, so dumping '%s' array\n", previouskey);</span><br><span style="color: hsl(120, 100%, 40%);">+ buf2 = buf;</span><br><span style="color: hsl(120, 100%, 40%);">+ buflen2 = len;</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf2, buflen2, "%s", "[");</span><br><span style="color: hsl(120, 100%, 40%);">+ buf2++;</span><br><span style="color: hsl(120, 100%, 40%);">+ buflen2--;</span><br><span style="color: hsl(120, 100%, 40%);">+ for (r = 0; r < size; r++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int rlen;</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf2, buflen2, "\"%s\",", ast_json_string_get(ast_json_array_get(jsonval, r)));</span><br><span style="color: hsl(120, 100%, 40%);">+ rlen = strlen(ast_json_string_get(ast_json_array_get(jsonval, r)));</span><br><span style="color: hsl(120, 100%, 40%);">+ buf2 += rlen + 3; /* add 3 for the surrounding quotes and trailing comma */</span><br><span style="color: hsl(120, 100%, 40%);">+ buflen2 -= (buflen2 + 3);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* no trailing comma after last item */</span><br><span style="color: hsl(120, 100%, 40%);">+ buf2--;</span><br><span style="color: hsl(120, 100%, 40%);">+ buflen2++;</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf2, buflen2, "%s", "]");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (ast_str_to_int(currentkey, &r) || r < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Requested index '%s' is not numeric or is invalid\n", currentkey);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (r >= size) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Requested index '%d' does not exist in parsed array\n", r);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ result = ast_json_string_get(ast_json_array_get(jsonval, r));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (result) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Index %d in array contains '%s'\n", r, result);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%s", result);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ default:</span><br><span style="color: hsl(120, 100%, 40%);">+ result2 = ast_json_dump_string(jsonval);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%s", result2);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_free(result2);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> ast_json_free(json);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_set(&str, 0, "%s", buf); /* recurse on this node if necessary */</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- switch(ast_json_typeof(jsonval)) {</span><br><span style="color: hsl(0, 100%, 40%);">- int r;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_JSON_STRING:</span><br><span style="color: hsl(0, 100%, 40%);">- result = ast_json_string_get(jsonval);</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(buf, len, "%s", result);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- case AST_JSON_INTEGER:</span><br><span style="color: hsl(0, 100%, 40%);">- r = ast_json_integer_get(jsonval);</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(buf, len, "%d", r); /* the snprintf below is mutually exclusive with this one */</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- default:</span><br><span style="color: hsl(0, 100%, 40%);">- result2 = ast_json_dump_string(jsonval);</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(buf, len, "%s", result2);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_json_free(result2);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_json_free(json);</span><br><span> </span><br><span> return 0;</span><br><span> }</span><br><span>@@ -146,12 +241,23 @@</span><br><span> struct ast_channel *chan; /* dummy channel */</span><br><span> struct ast_str *str; /* fancy string for holding comparing value */</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- const char *test_strings[][5] = {</span><br><span style="color: hsl(0, 100%, 40%);">- {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "city", "Anytown"},</span><br><span style="color: hsl(0, 100%, 40%);">- {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "state", "USA"},</span><br><span style="color: hsl(0, 100%, 40%);">- {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "blah", ""},</span><br><span style="color: hsl(0, 100%, 40%);">- {"{\"key1\": \"123\", \"key2\": \"456\"}", "key1", "123"},</span><br><span style="color: hsl(0, 100%, 40%);">- {"{\"key1\": 123, \"key2\": 456}", "key1", "123"},</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *test_strings[][6] = {</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "city", "Anytown"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "state", "USA"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "blah", ""},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{\"key1\": \"123\", \"key2\": \"456\"}", "", "key1", "123"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{\"key1\": 123, \"key2\": 456}", "", "key1", "123"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "/", "path/to/elem", "someVar"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "", "path.to.elem2", ""},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ",c", "path.to.arr", "2"}, /* test count */</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr", "[\"item0\",\"item1\"]"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ".", "path.to.arr.1", "item1"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr", "[\"item0\",\"item1\"]"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/1", "item1"},</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/2", ""}, /* nonexistent index */</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/-1", ""}, /* bogus index */</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/test", ""}, /* bogus index */</span><br><span style="color: hsl(120, 100%, 40%);">+ {"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr.test.test2.subkey", ""}, /* bogus index */</span><br><span> };</span><br><span> </span><br><span> switch (cmd) {</span><br><span>@@ -177,7 +283,7 @@</span><br><span> }</span><br><span> </span><br><span> for (i = 0; i < ARRAY_LEN(test_strings); i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- char tmp[512], tmp2[512] = "";</span><br><span style="color: hsl(120, 100%, 40%);">+ char tmp[512];</span><br><span> </span><br><span> struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]);</span><br><span> if (!var) {</span><br><span>@@ -189,11 +295,11 @@</span><br><span> </span><br><span> AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s)}", "test_string", test_strings[i][1]);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s,%s)}", "test_string", test_strings[i][2], test_strings[i][1]);</span><br><span> </span><br><span> ast_str_substitute_variables(&str, 0, chan, tmp);</span><br><span style="color: hsl(0, 100%, 40%);">- if (strcmp(test_strings[i][2], ast_str_buffer(str))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_test_status_update(test, "Format string '%s' substituted to '%s'. Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][2]);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp(test_strings[i][3], ast_str_buffer(str))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ 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]);</span><br><span> res = AST_TEST_FAIL;</span><br><span> }</span><br><span> }</span><br><span>@@ -209,7 +315,9 @@</span><br><span> {</span><br><span> int res;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef TEST_FRAMEWORK</span><br><span> AST_TEST_UNREGISTER(test_JSON_DECODE);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> res = ast_custom_function_unregister(&json_decode_function);</span><br><span> </span><br><span> return res;</span><br><span>@@ -219,7 +327,9 @@</span><br><span> {</span><br><span> int res;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef TEST_FRAMEWORK</span><br><span> AST_TEST_REGISTER(test_JSON_DECODE);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> res = ast_custom_function_register(&json_decode_function);</span><br><span> </span><br><span> return res;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18012">change 18012</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18012"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I603940b216a3911b498fc6583b18934011ef5d5b </div>
<div style="display:none"> Gerrit-Change-Number: 18012 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>