From: Corey Farrell Date: Tue, 17 Jul 2018 03:55:02 +0000 (-0400) Subject: json: Take advantage of new API's. X-Git-Tag: 16.1.0-rc1~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b0dbda9367961a287ccfcf069a239cf5a6143d9;p=thirdparty%2Fasterisk.git json: Take advantage of new API's. * 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 "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 --- diff --git a/main/json.c b/main/json.c index f8f4c0df94..0bc1bd8277 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 */ @@ -747,37 +747,15 @@ static struct ast_json *json_party_subaddress(struct ast_party_subaddress *subad 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 3d507745d3..8d40594fda 100644 --- a/main/rtp_engine.c +++ b/main/rtp_engine.c @@ -3430,10 +3430,10 @@ static struct ast_json *rtcp_report_to_json(struct stasis_message *msg, } } - 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 c20156f5f6..51f01c0b0c 100644 --- a/main/stasis.c +++ b/main/stasis.c @@ -1368,8 +1368,8 @@ static struct ast_json *multi_user_event_to_json( 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 dcd74141f4..29c0c7c91d 100644 --- a/res/res_stasis.c +++ b/res/res_stasis.c @@ -148,10 +148,10 @@ static struct ast_json *stasis_start_to_json(struct stasis_message *message, 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 5b8256fc48..a25a3005b5 100644 --- a/res/res_stasis_playback.c +++ b/res/res_stasis_playback.c @@ -111,9 +111,9 @@ static struct ast_json *playback_to_json(struct stasis_message *message, 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 17213aa245..755bdcf3aa 100644 --- a/res/res_stasis_recording.c +++ b/res/res_stasis_recording.c @@ -89,9 +89,9 @@ static struct ast_json *recording_to_json(struct stasis_message *message, 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,