cjson适配

Signed-off-by: woohoa <wanghuan36@huawei.com>
This commit is contained in:
15950533375
2025-06-17 20:15:09 +08:00
parent 000cdbf64e
commit ed520f6728
16 changed files with 108 additions and 86 deletions
+1 -1
View File
@@ -40,7 +40,7 @@
"netstack",
"os_account",
"ffrt",
"json",
"cJSON",
"curl",
"preferences",
"access_token",
+2 -2
View File
@@ -1,4 +1,4 @@
# Copyright (C) 2024 Huawei Device Co., Ltd.
# Copyright (C) 2024-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -49,6 +49,7 @@ ohos_shared_library("app_domain_verify_app_details_rdb") {
"ability_runtime:ability_manager",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"curl:curl_shared",
"eventhandler:libeventhandler",
@@ -56,7 +57,6 @@ ohos_shared_library("app_domain_verify_app_details_rdb") {
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"netstack:http_client",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
+2 -2
View File
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -62,13 +62,13 @@ ohos_shared_library("app_domain_verify_frameworks_common") {
"access_token:libtokenid_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"ffrt:libffrt",
"hicollie:libhicollie",
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"memmgr:memmgrclient",
"netstack:http_client",
"os_account:os_account_innerkits",
+2 -2
View File
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -63,6 +63,7 @@ ohos_shared_library("app_domain_verify_agent_verifier") {
"ability_runtime:ability_manager",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"curl:curl_shared",
"eventhandler:libeventhandler",
@@ -70,7 +71,6 @@ ohos_shared_library("app_domain_verify_agent_verifier") {
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"netstack:http_client",
"os_account:os_account_innerkits",
"safwk:system_ability_fwk",
+48 -49
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Copyright (C) 2023-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -12,64 +12,63 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "json.hpp"
#include "cJSON.h"
#include "domain_json_util.h"
#include "agent_constants.h"
#include "app_domain_verify_hilog.h"
namespace OHOS {
namespace AppDomainVerify {
using json = nlohmann::json;
bool JsonUtil::Parse(const std::string &assetJsonsStr, AssetJsonObj &assetJsonObj)
{
if (!assetJsonsStr.empty()) {
json jsonObj;
try {
if (!json::accept(assetJsonsStr)) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be accept.");
return false;
}
jsonObj = json::parse(assetJsonsStr);
} catch (json::parse_error &e) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be parsed.");
return false;
}
if (jsonObj == nullptr || !jsonObj.is_object()) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE,
"assetLinksStr can not be parsed into obj.");
return false;
}
if (jsonObj.find(ApplinkingAssetKeys::APP_LINKING) == jsonObj.end() ||
!jsonObj.at(ApplinkingAssetKeys::APP_LINKING).is_object()) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not parsed applinking into obj.");
return false;
}
auto applinkingObj = jsonObj.at(ApplinkingAssetKeys::APP_LINKING);
if (applinkingObj.find(ApplinkingAssetKeys::APPS) != applinkingObj.end() &&
applinkingObj.at(ApplinkingAssetKeys::APPS).is_array()) {
auto appsArray = applinkingObj.at(ApplinkingAssetKeys::APPS);
for (size_t i = 0; i < appsArray.size(); i++) {
AppVerifyBaseInfo appVerifyBaseInfo;
auto arrayItem = appsArray[i];
appVerifyBaseInfo.appIdentifier = arrayItem.find(ApplinkingAssetKeys::APP_IDENTIFIER) !=
arrayItem.end() &&
arrayItem.at(ApplinkingAssetKeys::APP_IDENTIFIER).is_string() ?
arrayItem.at(ApplinkingAssetKeys::APP_IDENTIFIER) :
"";
appVerifyBaseInfo.bundleName = arrayItem.find(ApplinkingAssetKeys::BUNDLE_NAME) != arrayItem.end() &&
arrayItem.at(ApplinkingAssetKeys::BUNDLE_NAME).is_string() ?
arrayItem.at(ApplinkingAssetKeys::BUNDLE_NAME) :
"";
appVerifyBaseInfo.fingerprint = arrayItem.find(ApplinkingAssetKeys::FINGERPRINT) != arrayItem.end() &&
arrayItem.at(ApplinkingAssetKeys::FINGERPRINT).is_string() ?
arrayItem.at(ApplinkingAssetKeys::FINGERPRINT) :
"";
assetJsonObj.applinking.apps.emplace_back(appVerifyBaseInfo);
}
return true;
}
if (assetJsonsStr.empty()) {
return false;
}
cJSON *jsonObj = cJSON_Parse(assetJsonsStr.c_str());
if (jsonObj == nullptr) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be parsed.");
return false;
}
if (!cJSON_IsObject(jsonObj)) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetLinksStr can not be parsed into obj.");
cJSON_Delete(jsonObj);
return false;
}
cJSON *applinkingObj = cJSON_GetObjectItemCaseSensitive(jsonObj, ApplinkingAssetKeys::APP_LINKING.c_str());
if (applinkingObj == nullptr || !cJSON_IsObject(applinkingObj)) {
APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not parsed applinking into obj.");
cJSON_Delete(jsonObj);
return false;
}
cJSON *appsArray = cJSON_GetObjectItemCaseSensitive(applinkingObj, ApplinkingAssetKeys::APPS.c_str());
if (appsArray != nullptr && cJSON_IsArray(appsArray)) {
int arraySize = cJSON_GetArraySize(appsArray);
for (int i = 0; i < arraySize; i++) {
cJSON *arrayItem = cJSON_GetArrayItem(appsArray, i);
if (!cJSON_IsObject(arrayItem)) {
continue;
}
AppVerifyBaseInfo appVerifyBaseInfo;
cJSON *appIdentifier = cJSON_GetObjectItemCaseSensitive(arrayItem,
ApplinkingAssetKeys::APP_IDENTIFIER.c_str());
if (appIdentifier != nullptr && cJSON_IsString(appIdentifier) && appIdentifier->valuestring != nullptr) {
appVerifyBaseInfo.appIdentifier = appIdentifier->valuestring;
}
cJSON *bundleName = cJSON_GetObjectItemCaseSensitive(arrayItem, ApplinkingAssetKeys::BUNDLE_NAME.c_str());
if (bundleName != nullptr && cJSON_IsString(bundleName) && bundleName->valuestring != nullptr) {
appVerifyBaseInfo.bundleName = bundleName->valuestring;
}
cJSON *fingerprint = cJSON_GetObjectItemCaseSensitive(arrayItem, ApplinkingAssetKeys::FINGERPRINT.c_str());
if (fingerprint != nullptr && cJSON_IsString(fingerprint) && fingerprint->valuestring != nullptr) {
appVerifyBaseInfo.fingerprint = fingerprint->valuestring;
}
assetJsonObj.applinking.apps.emplace_back(appVerifyBaseInfo);
}
cJSON_Delete(jsonObj);
return true;
}
cJSON_Delete(jsonObj);
return false;
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -53,11 +53,11 @@ ohos_shared_library("app_domain_verify_common") {
external_deps = [
"ability_base:want",
"cJSON:cjson",
"c_utils:utils",
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
+3 -3
View File
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -92,13 +92,13 @@ ohos_shared_library("app_domain_verify_mgr_service") {
"access_token:libtokenid_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"os_account:os_account_innerkits",
"preferences:native_preferences",
"relational_store:native_rdb",
@@ -186,6 +186,7 @@ ohos_shared_library("app_domain_verify_agent_service") {
"ability_base:zuri",
"ability_runtime:ability_manager",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"curl:curl_shared",
"eventhandler:libeventhandler",
@@ -194,7 +195,6 @@ ohos_shared_library("app_domain_verify_agent_service") {
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"netmanager_base:net_conn_manager_if",
"netstack:http_client",
"os_account:os_account_innerkits",
@@ -1,4 +1,4 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Copyright (c) 2024-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -71,7 +71,6 @@ ohos_fuzztest("AppDomainVerifyAgentServiceFuzzTest") {
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"netmanager_base:net_conn_manager_if",
"netstack:http_client",
"os_account:os_account_innerkits",
@@ -1,4 +1,4 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Copyright (c) 2024-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -72,7 +72,6 @@ ohos_fuzztest("AppDomainVerifyMgrServiceFuzzTest") {
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_core",
"json:nlohmann_json_static",
"netmanager_base:net_conn_manager_if",
"netstack:http_client",
"os_account:os_account_innerkits",
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -59,7 +59,6 @@ ohos_unittest("app_domain_verify_bean_parcel_test") {
"googletest:gtest_main",
"hilog:libhilog",
"hisysevent:libhisysevent",
"json:nlohmann_json_static",
]
defines = [ "API_EXPORT=__attribute__((visibility (\"default\")))" ]
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -63,6 +63,7 @@ ohos_unittest("app_domain_verify_bms_test") {
"access_token:libtokensetproc_shared",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "cJSON.h"
#include "parse_util.h"
#include "system_ability_ondemand_reason.h"
@@ -48,14 +49,21 @@ bool OnDemandReasonExtraData::Marshalling(Parcel& parcel) const
if (!parcel.WriteString(data_)) {
return false;
}
nlohmann::json payload;
for (auto it = want_.begin(); it != want_.end(); ++it) {
payload[it->first] = it->second;
}
if (!parcel.WriteString(payload.dump())) {
cJSON *payload = cJSON_CreateObject();
if (payload == nullptr) {
return false;
}
return true;
for (const auto& pair : want_) {
cJSON_AddStringToObject(payload, pair.first.c_str(), pair.second.c_str());
}
char *jsonString = cJSON_PrintUnformatted(payload);
bool result = false;
if (jsonString != nullptr) {
result = parcel.WriteString(jsonString);
cJSON_free(jsonString);
}
cJSON_Delete(payload);
return result;
}
OnDemandReasonExtraData *OnDemandReasonExtraData::Unmarshalling(Parcel& parcel)
@@ -69,14 +77,27 @@ OnDemandReasonExtraData *OnDemandReasonExtraData::Unmarshalling(Parcel& parcel)
return nullptr;
}
std::map<std::string, std::string> want;
nlohmann::json payload = ParseUtil::StringToJsonObj(parcel.ReadString());
for (nlohmann::json::iterator it = payload.begin(); it != payload.end(); ++it) {
if (it.value().is_string()) {
want[it.key()] = it.value();
}
std::string jsonStr;
if (!parcel.ReadString(jsonStr)) {
return nullptr;
}
OnDemandReasonExtraData* extraData = new OnDemandReasonExtraData(code, data, want);
return extraData;
cJSON *payload = cJSON_Parse(jsonStr.c_str());
if (payload == nullptr) {
return nullptr;
}
cJSON *child = payload->child;
while (child != nullptr) {
if (child->string == nullptr || *child->string == '\0') {
child = child->next;
continue;
}
if (cJSON_IsString(child) && child->valuestring != nullptr) {
want[child->string] = child->valuestring;
}
child = child->next;
}
cJSON_Delete(payload);
return new OnDemandReasonExtraData(code, data, want);
}
SystemAbilityOnDemandReason::SystemAbilityOnDemandReason(OnDemandReasonId reasonId, const std::string& reasonName,
@@ -1,4 +1,4 @@
# Copyright (C) 2024 Huawei Device Co., Ltd.
# Copyright (C) 2024-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -62,6 +62,7 @@ ohos_unittest("ability_filter_test") {
"access_token:libtokensetproc_shared",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -58,6 +58,7 @@ ohos_unittest("app_domain_verify_agent_service_test") {
"ability_base:zuri",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -78,6 +78,7 @@ ohos_unittest("app_domain_verify_mgr_service_test") {
"access_token:libtokensetproc_shared",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
+2 -1
View File
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Copyright (C) 2023-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -62,6 +62,7 @@ ohos_unittest("app_domain_verify_agent_verifier_test") {
"ability_base:zuri",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",