|
| 1 | +/* |
| 2 | + * Asterisk -- An open source telephony toolkit. |
| 3 | + * |
| 4 | + * Copyright (C) 2021, Naveen Albert |
| 5 | + * |
| 6 | + * See http://www.asterisk.org for more information about |
| 7 | + * the Asterisk project. Please do not directly contact |
| 8 | + * any of the maintainers of this project for assistance; |
| 9 | + * the project provides a web site, mailing lists and IRC |
| 10 | + * channels for your use. |
| 11 | + * |
| 12 | + * This program is free software, distributed under the terms of |
| 13 | + * the GNU General Public License Version 2. See the LICENSE file |
| 14 | + * at the top of the source tree. |
| 15 | + */ |
| 16 | + |
| 17 | +/*! \file |
| 18 | + * |
| 19 | + * \brief JSON parsing function |
| 20 | + * |
| 21 | + * \author Naveen Albert <asterisk@phreaknet.org> |
| 22 | + * |
| 23 | + * \ingroup functions |
| 24 | + */ |
| 25 | + |
| 26 | +/*** MODULEINFO |
| 27 | + <support_level>extended</support_level> |
| 28 | + ***/ |
| 29 | + |
| 30 | +#include "asterisk.h" |
| 31 | + |
| 32 | +#include "asterisk/module.h" |
| 33 | +#include "asterisk/channel.h" |
| 34 | +#include "asterisk/pbx.h" |
| 35 | +#include "asterisk/utils.h" |
| 36 | +#include "asterisk/test.h" |
| 37 | +#include "asterisk/app.h" |
| 38 | +#include "asterisk/conversions.h" |
| 39 | + |
| 40 | +/*** DOCUMENTATION |
| 41 | + <function name="JSON_DECODE" language="en_US"> |
| 42 | + <synopsis> |
| 43 | + Returns the string value of a JSON object key from a string containing a |
| 44 | + JSON array. |
| 45 | + </synopsis> |
| 46 | + <syntax> |
| 47 | + <parameter name="varname" required="true"> |
| 48 | + <para>The name of the variable containing the JSON string to parse.</para> |
| 49 | + </parameter> |
| 50 | + <parameter name="item" required="true"> |
| 51 | + <para>The name of the key whose value to return.</para> |
| 52 | + </parameter> |
| 53 | + </syntax> |
| 54 | + <description> |
| 55 | + <para>The JSON_DECODE function retrieves the value of the given variable name |
| 56 | + and parses it as JSON, returning the value at a specified key. If the key cannot |
| 57 | + be found, an empty string is returned.</para> |
| 58 | + </description> |
| 59 | + <see-also> |
| 60 | + <ref type="function">CURL</ref> |
| 61 | + </see-also> |
| 62 | + </function> |
| 63 | + ***/ |
| 64 | + |
| 65 | +AST_THREADSTORAGE(result_buf); |
| 66 | + |
| 67 | +static int json_decode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 68 | +{ |
| 69 | + struct ast_json *json, *jsonval; |
| 70 | + AST_DECLARE_APP_ARGS(args, |
| 71 | + AST_APP_ARG(varname); |
| 72 | + AST_APP_ARG(key); |
| 73 | + ); |
| 74 | + char *varsubst, *result2; |
| 75 | + const char *result = NULL; |
| 76 | + struct ast_str *str = ast_str_thread_get(&result_buf, 16); |
| 77 | + |
| 78 | + AST_STANDARD_APP_ARGS(args, data); |
| 79 | + |
| 80 | + if (ast_strlen_zero(args.varname)) { |
| 81 | + ast_log(LOG_WARNING, "%s requires a variable name\n", cmd); |
| 82 | + return -1; |
| 83 | + } |
| 84 | + if (ast_strlen_zero(args.key)) { |
| 85 | + ast_log(LOG_WARNING, "%s requires a key\n", cmd); |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + varsubst = ast_alloca(strlen(args.varname) + 4); /* +4 for ${} and null terminator */ |
| 90 | + if (!varsubst) { |
| 91 | + ast_log(LOG_ERROR, "Failed to allocate string\n"); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + sprintf(varsubst, "${%s}", args.varname); /* safe, because of the above allocation */ |
| 95 | + ast_str_substitute_variables(&str, 0, chan, varsubst); |
| 96 | + if (ast_str_strlen(str) == 0) { |
| 97 | + ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname); |
| 98 | + return -1; /* empty json string */ |
| 99 | + } |
| 100 | + |
| 101 | + ast_debug(1, "Parsing JSON: %s\n", ast_str_buffer(str)); |
| 102 | + |
| 103 | + json = ast_json_load_str(str, NULL); |
| 104 | + |
| 105 | + if (!json) { |
| 106 | + ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str)); |
| 107 | + return -1; |
| 108 | + } |
| 109 | + |
| 110 | + jsonval = ast_json_object_get(json, args.key); |
| 111 | + if (!jsonval) { /* no error or warning should be thrown */ |
| 112 | + ast_debug(1, "Could not find key '%s' in parsed JSON\n", args.key); |
| 113 | + ast_json_free(json); |
| 114 | + return -1; |
| 115 | + } |
| 116 | + switch(ast_json_typeof(jsonval)) { |
| 117 | + int r; |
| 118 | + case AST_JSON_STRING: |
| 119 | + result = ast_json_string_get(jsonval); |
| 120 | + snprintf(buf, len, "%s", result); |
| 121 | + break; |
| 122 | + case AST_JSON_INTEGER: |
| 123 | + r = ast_json_integer_get(jsonval); |
| 124 | + snprintf(buf, len, "%d", r); /* the snprintf below is mutually exclusive with this one */ |
| 125 | + break; |
| 126 | + default: |
| 127 | + result2 = ast_json_dump_string(jsonval); |
| 128 | + snprintf(buf, len, "%s", result2); |
| 129 | + ast_json_free(result2); |
| 130 | + break; |
| 131 | + } |
| 132 | + ast_json_free(json); |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +static struct ast_custom_function json_decode_function = { |
| 138 | + .name = "JSON_DECODE", |
| 139 | + .read = json_decode_read, |
| 140 | +}; |
| 141 | + |
| 142 | +#ifdef TEST_FRAMEWORK |
| 143 | +AST_TEST_DEFINE(test_JSON_DECODE) |
| 144 | +{ |
| 145 | + int i, res = AST_TEST_PASS; |
| 146 | + struct ast_channel *chan; /* dummy channel */ |
| 147 | + struct ast_str *str; /* fancy string for holding comparing value */ |
| 148 | + |
| 149 | + const char *test_strings[][5] = { |
| 150 | + {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "city", "Anytown"}, |
| 151 | + {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "state", "USA"}, |
| 152 | + {"{\"city\": \"Anytown\", \"state\": \"USA\"}", "blah", ""}, |
| 153 | + {"{\"key1\": \"123\", \"key2\": \"456\"}", "key1", "123"}, |
| 154 | + {"{\"key1\": 123, \"key2\": 456}", "key1", "123"}, |
| 155 | + }; |
| 156 | + |
| 157 | + switch (cmd) { |
| 158 | + case TEST_INIT: |
| 159 | + info->name = "func_JSON_DECODE"; |
| 160 | + info->category = "/funcs/func_json/"; |
| 161 | + info->summary = "Test JSON_DECODE function"; |
| 162 | + info->description = "Verify JSON_DECODE behavior"; |
| 163 | + return AST_TEST_NOT_RUN; |
| 164 | + case TEST_EXECUTE: |
| 165 | + break; |
| 166 | + } |
| 167 | + |
| 168 | + if (!(chan = ast_dummy_channel_alloc())) { |
| 169 | + ast_test_status_update(test, "Unable to allocate dummy channel\n"); |
| 170 | + return AST_TEST_FAIL; |
| 171 | + } |
| 172 | + |
| 173 | + if (!(str = ast_str_create(64))) { |
| 174 | + ast_test_status_update(test, "Unable to allocate dynamic string buffer\n"); |
| 175 | + ast_channel_release(chan); |
| 176 | + return AST_TEST_FAIL; |
| 177 | + } |
| 178 | + |
| 179 | + for (i = 0; i < ARRAY_LEN(test_strings); i++) { |
| 180 | + char tmp[512], tmp2[512] = ""; |
| 181 | + |
| 182 | + struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]); |
| 183 | + if (!var) { |
| 184 | + ast_test_status_update(test, "Unable to allocate variable\n"); |
| 185 | + ast_free(str); |
| 186 | + ast_channel_release(chan); |
| 187 | + return AST_TEST_FAIL; |
| 188 | + } |
| 189 | + |
| 190 | + AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries); |
| 191 | + |
| 192 | + snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s)}", "test_string", test_strings[i][1]); |
| 193 | + |
| 194 | + ast_str_substitute_variables(&str, 0, chan, tmp); |
| 195 | + if (strcmp(test_strings[i][2], ast_str_buffer(str))) { |
| 196 | + ast_test_status_update(test, "Format string '%s' substituted to '%s'. Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][2]); |
| 197 | + res = AST_TEST_FAIL; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + ast_free(str); |
| 202 | + ast_channel_release(chan); |
| 203 | + |
| 204 | + return res; |
| 205 | +} |
| 206 | +#endif |
| 207 | + |
| 208 | +static int unload_module(void) |
| 209 | +{ |
| 210 | + int res; |
| 211 | + |
| 212 | + AST_TEST_UNREGISTER(test_JSON_DECODE); |
| 213 | + res = ast_custom_function_unregister(&json_decode_function); |
| 214 | + |
| 215 | + return res; |
| 216 | +} |
| 217 | + |
| 218 | +static int load_module(void) |
| 219 | +{ |
| 220 | + int res; |
| 221 | + |
| 222 | + AST_TEST_REGISTER(test_JSON_DECODE); |
| 223 | + res = ast_custom_function_register(&json_decode_function); |
| 224 | + |
| 225 | + return res; |
| 226 | +} |
| 227 | + |
| 228 | +AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JSON decoding function"); |
0 commit comments