[Asterisk-code-review] func_json: Fix JSON parsing issues. (asterisk[20])

Joshua Colp asteriskteam at digium.com
Thu Mar 16 10:42:22 CDT 2023


Joshua Colp has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/19984 )

Change subject: func_json: Fix JSON parsing issues.
......................................................................

func_json: Fix JSON parsing issues.

Fix issue with returning empty instead of dumping
the JSON string when recursing.

Also adds a unit test to capture this fix.

ASTERISK-30441 #close

Change-Id: If0bde9f3fe84f7af485e0838205cc21e0f752a85
---
M funcs/func_json.c
1 file changed, 22 insertions(+), 1 deletion(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
  George Joseph: Looks good to me, approved




diff --git a/funcs/func_json.c b/funcs/func_json.c
index 6672c58..8bfcd4c 100644
--- a/funcs/func_json.c
+++ b/funcs/func_json.c
@@ -174,7 +174,11 @@
 				previouskey = currentkey;
 				currentkey = strsep(key, nestchar); /* retrieve the desired index */
 				ast_debug(1, "Recursing on object (key was '%s' and is now '%s')\n", previouskey, currentkey);
-				if (parse_node(key, currentkey, nestchar, count, ast_json_object_get(jsonval, currentkey), buf, len, depth)) { /* recurse on this node */
+				if (!currentkey) { /* this is the end, so just dump the object */
+					char *result2 = ast_json_dump_string(jsonval);
+					ast_copy_string(buf, result2, len);
+					ast_json_free(result2);
+				} else if (parse_node(key, currentkey, nestchar, count, ast_json_object_get(jsonval, currentkey), buf, len, depth)) { /* recurse on this node */
 					return -1;
 				}
 			}
@@ -305,6 +309,7 @@
 		{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": \"John Smith\", \"phone\": \"123\"}, {\"name\": \"Jane Doe\", \"phone\": \"234\"} ] } } }", ",c", "path.to.arr.0.name", "John Smith"},
 		{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": 1, \"phone\": 123}, {\"name\": 2, \"phone\": 234} ] } } }", ",c", "path.to.arr.0.name", "1"},
 		{"{ \"path\": { \"to\": { \"arr\": [ {\"name\": [ \"item11\", \"item12\" ], \"phone\": [ \"item13\", \"item14\" ]}, {\"name\": [ \"item15\", \"item16\" ], \"phone\": [ \"item17\", \"item18\" ]} ] } } }", ",c", "path.to.arr.0.name.1", "item12"},
+		{"{ \"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\"}"},
 	};
 
 	switch (cmd) {

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

Gerrit-Project: asterisk
Gerrit-Branch: 20
Gerrit-Change-Id: If0bde9f3fe84f7af485e0838205cc21e0f752a85
Gerrit-Change-Number: 19984
Gerrit-PatchSet: 2
Gerrit-Owner: N A <asterisk at phreaknet.org>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20230316/39bce7ff/attachment.html>


More information about the asterisk-code-review mailing list