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

N A asteriskteam at digium.com
Wed Nov 23 04:40:47 CST 2022


Attention is currently required from: Joshua Colp, Kevin Harwell.

N A has posted comments on this change. ( https://gerrit.asterisk.org/c/asterisk/+/18012 )

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


Patch Set 9:

(2 comments)

File funcs/func_json.c:

https://gerrit.asterisk.org/c/asterisk/+/18012/comment/cd91341b_05fab16a 
PS4, Line 90: static int parse_node(char **key, char *currentkey, char *nestchar, int count, struct ast_json *json, char *buf, size_t len)
            : {
            : 	const char *result = NULL;
            : 	char *previouskey;
            : 	struct ast_json *jsonval = json;
            : 
            : 	snprintf(buf, len, "%s", ""); /* clear the buffer from previous round if necessary */
            : 	if (!json) { /* no error or warning should be thrown */
            : 		ast_debug(1, "Could not find key '%s' in parsed JSON\n", currentkey);
            : 		return -1;
            : 	}
            : 
            : 	switch(ast_json_typeof(jsonval)) {
            : 		unsigned long int size;
            : 		int r;
            : 		char *result2;
            : 
            : 		case AST_JSON_STRING:
            : 			ast_debug(1, "Got JSON string\n");
            : 			result = ast_json_string_get(jsonval);
            : 			snprintf(buf, len, "%s", result);
            : 			break;
            : 		case AST_JSON_INTEGER:
            : 			ast_debug(1, "Got JSON integer\n");
            : 			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:
            : 			ast_debug(1, "Got JSON array\n");
            : 			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, key: %s\n", size, currentkey);
            : 			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);
            : 					return 0;
            : 				} else {
            : 					char *jsonarray;
            : 					ast_debug(1, "No key on which to index in the array, so dumping '%s' array\n", previouskey);
            : 					jsonarray = ast_json_dump_string(jsonval);
            : 					ast_copy_string(buf, jsonarray, len);
            : 					ast_json_free(jsonarray);
            : 				}
            : 			} 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 {
            : 				struct ast_json *json2 = ast_json_array_get(jsonval, r);
            : 				if (!json2) {
            : 					ast_debug(1, "Array index %d contains empty item\n", r);
            : 					return -1;
            : 				}
            : 				previouskey = currentkey;
            : 				currentkey = strsep(key, nestchar); /* get the next subkey */
            : 				ast_debug(1, "Recursing on index %d in array (key was '%s' and is now '%s')\n", r, previouskey, currentkey);
            : 				/* json2 is a borrowed ref. That's fine, since json won't get freed until recursing is over */
            : 				/* If there are keys remaining, then parse the next object we can get. Otherwise, just dump the child */
            : 				if (parse_node(key, currentkey, nestchar, count, currentkey ? ast_json_object_get(json2, currentkey) : json2, buf, len)) { /* recurse on this node */
            : 					return -1;
            : 				}
            : 			}
            : 			break;
            : 		default:
            : 			ast_debug(1, "Got generic JSON object\n");
            : 			result2 = ast_json_dump_string(jsonval);
            : 			snprintf(buf, len, "%s", result2);
            : 			ast_json_free(result2);
            : 	}
            : 	return 0;
            : }
> So I have been mucking around with this and it seems there is a difference between the previous way  […]
Ack


https://gerrit.asterisk.org/c/asterisk/+/18012/comment/f449731b_cc47946b 
PS4, Line 235: 		json = ast_json_load_str(str, NULL);
> There is no reason you should have to load the json multiple times. […]
Done



-- 
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: 9
Gerrit-Owner: N A <asterisk at phreaknet.org>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Kevin Harwell <default.enum at gmail.com>
Gerrit-CC: Joshua Colp <jcolp at sangoma.com>
Gerrit-Attention: Joshua Colp <jcolp at sangoma.com>
Gerrit-Attention: Kevin Harwell <default.enum at gmail.com>
Gerrit-Comment-Date: Wed, 23 Nov 2022 10:40:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: N A <asterisk at phreaknet.org>
Comment-In-Reply-To: Joshua Colp <jcolp at sangoma.com>
Comment-In-Reply-To: Kevin Harwell <default.enum at gmail.com>
Gerrit-MessageType: comment
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20221123/ee563423/attachment-0001.html>


More information about the asterisk-code-review mailing list