180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#include "json_utils.h" 1780922886Sopenharmony_ci#include "avsession_log.h" 1880922886Sopenharmony_ci#include "av_session.h" 1980922886Sopenharmony_ci 2080922886Sopenharmony_cinamespace OHOS::AVSession { 2180922886Sopenharmony_ciint32_t JsonUtils::ConvertSessionType(const std::string& typeString) 2280922886Sopenharmony_ci{ 2380922886Sopenharmony_ci int32_t type = AVSession::SESSION_TYPE_INVALID; 2480922886Sopenharmony_ci if (typeString == "audio") { 2580922886Sopenharmony_ci type = AVSession::SESSION_TYPE_AUDIO; 2680922886Sopenharmony_ci } else if (typeString == "video") { 2780922886Sopenharmony_ci type = AVSession::SESSION_TYPE_VIDEO; 2880922886Sopenharmony_ci } 2980922886Sopenharmony_ci return type; 3080922886Sopenharmony_ci} 3180922886Sopenharmony_ci 3280922886Sopenharmony_cistd::string JsonUtils::ConvertSessionType(int32_t type) 3380922886Sopenharmony_ci{ 3480922886Sopenharmony_ci if (type == AVSession::SESSION_TYPE_AUDIO) { 3580922886Sopenharmony_ci return "audio"; 3680922886Sopenharmony_ci } else if (type == AVSession::SESSION_TYPE_VIDEO) { 3780922886Sopenharmony_ci return "video"; 3880922886Sopenharmony_ci } else { 3980922886Sopenharmony_ci return ""; 4080922886Sopenharmony_ci } 4180922886Sopenharmony_ci} 4280922886Sopenharmony_ci 4380922886Sopenharmony_ciint32_t JsonUtils::JsonToVector(json object, std::vector<int32_t>& out) 4480922886Sopenharmony_ci{ 4580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!object.is_null(), AVSESSION_ERROR, "json object is null"); 4680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(object.is_array(), AVSESSION_ERROR, "json object is not array"); 4780922886Sopenharmony_ci for (json::iterator it = object.begin(); it != object.end(); ++it) { 4880922886Sopenharmony_ci out.push_back(it.value()); 4980922886Sopenharmony_ci } 5080922886Sopenharmony_ci return AVSESSION_SUCCESS; 5180922886Sopenharmony_ci} 5280922886Sopenharmony_ci 5380922886Sopenharmony_ciint32_t JsonUtils::GetJsonCapability(const std::vector<std::vector<int32_t>>& capability, std::string& jsonCapability) 5480922886Sopenharmony_ci{ 5580922886Sopenharmony_ci json jsonObject; 5680922886Sopenharmony_ci for (uint32_t i = 0; i < capability.size(); i++) { 5780922886Sopenharmony_ci if (i == SESSION_DATA_META) { 5880922886Sopenharmony_ci jsonObject["metaData"] = capability[i]; 5980922886Sopenharmony_ci continue; 6080922886Sopenharmony_ci } 6180922886Sopenharmony_ci if (i == SESSION_DATA_PLAYBACK_STATE) { 6280922886Sopenharmony_ci jsonObject["playbackState"] = capability[i]; 6380922886Sopenharmony_ci continue; 6480922886Sopenharmony_ci } 6580922886Sopenharmony_ci if (i == SESSION_DATA_CONTROL_COMMAND) { 6680922886Sopenharmony_ci jsonObject["controlCommand"] = capability[i]; 6780922886Sopenharmony_ci continue; 6880922886Sopenharmony_ci } 6980922886Sopenharmony_ci } 7080922886Sopenharmony_ci jsonCapability = jsonObject.dump(); 7180922886Sopenharmony_ci return AVSESSION_SUCCESS; 7280922886Sopenharmony_ci} 7380922886Sopenharmony_ci 7480922886Sopenharmony_cibool JsonUtils::IsInt32(const json& jsonObj, const std::string& key) 7580922886Sopenharmony_ci{ 7680922886Sopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() 7780922886Sopenharmony_ci && INT32_MIN <= jsonObj[key] && jsonObj[key] <= INT32_MAX; 7880922886Sopenharmony_ci if (!res) { 7980922886Sopenharmony_ci SLOGE("The key %{public}s of jsonObj is invalid", key.c_str()); 8080922886Sopenharmony_ci } 8180922886Sopenharmony_ci return res; 8280922886Sopenharmony_ci} 8380922886Sopenharmony_ci 8480922886Sopenharmony_cibool JsonUtils::IsString(const json& jsonObj, const std::string& key) 8580922886Sopenharmony_ci{ 8680922886Sopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_string(); 8780922886Sopenharmony_ci if (!res) { 8880922886Sopenharmony_ci SLOGE("The key %{public}s of jsonObj is invalid", key.c_str()); 8980922886Sopenharmony_ci } 9080922886Sopenharmony_ci return res; 9180922886Sopenharmony_ci} 9280922886Sopenharmony_ci 9380922886Sopenharmony_cibool JsonUtils::IsBool(const json& jsonObj, const std::string& key) 9480922886Sopenharmony_ci{ 9580922886Sopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_boolean(); 9680922886Sopenharmony_ci if (!res) { 9780922886Sopenharmony_ci SLOGE("The key %{public}s of jsonObj is invalid", key.c_str()); 9880922886Sopenharmony_ci } 9980922886Sopenharmony_ci return res; 10080922886Sopenharmony_ci} 10180922886Sopenharmony_ci 10280922886Sopenharmony_ciint32_t JsonUtils::GetVectorCapability(const std::string& jsonCapability, 10380922886Sopenharmony_ci std::vector<std::vector<int32_t>>& vectorCapability) 10480922886Sopenharmony_ci{ 10580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonCapability.empty(), AVSESSION_ERROR, "jsonCapability is empty"); 10680922886Sopenharmony_ci json jsonObj = json::parse(jsonCapability, nullptr, false); 10780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 10880922886Sopenharmony_ci int32_t ret = JsonToVector(jsonObj["metaData"], vectorCapability[SESSION_DATA_META]); 10980922886Sopenharmony_ci CHECK_AND_CONTINUE_LOG(ret == AVSESSION_SUCCESS, "Get metaDataCapability error"); 11080922886Sopenharmony_ci ret = JsonToVector(jsonObj["playbackState"], vectorCapability[SESSION_DATA_PLAYBACK_STATE]); 11180922886Sopenharmony_ci CHECK_AND_CONTINUE_LOG(ret == AVSESSION_SUCCESS, "Get playbackStateCapability error"); 11280922886Sopenharmony_ci ret = JsonToVector(jsonObj["controlCommand"], vectorCapability[SESSION_DATA_CONTROL_COMMAND]); 11380922886Sopenharmony_ci CHECK_AND_CONTINUE_LOG(ret == AVSESSION_SUCCESS, "Get controlCommandCapability error"); 11480922886Sopenharmony_ci return AVSESSION_SUCCESS; 11580922886Sopenharmony_ci} 11680922886Sopenharmony_ci 11780922886Sopenharmony_ciint32_t JsonUtils::GetAllCapability(const std::string& sessionInfo, std::string& jsonCapability) 11880922886Sopenharmony_ci{ 11980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty"); 12080922886Sopenharmony_ci json jsonSessionInfo = json::parse(sessionInfo, nullptr, false); 12180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonSessionInfo.is_discarded() && !jsonSessionInfo.is_null(), AVSESSION_ERROR, 12280922886Sopenharmony_ci "json object is null"); 12380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonSessionInfo.contains("compatibility"), AVSESSION_ERROR, 12480922886Sopenharmony_ci "The key of jsonObj is invalid"); 12580922886Sopenharmony_ci json compatibility = jsonSessionInfo["compatibility"]; 12680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!compatibility.is_null(), AVSESSION_ERROR, "Getcompatibility error"); 12780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(compatibility.contains("capabilitySet"), AVSESSION_ERROR, 12880922886Sopenharmony_ci "The key of compatibility is invalid"); 12980922886Sopenharmony_ci json capabilitySet = compatibility["capabilitySet"]; 13080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!capabilitySet.is_null(), AVSESSION_ERROR, "GetCapabilitySet error"); 13180922886Sopenharmony_ci jsonCapability = capabilitySet.dump(); 13280922886Sopenharmony_ci return AVSESSION_SUCCESS; 13380922886Sopenharmony_ci} 13480922886Sopenharmony_ci 13580922886Sopenharmony_ciint32_t JsonUtils::SetSessionBasicInfo(std::string& sessionInfo, const AVSessionBasicInfo& basicInfo) 13680922886Sopenharmony_ci{ 13780922886Sopenharmony_ci json jsonObj; 13880922886Sopenharmony_ci if (sessionInfo.empty()) { 13980922886Sopenharmony_ci jsonObj = json::parse(R"({})", nullptr, false); 14080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 14180922886Sopenharmony_ci } else { 14280922886Sopenharmony_ci jsonObj = json::parse(sessionInfo, nullptr, false); 14380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 14480922886Sopenharmony_ci } 14580922886Sopenharmony_ci jsonObj["compatibility"]["networkId"] = basicInfo.networkId_; 14680922886Sopenharmony_ci jsonObj["compatibility"]["vendorId"] = basicInfo.vendorId_; 14780922886Sopenharmony_ci jsonObj["compatibility"]["deviceType"] = basicInfo.deviceType_; 14880922886Sopenharmony_ci jsonObj["compatibility"]["systemVersion"] = basicInfo.systemVersion_; 14980922886Sopenharmony_ci jsonObj["compatibility"]["avsessionVersion"] = basicInfo.sessionVersion_; 15080922886Sopenharmony_ci jsonObj["compatibility"]["reserve"] = basicInfo.reserve_; 15180922886Sopenharmony_ci jsonObj["compatibility"]["features"] = basicInfo.feature_; 15280922886Sopenharmony_ci jsonObj["compatibility"]["capabilitySet"]["metaData"] = basicInfo.metaDataCap_; 15380922886Sopenharmony_ci jsonObj["compatibility"]["capabilitySet"]["playbackState"] = basicInfo.playBackStateCap_; 15480922886Sopenharmony_ci jsonObj["compatibility"]["capabilitySet"]["controlCommand"] = basicInfo.controlCommandCap_; 15580922886Sopenharmony_ci jsonObj["compatibility"]["extendCapability"] = basicInfo.extendCapability_; 15680922886Sopenharmony_ci jsonObj["data"]["systemTime"] = basicInfo.systemTime_; 15780922886Sopenharmony_ci jsonObj["data"]["extend"] = basicInfo.extend_; 15880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.empty(), AVSESSION_ERROR, "SetBasicInfo error"); 15980922886Sopenharmony_ci sessionInfo = jsonObj.dump(); 16080922886Sopenharmony_ci return AVSESSION_SUCCESS; 16180922886Sopenharmony_ci} 16280922886Sopenharmony_ci 16380922886Sopenharmony_ciint32_t JsonUtils::GetSessionBasicInfo(const std::string& sessionInfo, AVSessionBasicInfo& basicInfo) 16480922886Sopenharmony_ci{ 16580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty"); 16680922886Sopenharmony_ci json jsonObj = json::parse(sessionInfo, nullptr, false); 16780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 16880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonObj.contains("compatibility") && jsonObj.contains("data"), AVSESSION_ERROR, 16980922886Sopenharmony_ci "The key of jsonObj is invalid"); 17080922886Sopenharmony_ci json compatibility = jsonObj["compatibility"]; 17180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!compatibility.empty(), AVSESSION_ERROR, "Getcompatibility error"); 17280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(IsString(compatibility, "networkId") && IsString(compatibility, "vendorId") 17380922886Sopenharmony_ci && IsString(compatibility, "deviceType") && IsString(compatibility, "systemVersion") 17480922886Sopenharmony_ci && IsInt32(compatibility, "avsessionVersion") && IsInt32(jsonObj["data"], "systemTime") 17580922886Sopenharmony_ci && compatibility.contains("reserve") && compatibility.contains("features") 17680922886Sopenharmony_ci && compatibility.contains("extendCapability"), AVSESSION_ERROR, 17780922886Sopenharmony_ci "The key of jsonObj is invalid"); 17880922886Sopenharmony_ci basicInfo.networkId_ = compatibility["networkId"]; 17980922886Sopenharmony_ci basicInfo.vendorId_ = compatibility["vendorId"]; 18080922886Sopenharmony_ci basicInfo.deviceType_ = compatibility["deviceType"]; 18180922886Sopenharmony_ci basicInfo.systemVersion_ = compatibility["systemVersion"]; 18280922886Sopenharmony_ci basicInfo.sessionVersion_ = compatibility["avsessionVersion"]; 18380922886Sopenharmony_ci int32_t ret = JsonToVector(compatibility["reserve"], basicInfo.reserve_); 18480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get reserve error"); 18580922886Sopenharmony_ci ret = JsonToVector(compatibility["features"], basicInfo.feature_); 18680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get feature error"); 18780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(compatibility.contains("capabilitySet"), AVSESSION_ERROR, 18880922886Sopenharmony_ci "The key of jsonObj is invalid"); 18980922886Sopenharmony_ci json capabilitySet = compatibility["capabilitySet"]; 19080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!capabilitySet.empty(), AVSESSION_ERROR, "GetCapabilitySet error"); 19180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(capabilitySet.contains("metaData") && capabilitySet.contains("playbackState") 19280922886Sopenharmony_ci && capabilitySet.contains("controlCommand"), AVSESSION_ERROR, "The key of jsonObj is invalid"); 19380922886Sopenharmony_ci ret = JsonToVector(capabilitySet["metaData"], basicInfo.metaDataCap_); 19480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get metaData error"); 19580922886Sopenharmony_ci ret = JsonToVector(capabilitySet["playbackState"], basicInfo.playBackStateCap_); 19680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get playbackState error"); 19780922886Sopenharmony_ci ret = JsonToVector(capabilitySet["controlCommand"], basicInfo.controlCommandCap_); 19880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get controlCommand error"); 19980922886Sopenharmony_ci ret = JsonToVector(compatibility["extendCapability"], basicInfo.extendCapability_); 20080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get extendCapability error"); 20180922886Sopenharmony_ci basicInfo.systemTime_ = jsonObj["data"]["systemTime"]; 20280922886Sopenharmony_ci ret = JsonToVector(jsonObj["data"]["extend"], basicInfo.extend_); 20380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get data extend error"); 20480922886Sopenharmony_ci return AVSESSION_SUCCESS; 20580922886Sopenharmony_ci} 20680922886Sopenharmony_ci 20780922886Sopenharmony_ciint32_t JsonUtils::SetSessionDescriptors(std::string& sessionInfo, const std::vector<AVSessionDescriptor>& descriptors) 20880922886Sopenharmony_ci{ 20980922886Sopenharmony_ci json jsonObj; 21080922886Sopenharmony_ci if (sessionInfo.empty()) { 21180922886Sopenharmony_ci jsonObj = json::parse(R"({})", nullptr, false); 21280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 21380922886Sopenharmony_ci } else { 21480922886Sopenharmony_ci jsonObj = json::parse(sessionInfo, nullptr, false); 21580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 21680922886Sopenharmony_ci } 21780922886Sopenharmony_ci for (uint32_t i = 0; i < descriptors.size(); i++) { 21880922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["sessionId"] = descriptors[i].sessionId_; 21980922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["type"] = ConvertSessionType(descriptors[i].sessionType_); 22080922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["bundleName"] = descriptors[i].elementName_.GetBundleName(); 22180922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["abilityName"] = descriptors[i].elementName_.GetAbilityName(); 22280922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["tag"] = descriptors[i].sessionTag_; 22380922886Sopenharmony_ci jsonObj["data"]["sessionDescriptors"][i]["isThirdPartyApp"] = descriptors[i].isThirdPartyApp_; 22480922886Sopenharmony_ci } 22580922886Sopenharmony_ci sessionInfo = jsonObj.dump(); 22680922886Sopenharmony_ci return AVSESSION_SUCCESS; 22780922886Sopenharmony_ci} 22880922886Sopenharmony_ci 22980922886Sopenharmony_ciint32_t JsonUtils::GetSessionDescriptors(const std::string& sessionInfo, std::vector<AVSessionDescriptor>& descriptors) 23080922886Sopenharmony_ci{ 23180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty"); 23280922886Sopenharmony_ci json jsonObj = json::parse(sessionInfo, nullptr, false); 23380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 23480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonObj.contains("data"), AVSESSION_ERROR, "json object data is null"); 23580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonObj["data"].contains("sessionDescriptors"), AVSESSION_ERROR, 23680922886Sopenharmony_ci "The key of jsonObj is invalid"); 23780922886Sopenharmony_ci json sessionDescriptors = jsonObj["data"]["sessionDescriptors"]; 23880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionDescriptors.is_null(), AVSESSION_ERROR, "sessionDescriptors is null"); 23980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(sessionDescriptors.is_array(), AVSESSION_ERROR, "json sessionDescriptors is not array"); 24080922886Sopenharmony_ci for (json::iterator it = sessionDescriptors.begin(); it != sessionDescriptors.end(); ++it) { 24180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(IsString(it.value(), "sessionId") && IsString(it.value(), "type") 24280922886Sopenharmony_ci && IsString(it.value(), "bundleName") && IsString(it.value(), "abilityName") 24380922886Sopenharmony_ci && IsString(it.value(), "tag") && IsBool(it.value(), "isThirdPartyApp"), AVSESSION_ERROR, 24480922886Sopenharmony_ci "The key of jsonObj is invalid"); 24580922886Sopenharmony_ci AVSessionDescriptor descriptor; 24680922886Sopenharmony_ci descriptor.sessionId_ = it.value()["sessionId"]; 24780922886Sopenharmony_ci std::string type = it.value()["type"]; 24880922886Sopenharmony_ci descriptor.sessionType_ = ConvertSessionType(type); 24980922886Sopenharmony_ci descriptor.elementName_.SetBundleName(it.value()["bundleName"]); 25080922886Sopenharmony_ci descriptor.elementName_.SetAbilityName(it.value()["abilityName"]); 25180922886Sopenharmony_ci descriptor.sessionTag_ = it.value()["tag"]; 25280922886Sopenharmony_ci descriptor.isThirdPartyApp_ = it.value()["isThirdPartyApp"]; 25380922886Sopenharmony_ci descriptors.push_back(descriptor); 25480922886Sopenharmony_ci } 25580922886Sopenharmony_ci return AVSESSION_SUCCESS; 25680922886Sopenharmony_ci} 25780922886Sopenharmony_ci 25880922886Sopenharmony_ciint32_t JsonUtils::SetSessionDescriptor(std::string& sessionInfo, const AVSessionDescriptor& descriptor) 25980922886Sopenharmony_ci{ 26080922886Sopenharmony_ci json jsonObj; 26180922886Sopenharmony_ci if (sessionInfo.empty()) { 26280922886Sopenharmony_ci jsonObj = json::parse(R"({})", nullptr, false); 26380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 26480922886Sopenharmony_ci } else { 26580922886Sopenharmony_ci jsonObj = json::parse(sessionInfo, nullptr, false); 26680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 26780922886Sopenharmony_ci } 26880922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["sessionId"] = descriptor.sessionId_; 26980922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["type"] = ConvertSessionType(descriptor.sessionType_); 27080922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["bundleName"] = descriptor.elementName_.GetBundleName(); 27180922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["abilityName"] = descriptor.elementName_.GetAbilityName(); 27280922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["tag"] = descriptor.sessionTag_; 27380922886Sopenharmony_ci jsonObj["data"]["sessionDescriptor"]["isThirdPartyApp"] = descriptor.isThirdPartyApp_; 27480922886Sopenharmony_ci sessionInfo = jsonObj.dump(); 27580922886Sopenharmony_ci return AVSESSION_SUCCESS; 27680922886Sopenharmony_ci} 27780922886Sopenharmony_ci 27880922886Sopenharmony_ciint32_t JsonUtils::GetSessionDescriptor(const std::string& sessionInfo, AVSessionDescriptor& descriptor) 27980922886Sopenharmony_ci{ 28080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty"); 28180922886Sopenharmony_ci json jsonObj = json::parse(sessionInfo, nullptr, false); 28280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!jsonObj.is_discarded() && !jsonObj.is_null(), AVSESSION_ERROR, "json object is null"); 28380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonObj.contains("data"), AVSESSION_ERROR, "json object data is null"); 28480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jsonObj["data"].contains("sessionDescriptor"), AVSESSION_ERROR, 28580922886Sopenharmony_ci "The key of jsonObj is invalid"); 28680922886Sopenharmony_ci json sessionDescriptor = jsonObj["data"]["sessionDescriptor"]; 28780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!sessionDescriptor.is_null(), AVSESSION_ERROR, "sessionDescriptor is null"); 28880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(IsString(sessionDescriptor, "sessionId") && IsString(sessionDescriptor, "type") 28980922886Sopenharmony_ci && IsString(sessionDescriptor, "bundleName") && IsString(sessionDescriptor, "abilityName") 29080922886Sopenharmony_ci && IsString(sessionDescriptor, "tag") && IsBool(sessionDescriptor, "isThirdPartyApp"), AVSESSION_ERROR, 29180922886Sopenharmony_ci "The key of jsonObj is invalid"); 29280922886Sopenharmony_ci 29380922886Sopenharmony_ci descriptor.sessionId_ = sessionDescriptor["sessionId"]; 29480922886Sopenharmony_ci std::string type = sessionDescriptor["type"]; 29580922886Sopenharmony_ci descriptor.sessionType_ = ConvertSessionType(type); 29680922886Sopenharmony_ci descriptor.elementName_.SetBundleName(sessionDescriptor["bundleName"]); 29780922886Sopenharmony_ci descriptor.elementName_.SetAbilityName(sessionDescriptor["abilityName"]); 29880922886Sopenharmony_ci descriptor.sessionTag_ = sessionDescriptor["tag"]; 29980922886Sopenharmony_ci descriptor.isThirdPartyApp_ = sessionDescriptor["isThirdPartyApp"]; 30080922886Sopenharmony_ci return AVSESSION_SUCCESS; 30180922886Sopenharmony_ci} 30280922886Sopenharmony_ci} // namespace OHOS::AVSession 303