[Asterisk-code-review] json: Take advantage of new API's. (asterisk[master])

Corey Farrell asteriskteam at digium.com
Tue Sep 18 15:17:46 CDT 2018


Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/10193


Change subject: json: Take advantage of new API's.
......................................................................

json: Take advantage of new API's.

* Use json_sprintf to implement ast_json_stringf and ast_json_vstringf.
* Use "o*" format specifier for optional fields in ast_json_party_id.
* Stop using ast_json_deep_copy on immutable objects, it is now thread
  safe to just use ast_json_ref.

Additional changes to ast_json_pack calls in the vicinity:
* Use "O" when an object needs to be bumped.  This was previously
  avoided as it was not thread safe.
* Use "s?", "o?" and "O?" to replace NULL with ast_json_null().  The
  "?" is a new feature of ast_json_pack starting with Asterisk 16.

Change-Id: I8382d28d7d83ee0ce13334e51ae45dbc0bdaef48
---
M main/json.c
M main/rtp_engine.c
M main/stasis.c
M res/res_stasis.c
M res/res_stasis_playback.c
M res/res_stasis_recording.c
6 files changed, 26 insertions(+), 54 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/93/10193/1

diff --git a/main/json.c b/main/json.c
index f8f4c0d..38367a4 100644
--- a/main/json.c
+++ b/main/json.c
@@ -21,7 +21,7 @@
  * \brief JSON abstraction layer.
  *
  * This is a very thin wrapper around the Jansson API. For more details on it,
- * see its docs at http://www.digip.org/jansson/doc/2.4/apiref.html.
+ * see its docs at http://www.digip.org/jansson/doc/2.11/apiref.html.
  *
  * \author David M. Lee, II <dlee at digium.com>
  */
@@ -292,17 +292,11 @@
 
 struct ast_json *ast_json_vstringf(const char *format, va_list args)
 {
-	char *str = NULL;
-	json_t *ret = NULL;
-
-	if (format) {
-		int err = ast_vasprintf(&str, format, args);
-		if (err >= 0) {
-			ret = json_string(str);
-			ast_free(str);
-		}
+	if (!format) {
+		return NULL;
 	}
-	return (struct ast_json *)ret;
+
+	return (struct ast_json *)json_vsprintf(format, args);
 }
 
 struct ast_json *ast_json_integer_create(intmax_t value)
@@ -622,9 +616,9 @@
 
 struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
 {
-	return ast_json_pack("{s: o, s: o, s: o}",
-		"context", context ? ast_json_string_create(context) : ast_json_null(),
-		"exten", exten ? ast_json_string_create(exten) : ast_json_null(),
+	return ast_json_pack("{s: s?, s: s?, s: o}",
+		"context", context,
+		"exten", exten,
 		"priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
 }
 
@@ -747,37 +741,15 @@
 
 struct ast_json *ast_json_party_id(struct ast_party_id *party)
 {
-	RAII_VAR(struct ast_json *, json_party_id, NULL, ast_json_unref);
-	int pres;
+	int pres = ast_party_id_presentation(party);
 
 	/* Combined party presentation */
-	pres = ast_party_id_presentation(party);
-	json_party_id = ast_json_pack("{s: i, s: s}",
+	return ast_json_pack("{s: i, s: s, s: o*, s: o*, s: o*}",
 		"presentation", pres,
-		"presentation_txt", ast_describe_caller_presentation(pres));
-	if (!json_party_id) {
-		return NULL;
-	}
-
-	/* Party number */
-	if (party->number.valid
-		&& ast_json_object_set(json_party_id, "number", json_party_number(&party->number))) {
-		return NULL;
-	}
-
-	/* Party name */
-	if (party->name.valid
-		&& ast_json_object_set(json_party_id, "name", json_party_name(&party->name))) {
-		return NULL;
-	}
-
-	/* Party subaddress */
-	if (party->subaddress.valid
-		&& ast_json_object_set(json_party_id, "subaddress", json_party_subaddress(&party->subaddress))) {
-		return NULL;
-	}
-
-	return ast_json_ref(json_party_id);
+		"presentation_txt", ast_describe_caller_presentation(pres),
+		"number", json_party_number(&party->number),
+		"name", json_party_name(&party->name),
+		"subaddress", json_party_subaddress(&party->subaddress));
 }
 
 enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index 3d50774..8d40594 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -3430,10 +3430,10 @@
 		}
 	}
 
-	return ast_json_pack("{s: o, s: o, s: o}",
-		"channel", payload->snapshot ? json_channel : ast_json_null(),
+	return ast_json_pack("{s: o?, s: o, s: O?}",
+		"channel", json_channel,
 		"rtcp_report", json_rtcp_report,
-		"blob", ast_json_deep_copy(payload->blob) ?: ast_json_null());
+		"blob", payload->blob);
 }
 
 static void rtp_rtcp_report_dtor(void *obj)
diff --git a/main/stasis.c b/main/stasis.c
index 1616deb..ef2be22 100644
--- a/main/stasis.c
+++ b/main/stasis.c
@@ -1332,8 +1332,8 @@
 
 	ast_json_object_set(out, "type", ast_json_string_create("ChannelUserevent"));
 	ast_json_object_set(out, "timestamp", ast_json_timeval(*tv, NULL));
-	ast_json_object_set(out, "eventname", ast_json_string_create(ast_json_string_get((ast_json_object_get(blob, "eventname")))));
-	ast_json_object_set(out, "userevent", ast_json_deep_copy(blob));
+	ast_json_object_set(out, "eventname", ast_json_ref(ast_json_object_get(blob, "eventname")));
+	ast_json_object_set(out, "userevent", ast_json_ref(blob));
 
 	for (type = 0; type < STASIS_UMOS_MAX; ++type) {
 		for (i = 0; i < AST_VECTOR_SIZE(&multi->snapshots[type]); ++i) {
diff --git a/res/res_stasis.c b/res/res_stasis.c
index dcd7414..29c0c7c 100644
--- a/res/res_stasis.c
+++ b/res/res_stasis.c
@@ -148,10 +148,10 @@
 		return NULL;
 	}
 
-	msg = ast_json_pack("{s: s, s: o, s: o, s: o}",
+	msg = ast_json_pack("{s: s, s: O, s: O, s: o}",
 		"type", "StasisStart",
-		"timestamp", ast_json_copy(ast_json_object_get(payload->blob, "timestamp")),
-		"args", ast_json_deep_copy(ast_json_object_get(payload->blob, "args")),
+		"timestamp", ast_json_object_get(payload->blob, "timestamp"),
+		"args", ast_json_object_get(payload->blob, "args"),
 		"channel", ast_channel_snapshot_to_json(payload->channel, NULL));
 	if (!msg) {
 		ast_log(LOG_ERROR, "Failed to pack JSON for StasisStart message\n");
diff --git a/res/res_stasis_playback.c b/res/res_stasis_playback.c
index 5b8256f..a25a300 100644
--- a/res/res_stasis_playback.c
+++ b/res/res_stasis_playback.c
@@ -111,9 +111,9 @@
 		return NULL;
 	}
 
-	return ast_json_pack("{s: s, s: o}",
+	return ast_json_pack("{s: s, s: O}",
 		"type", type,
-		"playback", ast_json_deep_copy(blob));
+		"playback", blob);
 }
 
 STASIS_MESSAGE_TYPE_DEFN(stasis_app_playback_snapshot_type,
diff --git a/res/res_stasis_recording.c b/res/res_stasis_recording.c
index 17213aa..755bdcf 100644
--- a/res/res_stasis_recording.c
+++ b/res/res_stasis_recording.c
@@ -89,9 +89,9 @@
 		return NULL;
 	}
 
-	return ast_json_pack("{s: s, s: o}",
+	return ast_json_pack("{s: s, s: O}",
 		"type", type,
-		"recording", ast_json_deep_copy(blob));
+		"recording", blob);
 }
 
 STASIS_MESSAGE_TYPE_DEFN(stasis_app_recording_snapshot_type,

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8382d28d7d83ee0ce13334e51ae45dbc0bdaef48
Gerrit-Change-Number: 10193
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180918/d1b7ccdd/attachment-0001.html>


More information about the asterisk-code-review mailing list