diff --git a/bundle.json b/bundle.json index c321683c3307445b7ee59e9431ea2f97f61745b2..54a336a1e77d35a81db53c8a8417e5050d008bd3 100644 --- a/bundle.json +++ b/bundle.json @@ -91,7 +91,8 @@ "appspawn", "bounds_checking_function", "webview", - "user_auth_framework" + "user_auth_framework", + "cJSON" ], "third_party": [ "libuv", diff --git a/interfaces/inner_api/appexecfwk_base/src/bundle_dir.cpp b/interfaces/inner_api/appexecfwk_base/src/bundle_dir.cpp index c2ab131db8c207e7f9daf7a6afc776c2be3661a8..86912fa91e80af9e7605997d3993af5d873c3fad 100644 --- a/interfaces/inner_api/appexecfwk_base/src/bundle_dir.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/bundle_dir.cpp @@ -1,5 +1,5 @@ /* - * 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 @@ -15,10 +15,8 @@ #include "bundle_dir.h" -#include "nlohmann/json.hpp" #include "string_ex.h" -#include "json_util.h" #include "parcel_macro.h" #include diff --git a/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_info.cpp b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_info.cpp index bd6116fa0f2119b32841869cef0fa206c402f93f..d55bb2a8c84e4d013f438ec08ffe7160badbc26d 100644 --- a/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_info.cpp @@ -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 @@ -16,7 +16,6 @@ #include "bundle_resource_info.h" #include "app_log_wrapper.h" -#include "nlohmann/json.hpp" #include "parcel_macro.h" #include "string_ex.h" diff --git a/interfaces/inner_api/appexecfwk_base/src/bundle_resource/launcher_ability_resource_info.cpp b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/launcher_ability_resource_info.cpp index 6a1359321a20e5ade38dbe7b15b793ceb9538852..19db1db7cc8863c2e616b82a83ac7c0b69e9de29 100644 --- a/interfaces/inner_api/appexecfwk_base/src/bundle_resource/launcher_ability_resource_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/launcher_ability_resource_info.cpp @@ -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 @@ -16,7 +16,6 @@ #include "launcher_ability_resource_info.h" #include "app_log_wrapper.h" -#include "nlohmann/json.hpp" #include "parcel_macro.h" #include "string_ex.h" diff --git a/interfaces/inner_api/appexecfwk_base/src/clone/clone_param.cpp b/interfaces/inner_api/appexecfwk_base/src/clone/clone_param.cpp index 5e8a6d248622e63ac7a2c484d4d82df14566f951..243b8efd80c2b493f7f96c2b8106d3ad3a66e572 100644 --- a/interfaces/inner_api/appexecfwk_base/src/clone/clone_param.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/clone/clone_param.cpp @@ -1,5 +1,5 @@ /* - * 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 @@ -15,7 +15,6 @@ #include "clone_param.h" -#include "nlohmann/json.hpp" #include "string_ex.h" #include "app_log_wrapper.h" diff --git a/interfaces/inner_api/appexecfwk_base/src/install_param.cpp b/interfaces/inner_api/appexecfwk_base/src/install_param.cpp index a6b51bcf602ee920261d918c19aa3216fd8aca81..77eb3cec90f1cc7a1f6a4667a882193f0977a77e 100644 --- a/interfaces/inner_api/appexecfwk_base/src/install_param.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/install_param.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -15,7 +15,6 @@ #include "install_param.h" -#include "nlohmann/json.hpp" #include "string_ex.h" #include "app_log_wrapper.h" diff --git a/interfaces/inner_api/appexecfwk_base/src/permission_define.cpp b/interfaces/inner_api/appexecfwk_base/src/permission_define.cpp index 11219e02142c8092bd8e8ba5a58c4551ebc4bda4..def1e4647424e62ebc2bbf7701630a1106c1a4c7 100644 --- a/interfaces/inner_api/appexecfwk_base/src/permission_define.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/permission_define.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -15,7 +15,6 @@ #include "permission_define.h" -#include "nlohmann/json.hpp" #include "string_ex.h" #include "app_log_wrapper.h" diff --git a/interfaces/inner_api/appexecfwk_base/src/preinstalled_application_info.cpp b/interfaces/inner_api/appexecfwk_base/src/preinstalled_application_info.cpp index 156f52f27cba47424213ffa234c40951a15b6815..1cb6a05261276c665d0006db8835e881cb5b2894 100644 --- a/interfaces/inner_api/appexecfwk_base/src/preinstalled_application_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/preinstalled_application_info.cpp @@ -1,5 +1,5 @@ /* - * 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 @@ -20,8 +20,6 @@ #include #include -#include "json_util.h" -#include "nlohmann/json.hpp" #include "parcel_macro.h" #include "string_ex.h" diff --git a/services/bundlemgr/BUILD.gn b/services/bundlemgr/BUILD.gn index 8c854da0868eda7a9a4111da9bf47876c629a338..eca1e8e948be076022715c01db2511136b942549 100644 --- a/services/bundlemgr/BUILD.gn +++ b/services/bundlemgr/BUILD.gn @@ -108,6 +108,7 @@ ohos_source_set("parser_common") { ] public_external_deps = [ + "cJSON:cjson", "json:nlohmann_json_static", "zlib:libz", ] @@ -204,6 +205,7 @@ ohos_source_set("bundle_parser") { ] public_external_deps = [ + "cJSON:cjson", "json:nlohmann_json_static", "zlib:libz", ] diff --git a/services/bundlemgr/include/inner_patch_info.h b/services/bundlemgr/include/inner_patch_info.h index 650b05746edad97289f95481e37b8035d46420ce..4c2d26edf369af2653603986b5136b799a94d06b 100644 --- a/services/bundlemgr/include/inner_patch_info.h +++ b/services/bundlemgr/include/inner_patch_info.h @@ -19,7 +19,7 @@ #include #include "json_util.h" -#include "nlohmann/json.hpp" +#include "cJSON.h" namespace OHOS { namespace AppExecFwk { @@ -59,7 +59,7 @@ public: } private: - void ToJson(nlohmann::json &jsonObject) const; + void ToJson(cJSON* jsonObject) const; PatchInfo patchInfo_; }; diff --git a/services/bundlemgr/src/inner_patch_info.cpp b/services/bundlemgr/src/inner_patch_info.cpp index 8c9a617a09270710584b7ff99fe7098a13ef315b..f3e6204b15b95f71526d10c175f2c0b90d96ba7c 100644 --- a/services/bundlemgr/src/inner_patch_info.cpp +++ b/services/bundlemgr/src/inner_patch_info.cpp @@ -19,7 +19,6 @@ namespace OHOS { namespace AppExecFwk { -using json = nlohmann::json; namespace { constexpr const char* VERSION_CODE = "versionCode"; constexpr const char* APP_PATCH_TYPE = "appPatchType"; @@ -27,39 +26,68 @@ constexpr const char* APP_PATCH_TYPE = "appPatchType"; bool InnerPatchInfo::FromJson(const std::string &jsonObject) { - if (jsonObject.empty() || !json::accept(jsonObject)) { + if (jsonObject.empty()) { APP_LOGE("invalid param"); return false; } - json jsonObj = json::parse(jsonObject, nullptr, false); - if (jsonObj.is_discarded()) { - APP_LOGE("jsonObj discarded"); + cJSON* root = cJSON_Parse(jsonObject.c_str()); + if (!root) { + APP_LOGE("parse json failed"); return false; } - if (!jsonObj.contains(VERSION_CODE) || !jsonObj.contains(APP_PATCH_TYPE)) { + if (!cJSON_IsObject(root)) { + APP_LOGE("json is not object"); + cJSON_Delete(root); + return false; + } + cJSON* versionCode = cJSON_GetObjectItem(root, VERSION_CODE); + cJSON* appPatchType = cJSON_GetObjectItem(root, APP_PATCH_TYPE); + if (!versionCode || !appPatchType) { APP_LOGE("json object missing required failed"); + cJSON_Delete(root); return false; } - if (!jsonObj[VERSION_CODE].is_number() || !jsonObj[APP_PATCH_TYPE].is_number()) { + if (!cJSON_IsNumber(versionCode) || !cJSON_IsNumber(appPatchType)) { APP_LOGE("json object value data type failed"); + cJSON_Delete(root); return false; } - patchInfo_.versionCode = static_cast(jsonObj[VERSION_CODE].get()); - patchInfo_.appPatchType = static_cast(jsonObj[APP_PATCH_TYPE].get()); + patchInfo_.versionCode = static_cast(versionCode->valuedouble); + patchInfo_.appPatchType = static_cast(appPatchType->valuedouble); + cJSON_Delete(root); return true; } std::string InnerPatchInfo::ToString() const { - nlohmann::json jsonObj; - ToJson(jsonObj); - return jsonObj.dump(); + cJSON* jsonObject = cJSON_CreateObject(); + if (jsonObject == nullptr) { + APP_LOGE("dump InnerPatchInfo cJSON_CreateObject error"); + return ""; + } + ToJson(jsonObject); + std::string result; + char* jsonStr = cJSON_PrintUnformatted(jsonObject); + if (jsonStr == nullptr) { + APP_LOGE("Failed to print JSON object"); + cJSON_Delete(jsonObject); + return ""; + } + result.append(jsonStr); + std::free(jsonStr); + jsonStr = nullptr; + cJSON_Delete(jsonObject); + return result; } -void InnerPatchInfo::ToJson(nlohmann::json &jsonObject) const +void InnerPatchInfo::ToJson(cJSON* jsonObject) const { - jsonObject[VERSION_CODE] = patchInfo_.versionCode; - jsonObject[APP_PATCH_TYPE] = patchInfo_.appPatchType; + if (!jsonObject || !cJSON_IsObject(jsonObject)) { + APP_LOGE("Invalid JSON object"); + return; + } + cJSON_AddNumberToObject(jsonObject, VERSION_CODE, patchInfo_.versionCode); + cJSON_AddNumberToObject(jsonObject, APP_PATCH_TYPE, static_cast(patchInfo_.appPatchType)); } } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp b/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp index c302217501df935a6fbcddd747c75e4994b47cac..323c94bed54bd349de48107973afc10166c1ce04 100644 --- a/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp +++ b/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp @@ -1916,12 +1916,16 @@ HWTEST_F(BmsEventHandlerTest, InnerPatchInfo_ToJson_0001, Function | SmallTest | EXPECT_EQ(innerPatchInfo.GetVersionCode(), 123); EXPECT_EQ(innerPatchInfo.GetAppPatchType(), AppPatchType::INTERNAL); - nlohmann::json jsonObj; + cJSON* jsonObj = cJSON_CreateObject(); + EXPECT_NE(jsonObj, nullptr); innerPatchInfo.ToJson(jsonObj); - EXPECT_TRUE(jsonObj.contains(VERSION_CODE)); - EXPECT_TRUE(jsonObj.contains(APP_PATCH_TYPE)); - EXPECT_EQ(jsonObj[VERSION_CODE], 123); - EXPECT_EQ(jsonObj[APP_PATCH_TYPE], static_cast(AppPatchType::INTERNAL)); + cJSON* versionCode = cJSON_GetObjectItem(jsonObj, VERSION_CODE); + EXPECT_NE(versionCode, nullptr); + cJSON* appPatchType = cJSON_GetObjectItem(jsonObj, APP_PATCH_TYPE); + EXPECT_NE(appPatchType, nullptr); + EXPECT_EQ(versionCode->valueint, 123); + EXPECT_EQ(appPatchType->valueint, static_cast(AppPatchType::INTERNAL)); + cJSON_Delete(jsonObj); } /**