| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
AST_THREADSTORAGE(result_buf); |
| 86 |
|
| 87 |
enum json_option_flags { |
| 88 |
OPT_COUNT = (1 << 0), |
| 89 |
}; |
| 90 |
|
| 91 |
AST_APP_OPTIONS(json_options, { |
| 92 |
AST_APP_OPTION('c', OPT_COUNT), |
| 93 |
}); |
| 94 |
|
| 95 |
#define MAX_JSON_STACK 32 |
| 96 |
|
| 97 |
static int parse_node(char **key, char *currentkey, char *nestchar, int count, struct ast_json *json, char *buf, size_t len, int *depth) |
| 98 |
{ |
| 99 |
const char *result = NULL; |
| 100 |
char *previouskey; |
| 101 |
struct ast_json *jsonval = json; |
| 102 |
|
| 103 |
|
| 104 |
(*depth)++; |
| 105 |
if (*depth > MAX_JSON_STACK) { |
| 106 |
ast_log(LOG_WARNING, "Max JSON stack (%d) exceeded\n", MAX_JSON_STACK); |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
snprintf(buf, len, "%s", ""); |
| 111 |
if (!json) { |
| 112 |
ast_debug(1, "Could not find key '%s' in parsed JSON\n", currentkey); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
switch(ast_json_typeof(jsonval)) { |
| 117 |
unsigned long int size; |
| 118 |
int r; |
| 119 |
double d; |
| 120 |
|
| 121 |
case AST_JSON_STRING: |
| 122 |
result = ast_json_string_get(jsonval); |
| 123 |
ast_debug(1, "Got JSON string: %s\n", result); |
| 124 |
ast_copy_string(buf, result, len); |
| 125 |
break; |
| 126 |
case AST_JSON_INTEGER: |
| 127 |
r = ast_json_integer_get(jsonval); |
| 128 |
ast_debug(1, "Got JSON integer: %d\n", r); |
| 129 |
snprintf(buf, len, "%d", r); |
| 130 |
break; |
| 131 |
case AST_JSON_REAL: |
| 132 |
d = ast_json_real_get(jsonval); |
| 133 |
ast_debug(1, "Got JSON real: %.17g\n", d); |
| 134 |
snprintf(buf, len, "%.17g", d); |
| 135 |
break; |
| 136 |
case AST_JSON_ARRAY: |
| 137 |
ast_debug(1, "Got JSON array\n"); |
| 138 |
previouskey = currentkey; |
| 139 |
currentkey = strsep(key, nestchar); |
| 140 |
size = ast_json_array_size(jsonval); |
| 141 |
ast_debug(1, "Parsed JSON array of size %lu, key: %s\n", size, currentkey); |
| 142 |
if (!currentkey) { |
| 143 |
if (count) { |
| 144 |
ast_debug(1, "No key on which to index in the array, so returning count: %lu\n", size); |
| 145 |
snprintf(buf, len, "%lu", size); |
| 146 |
return 0; |
| 147 |
} else { |
| 148 |
char *result2 = ast_json_dump_string(jsonval); |
| 149 |
ast_debug(1, "No key on which to index in the array, so dumping '%s' array\n", previouskey); |
| 150 |
ast_copy_string(buf, result2, len); |
| 151 |
ast_json_free(result2); |
| 152 |
} |
| 153 |
} else if (ast_str_to_int(currentkey, &r) || r < 0) { |
| 154 |
ast_debug(1, "Requested index '%s' is not numeric or is invalid\n", currentkey); |
| 155 |
} else if (r >= size) { |
| 156 |
ast_debug(1, "Requested index '%d' does not exist in parsed array\n", r); |
| 157 |
} else { |
| 158 |
ast_debug(1, "Recursing on index %d in array\n", r); |
| 159 |
if (parse_node(key, currentkey, nestchar, count, ast_json_array_get(jsonval, r), buf, len, depth)) { |
| 160 |
return -1; |
| 161 |
} |
| 162 |
} |
| 163 |
break; |
| 164 |
case AST_JSON_TRUE: |
| 165 |
case AST_JSON_FALSE: |
| 166 |
r = ast_json_is_true(jsonval); |
| 167 |
ast_debug(1, "Got JSON %s for key %s\n", r ? "true" : "false", currentkey); |
| 168 |
snprintf(buf, len, "%d", r); |
| 169 |
break; |
| 170 |
case AST_JSON_NULL: |
| 171 |
ast_debug(1, "Got JSON null for key %s\n", currentkey); |
| 172 |
break; |
| 173 |
case AST_JSON_OBJECT: |
| 174 |
ast_debug(1, "Got generic JSON object for key %s\n", currentkey); |
| 175 |
previouskey = currentkey; |
| 176 |
currentkey = strsep(key, nestchar); |
| 177 |
if (!currentkey) { |
| 178 |
char *result2 = ast_json_dump_string(jsonval); |
| 179 |
ast_copy_string(buf, result2, len); |
| 180 |
ast_json_free(result2); |
| 181 |
} else { |
| 182 |
ast_debug(1, "Recursing on object (key was '%s' and is now '%s')\n", previouskey, currentkey); |
| 183 |
if (parse_node(key, currentkey, nestchar, count, ast_json_object_get(jsonval, currentkey), buf, len, depth)) { |
| 184 |
return -1; |
| 185 |
} |
| 186 |
} |
| 187 |
break; |
| 188 |
default: |
| 189 |
ast_log(LOG_WARNING, "Got unsuported type %d\n", ast_json_typeof(jsonval)); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
static int json_decode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 196 |
{ |
| 197 |
int count = 0; |
| 198 |
struct ast_flags flags = {0}; |
| 199 |
struct ast_json *json = NULL, *start = NULL; |
| 200 |
char *nestchar = "."; |
| 201 |
int index, res, depth = 0; |
| 202 |
|
| 203 |
AST_DECLARE_APP_ARGS(args, |
| 204 |
AST_APP_ARG(varname); |
| 205 |
AST_APP_ARG(key); |
| 206 |
AST_APP_ARG(nestchar); |
| 207 |
AST_APP_ARG(options); |
| 208 |
); |
| 209 |
char *varsubst, *key, *currentkey, *nextkey, *firstkey, *tmp; |
| 210 |
struct ast_str *str = ast_str_thread_get(&result_buf, 16); |
| 211 |
|
| 212 |
AST_STANDARD_APP_ARGS(args, data); |
| 213 |
|
| 214 |
if (!ast_strlen_zero(args.options)) { |
| 215 |
ast_app_parse_options(json_options, &flags, NULL, args.options); |
| 216 |
if (ast_test_flag(&flags, OPT_COUNT)) { |
| 217 |
count = 1; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (ast_strlen_zero(args.varname)) { |
| 222 |
ast_log(LOG_WARNING, "%s requires a variable name\n", cmd); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
if (ast_strlen_zero(args.key)) { |
| 227 |
ast_log(LOG_WARNING, "%s requires a key\n", cmd); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
key = ast_strdupa(args.key); |
| 232 |
if (!ast_strlen_zero(args.nestchar)) { |
| 233 |
int seplen = strlen(args.nestchar); |
| 234 |
if (seplen != 1) { |
| 235 |
ast_log(LOG_WARNING, "Nesting separator '%s' has length %d and is invalid (must be a single character)\n", args.nestchar, seplen); |
| 236 |
} else { |
| 237 |
nestchar = args.nestchar; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
varsubst = ast_alloca(strlen(args.varname) + 4); |
| 242 |
if (!varsubst) { |
| 243 |
ast_log(LOG_ERROR, "Failed to allocate string\n"); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
sprintf(varsubst, "${%s}", args.varname); |
| 247 |
ast_str_substitute_variables(&str, 0, chan, varsubst); |
| 248 |
|
| 249 |
ast_debug(1, "Parsing JSON using nesting delimeter '%s'\n", nestchar); |
| 250 |
|
| 251 |
if (ast_str_strlen(str) == 0) { |
| 252 |
ast_debug(1, "Variable '%s' contains no data, nothing to search!\n", args.varname); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
currentkey = key; |
| 258 |
firstkey = ast_strdupa(currentkey); |
| 259 |
tmp = strstr(firstkey, nestchar); |
| 260 |
if (tmp) { |
| 261 |
*tmp = '\0'; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
ast_debug(1, "Parsing JSON: %s (key: '%s')\n", ast_str_buffer(str), currentkey); |
| 266 |
if (ast_strlen_zero(currentkey)) { |
| 267 |
ast_debug(1, "Empty JSON key\n"); |
| 268 |
return -1; |
| 269 |
} |
| 270 |
if (ast_str_strlen(str) == 0) { |
| 271 |
ast_debug(1, "JSON node '%s', contains no data, nothing to search!\n", currentkey); |
| 272 |
return -1; |
| 273 |
} |
| 274 |
|
| 275 |
json = ast_json_load_str(str, NULL); |
| 276 |
if (!json) { |
| 277 |
ast_log(LOG_WARNING, "Failed to parse as JSON: %s\n", ast_str_buffer(str)); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
nextkey = strsep(&key, nestchar); |
| 283 |
if (ast_json_is_object(json)) { |
| 284 |
start = ast_json_object_get(json, firstkey); |
| 285 |
} else { |
| 286 |
if (ast_str_to_int(currentkey, &index)) { |
| 287 |
ast_debug(1, "Requested index '%s' is not numeric or is invalid\n", currentkey); |
| 288 |
return -1; |
| 289 |
} |
| 290 |
start = ast_json_array_get(json, index); |
| 291 |
} |
| 292 |
|
| 293 |
res = parse_node(&key, nextkey, nestchar, count, start, buf, len, &depth); |
| 294 |
ast_json_unref(json); |
| 295 |
return res; |
| 296 |
} |
| 297 |
|
| 298 |
static struct ast_custom_function json_decode_function = { |
| 299 |
.name = "JSON_DECODE", |
| 300 |
.read = json_decode_read, |
| 301 |
}; |
| 302 |
|
| 303 |
#ifdef TEST_FRAMEWORK |
| 304 |
AST_TEST_DEFINE(test_JSON_DECODE) |
| 305 |
{ |
| 306 |
int i, res = AST_TEST_PASS; |
| 307 |
struct ast_channel *chan; |
| 308 |
struct ast_str *str; |
| 309 |
|
| 310 |
const char *test_strings[][6] = { |
| 311 |
{"{\"myboolean\": true, \"state\": \"USA\"}", "", "myboolean", "1"}, |
| 312 |
{"{\"myboolean\": false, \"state\": \"USA\"}", "", "myboolean", "0"}, |
| 313 |
{"{\"myreal\": 1E+2, \"state\": \"USA\"}", "", "myreal", "100"}, |
| 314 |
{"{\"myreal\": 1.23, \"state\": \"USA\"}", "", "myreal", "1.23"}, |
| 315 |
{"{\"myarray\": [[1]], \"state\": \"USA\"}", "", "myarray.0.0", "1"}, |
| 316 |
{"{\"myarray\": [null], \"state\": \"USA\"}", "", "myarray.0", ""}, |
| 317 |
{"{\"myarray\": [0, 1], \"state\": \"USA\"}", "", "myarray", "[0,1]"}, |
| 318 |
{"[0, 1]", "", "", ""}, |
| 319 |
{"[0, 1]", "", "0", "0"}, |
| 320 |
{"[0, 1]", "", "foo", ""}, |
| 321 |
{"{\"mynull\": null, \"state\": \"USA\"}", "", "mynull", ""}, |
| 322 |
{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "city", "Anytown"}, |
| 323 |
{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "state", "USA"}, |
| 324 |
{"{\"city\": \"Anytown\", \"state\": \"USA\"}", "", "blah", ""}, |
| 325 |
{"{\"key1\": \"123\", \"key2\": \"456\"}", "", "key1", "123"}, |
| 326 |
{"{\"key1\": 123, \"key2\": 456}", "", "key1", "123"}, |
| 327 |
{"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "/", "path/to/elem", "someVar"}, |
| 328 |
{"{ \"path\": { \"to\": { \"elem\": \"someVar\" } } }", "", "path.to.elem2", ""}, |
| 329 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/2", ""}, |
| 330 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/-1", ""}, |
| 331 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/test", ""}, |
| 332 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr.test.test2.subkey", ""}, |
| 333 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ",c", "path.to.arr", "2"}, |
| 334 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "", "path.to.arr", "[\"item0\",\"item1\"]"}, |
| 335 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", ".", "path.to.arr.1", "item1"}, |
| 336 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr", "[\"item0\",\"item1\"]"}, |
| 337 |
{"{ \"path\": { \"to\": { \"arr\": [ \"item0\", \"item1\" ] } } }", "/", "path/to/arr/1", "item1"}, |
| 338 |
{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": \"John Smith\", \"phone\": \"123\"}, {\"name\": \"Jane Doe\", \"phone\": \"234\"} ] } } }", ",c", "path.to.arr.0.name", "John Smith"}, |
| 339 |
{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": 1, \"phone\": 123}, {\"name\": 2, \"phone\": 234} ] } } }", ",c", "path.to.arr.0.name", "1"}, |
| 340 |
{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": [ \"item11\", \"item12\" ], \"phone\": [ \"item13\", \"item14\" ]}, {\"name\": [ \"item15\", \"item16\" ], \"phone\": [ \"item17\", \"item18\" ]} ] } } }", ",c", "path.to.arr.0.name.1", "item12"}, |
| 341 |
{"{ \"startId\": \"foobar\", \"abcd\": { \"id\": \"abcd\", \"type\": \"EXT\" }, \"bcde\": { \"id\": \"bcde\", \"type\": \"CONDITION\" }, \"defg\": { \"id\": \"defg\", \"type\": \"EXT\" }, \"efgh\": { \"id\": \"efgh\", \"type\": \"VOICEMAIL\" } }", "", "bcde", "{\"id\":\"bcde\",\"type\":\"CONDITION\"}"}, |
| 342 |
}; |
| 343 |
|
| 344 |
switch (cmd) { |
| 345 |
case TEST_INIT: |
| 346 |
info->name = "func_JSON_DECODE"; |
| 347 |
info->category = "/funcs/func_json/"; |
| 348 |
info->summary = "Test JSON_DECODE function"; |
| 349 |
info->description = "Verify JSON_DECODE behavior"; |
| 350 |
return AST_TEST_NOT_RUN; |
| 351 |
case TEST_EXECUTE: |
| 352 |
break; |
| 353 |
} |
| 354 |
|
| 355 |
if (!(chan = ast_dummy_channel_alloc())) { |
| 356 |
ast_test_status_update(test, "Unable to allocate dummy channel\n"); |
| 357 |
return AST_TEST_FAIL; |
| 358 |
} |
| 359 |
|
| 360 |
if (!(str = ast_str_create(64))) { |
| 361 |
ast_test_status_update(test, "Unable to allocate dynamic string buffer\n"); |
| 362 |
ast_channel_release(chan); |
| 363 |
return AST_TEST_FAIL; |
| 364 |
} |
| 365 |
|
| 366 |
for (i = 0; i < ARRAY_LEN(test_strings); i++) { |
| 367 |
char tmp[512]; |
| 368 |
|
| 369 |
struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]); |
| 370 |
if (!var) { |
| 371 |
ast_test_status_update(test, "Unable to allocate variable\n"); |
| 372 |
ast_free(str); |
| 373 |
ast_channel_release(chan); |
| 374 |
return AST_TEST_FAIL; |
| 375 |
} |
| 376 |
|
| 377 |
AST_LIST_INSERT_HEAD(ast_channel_varshead(chan), var, entries); |
| 378 |
|
| 379 |
snprintf(tmp, sizeof(tmp), "${JSON_DECODE(%s,%s,%s)}", "test_string", test_strings[i][2], test_strings[i][1]); |
| 380 |
|
| 381 |
ast_str_substitute_variables(&str, 0, chan, tmp); |
| 382 |
if (strcmp(test_strings[i][3], ast_str_buffer(str))) { |
| 383 |
ast_test_status_update(test, "Format string '%s' substituted to '%s' (key: %s). Expected '%s'.\n", test_strings[i][0], ast_str_buffer(str), test_strings[i][2], test_strings[i][3]); |
| 384 |
res = AST_TEST_FAIL; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
ast_free(str); |
| 389 |
ast_channel_release(chan); |
| 390 |
|
| 391 |
return res; |
| 392 |
} |
| 393 |
#endif |
| 394 |
|
| 395 |
static int unload_module(void) |
| 396 |
{ |
| 397 |
int res; |
| 398 |
|
| 399 |
#ifdef TEST_FRAMEWORK |
| 400 |
AST_TEST_UNREGISTER(test_JSON_DECODE); |
| 401 |
#endif |
| 402 |
res = ast_custom_function_unregister(&json_decode_function); |
| 403 |
|
| 404 |
return res; |
| 405 |
} |
| 406 |
|
| 407 |
static int load_module(void) |
| 408 |
{ |
| 409 |
int res; |
| 410 |
|
| 411 |
#ifdef TEST_FRAMEWORK |
| 412 |
AST_TEST_REGISTER(test_JSON_DECODE); |
| 413 |
#endif |
| 414 |
res = ast_custom_function_register(&json_decode_function); |
| 415 |
|
| 416 |
return res; |
| 417 |
} |
| 418 |
|
| 419 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "JSON decoding function"); |