diff --git a/foundations/ability/utils/include/dupdate_json_utils.h b/foundations/ability/utils/include/dupdate_json_utils.h index 6dfc6f570b4e8d4d7b297e9ebb33f4dcb0056074..f9db712980ac909e96f2d29c5fc5f58fcae11bbe 100644 --- a/foundations/ability/utils/include/dupdate_json_utils.h +++ b/foundations/ability/utils/include/dupdate_json_utils.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Huawei Device Co., Ltd. +* Copyright (c) 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 @@ -19,9 +19,9 @@ #include #include #include +#include -#include "nlohmann/json.hpp" - +#include "cJSON.h" #include "update_define.h" namespace OHOS::UpdateService { @@ -36,163 +36,143 @@ enum class JsonParseError { // 当前获取json对象/数组的封装使用的parse方法参数:string输入、无解析回调、解析失败不报异常 class JsonUtils { public: - template - static int32_t GetValueAndSetTo(const nlohmann::json &jsonObject, const std::string &key, T &value) + template static int32_t GetValueAndSetTo(cJSON *jsonObject, const std::string &key, T &value) { - if (jsonObject.find(key) == jsonObject.end()) { + if (!jsonObject || key.empty()) { return CAST_INT(JsonParseError::MISSING_PROP); } - if (!CheckType(jsonObject.at(key), value)) { - return CAST_INT(JsonParseError::TYPE_ERROR); - } - GetValue(jsonObject, key, value); - return CAST_INT(JsonParseError::ERR_OK); - } - - static bool ParseAndGetJsonObject(const std::string &jsonStr, nlohmann::json &root) - { - root = nlohmann::json::parse(jsonStr, nullptr, false); - if (root.is_discarded() || !root.is_object()) { - return false; - } - return true; - } - - static bool ParseAndGetJsonArray(const std::string &jsonStr, nlohmann::json &root) - { - root = nlohmann::json::parse(jsonStr, nullptr, false); - if (root.is_discarded() || !root.is_array()) { - return false; - } - return true; - } - static int32_t GetValueAndSetToArray(const nlohmann::json &jsonObject, const std::string &key, - nlohmann::json &value) - { - if (jsonObject.find(key) == jsonObject.end()) { + cJSON *item = cJSON_GetObjectItemCaseSensitive(jsonObject, key.c_str()); + if (!item) { return CAST_INT(JsonParseError::MISSING_PROP); } - if (!jsonObject.at(key).is_array()) { + + if (!CheckType(item, value)) { return CAST_INT(JsonParseError::TYPE_ERROR); } - jsonObject.at(key).get_to(value); + GetValue(item, value); return CAST_INT(JsonParseError::ERR_OK); } - static void SetJsonToVector(nlohmann::json &jsonObject, std::vector &vector) + static cJSON *ParseAndGetJsonObject(const std::string &jsonStr) { - if (jsonObject.is_array()) { - for (nlohmann::json::iterator it = jsonObject.begin(); it != jsonObject.end(); ++it) { - if (!it.value().is_string()) { - continue; - } - vector.push_back(static_cast(it.value())); - } - } - } - - static void SetJsonToVector(const nlohmann::json &jsonObject, const std::string &key, - std::vector &vector) - { - if (!IsArray(jsonObject, key)) { - return; + cJSON *item = cJSON_Parse(jsonStr.c_str()); + if (!item) { + return nullptr; } - nlohmann::json jsonArray = jsonObject.at(key); - SetJsonToVector(jsonArray, vector); - } - static bool IsArray(const nlohmann::json &jsonObject, const std::string &key) - { - if (jsonObject.find(key) == jsonObject.end()) { - return false; + if (!cJSON_IsObject(item)) + { + cJSON_Delete(item); + return nullptr; } - return jsonObject.at(key).is_array(); - } - template static std::string StructToJsonStr(const T &value) - { - nlohmann::json jsonObj(value); - return jsonObj.dump(); + return item; } - template static int32_t JsonStrToStruct(const std::string &jsonStr, T &value) + static cJSON *ParseAndGetJsonArray(const std::string &jsonStr) { - if (jsonStr.empty()) { - return CAST_INT(JsonParseError::COMMOM_ERROR); + cJSON *item = cJSON_Parse(jsonStr.c_str()); + if (!item) { + return nullptr; } - nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false); - if (!jsonObj.is_discarded() && CheckType(jsonObj, value)) { - value = jsonObj.get(); - return CAST_INT(JsonParseError::ERR_OK); + + if (!cJSON_IsArray(item)) + { + cJSON_Delete(item); + return nullptr; } - return CAST_INT(JsonParseError::TYPE_ERROR); + + return item; } private: - static bool CheckType(const nlohmann::json &jsonObject, std::string &value) + static bool CheckType(cJSON *jsonObject, std::string &value) { - return jsonObject.is_string(); + return jsonObject && cJSON_IsString(jsonObject); } - static bool CheckType(const nlohmann::json &jsonObject, int32_t &value) + static bool CheckType(cJSON *jsonObject, int32_t &value) { - return jsonObject.is_number(); + return jsonObject && cJSON_IsNumber(jsonObject); } - static bool CheckType(const nlohmann::json &jsonObject, uint32_t &value) + static bool CheckType(cJSON *jsonObject, int64_t &value) { - return jsonObject.is_number(); + return jsonObject && cJSON_IsNumber(jsonObject); } - static bool CheckType(const nlohmann::json &jsonObject, uint64_t &value) + static bool CheckType(cJSON *jsonObject, uint32_t &value) { - return jsonObject.is_number(); + return jsonObject && cJSON_IsNumber(jsonObject) && jsonObject->valuedouble >=0; } - static bool CheckType(const nlohmann::json &jsonObject, int64_t &value) + static bool CheckType(cJSON *jsonObject, uint64_t &value) { - return jsonObject.is_number(); + return jsonObject && cJSON_IsNumber(jsonObject) && jsonObject->valuedouble >=0; } - static bool CheckType(const nlohmann::json &jsonObject, double &value) + static bool CheckType(cJSON *jsonObject, bool value) { - return jsonObject.is_number(); + return jsonObject && (jsonObject->type == cJSON_True || jsonObject->type == cJSON_False); } - static bool CheckType(const nlohmann::json &jsonObject, bool &value) + template + static bool CheckType(cJSON *jsonObject, T &value) { - return jsonObject.is_boolean(); + return cJSON_IsObject(jsonObject); } - template static bool CheckType(const nlohmann::json &jsonObject, T &value) + template + static bool CheckType(cJSON *jsonObject, std::vector &value) { - return jsonObject.is_object(); + return cJSON_IsArray(jsonObject); } - template static bool CheckType(const nlohmann::json &jsonObject, std::vector &value) + static void GetValue(const cJSON *jsonArray, std::vector &value) { - return jsonObject.is_array(); - } + if (!jsonArray || !cJSON_IsArray(jsonArray)) { + return; + } - template static void GetValue(const nlohmann::json &jsonObject, const std::string &key, T &value) - { - jsonObject.at(key).get_to(value); + const cJSON *element = nullptr; + cJSON_ArrayForEach(element, jsonArray) { + if (cJSON_IsString(element) && element->valuestring != nullptr) { + value.push_back(element->valuestring); + } + } } - static void GetValue(const nlohmann::json &jsonObject, const std::string &key, std::vector &value) + template + static void GetValue(cJSON *item, T &value) { - if (!IsArray(jsonObject, key)) { - return; - } - nlohmann::json jsonArray = jsonObject.at(key); - for (nlohmann::json::iterator it = jsonArray.begin(); it != jsonArray.end(); ++it) { - if (!it.value().is_string()) { - continue; + if constexpr (std::is_same_v || std::is_same_v) { + if (cJSON_IsNumber(item)) { + value = item->valueint; } - value.push_back(static_cast(it.value())); + } else if constexpr (std::is_same_v || std::is_same_v) { + if (cJSON_IsNumber(item)) { + value = item->valuedouble; + } + } else if constexpr (std::is_same_v || std::is_same_v) { + if (cJSON_IsNumber(item)) { + value = item->valuedouble; + } + } else if constexpr (std::is_same_v) { + if (cJSON_IsTrue(item)) { + value = true; + } else { + value = false; + } + } else if constexpr (std::is_same_v) { + if (cJSON_IsString(item)) { + value = item->valuestring; + } + } else { + return; } + return; } }; } // namespace OHOS::UpdateService -#endif // JSON_UTILS_H \ No newline at end of file +#endif // JSON_UTILS_H diff --git a/foundations/ability/utils/utils.gni b/foundations/ability/utils/utils.gni index b4a33b7d191088b872f6b38bf1e8eb520e036dc4..8c6c450b29b80388371eced54fb3491d3d2e8912 100644 --- a/foundations/ability/utils/utils.gni +++ b/foundations/ability/utils/utils.gni @@ -13,7 +13,7 @@ import("//base/update/updateservice/updateengine.gni") -utils_external_deps = [ "json:nlohmann_json_static" ] +utils_external_deps = [] utils_deps = [] utils_include = [ "$updateengine_root_path/foundations/ability/utils/include" ] utils_src = [] diff --git a/foundations/model/include/business_error.h b/foundations/model/include/business_error.h index 05a2d9884b2f9a4ca3fb2183c1c7d72414ddd965..10a3232d1c1a5720819e0d0a7ab66d563f1a51f8 100644 --- a/foundations/model/include/business_error.h +++ b/foundations/model/include/business_error.h @@ -20,8 +20,6 @@ #include #include -#include "nlohmann/json.hpp" - #include "call_result.h" #include "dupdate_json_utils.h" #include "error_message.h" @@ -50,22 +48,6 @@ struct BusinessError : public Parcelable { return *this; } - friend void to_json(nlohmann::json &jsonObj, const BusinessError &businessError) - { - jsonObj["message"] = businessError.message; - jsonObj["errorNum"] = businessError.errorNum; - jsonObj["data"] = businessError.data; - } - - friend void from_json(const nlohmann::json &jsonObj, BusinessError &businessError) - { - JsonUtils::GetValueAndSetTo(jsonObj, "message", businessError.message); - JsonUtils::GetValueAndSetTo(jsonObj, "data", businessError.data); - int32_t errorNumber = static_cast(CallResult::SUCCESS); - JsonUtils::GetValueAndSetTo(jsonObj, "errorNum", errorNumber); - businessError.errorNum = static_cast(errorNumber); - } - bool ReadFromParcel(Parcel &parcel); bool Marshalling(Parcel &parcel) const override; static BusinessError *Unmarshalling(Parcel &parcel); diff --git a/foundations/model/include/error_message.h b/foundations/model/include/error_message.h index 5cf7128650ad3f4009f73ba915d66459076ef28b..b9acfa76fc16d6761ca4fa571f2a9f5624dc0ee8 100644 --- a/foundations/model/include/error_message.h +++ b/foundations/model/include/error_message.h @@ -19,8 +19,6 @@ #include #include -#include "nlohmann/json.hpp" - #include "dupdate_json_utils.h" #include "json_builder.h" @@ -31,18 +29,6 @@ struct ErrorMessage : public Parcelable { int32_t errorCode = 0; std::string errorMessage; - friend void to_json(nlohmann::json &jsonObj, const ErrorMessage &message) - { - jsonObj["errorCode"] = message.errorCode; - jsonObj["errorMessage"] = message.errorMessage; - } - - friend void from_json(const nlohmann::json &jsonObj, ErrorMessage &message) - { - JsonUtils::GetValueAndSetTo(jsonObj, "errorCode", message.errorCode); - JsonUtils::GetValueAndSetTo(jsonObj, "errorMessage", message.errorMessage); - } - JsonBuilder GetJsonBuilder() { return JsonBuilder() diff --git a/frameworks/js/napi/update/BUILD.gn b/frameworks/js/napi/update/BUILD.gn index 83dd6bfefe313f0104e75c7e282ccd63e63ee0a2..f5f0e9a44d350f63f7d9c6145537e8798fb0304f 100644 --- a/frameworks/js/napi/update/BUILD.gn +++ b/frameworks/js/napi/update/BUILD.gn @@ -50,9 +50,9 @@ ohos_shared_library("$updateengine_client_library_name") { "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "c_utils:utils", # sptr + "cJSON:cjson", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", "napi:ace_napi", ] diff --git a/interfaces/inner_api/engine/BUILD.gn b/interfaces/inner_api/engine/BUILD.gn index d289aa1074469bd769b6d8a6ac13f02bd4149015..f650da18541300894e73fc9646c6317d85c232a7 100644 --- a/interfaces/inner_api/engine/BUILD.gn +++ b/interfaces/inner_api/engine/BUILD.gn @@ -86,9 +86,9 @@ ohos_shared_library("$updateengine_inner_library_name") { external_deps = [ "bounds_checking_function:libsec_static", "c_utils:utils", # sptr + "cJSON:cjson", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/services/core/ability/adapter/src/config_parse.cpp b/services/core/ability/adapter/src/config_parse.cpp index 7dc800aeb25a52bbe84b2da8f0e822cac34ebdee..712f8d50bc19faf7bf90d30bdb5dfe858f37954b 100644 --- a/services/core/ability/adapter/src/config_parse.cpp +++ b/services/core/ability/adapter/src/config_parse.cpp @@ -63,8 +63,8 @@ void ConfigParse::LoadConfigInfo() std::string rawJson(streambuffer.str()); readFile.close(); - nlohmann::json root = nlohmann::json::parse(rawJson, nullptr, false); - if (root.is_discarded()) { + cJSON *root = cJSON_Parse(rawJson.c_str()); + if (!root) { ENGINE_LOGE("json Create error!"); return; } @@ -72,6 +72,7 @@ void ConfigParse::LoadConfigInfo() JsonUtils::GetValueAndSetTo(root, "abInstallTimeout", configInfo_.abInstallTimeout); JsonUtils::GetValueAndSetTo(root, "streamInstallTimeout", configInfo_.streamInstallTimeout); JsonUtils::GetValueAndSetTo(root, "moduleLibPath", configInfo_.moduleLibPath); + cJSON_Delete(root); } } // namespace UpdateService } // namespace OHOS diff --git a/services/core/ability/model/include/device_info.h b/services/core/ability/model/include/device_info.h index ea146030ad36d5e9dc9c90bca9b04019159c1f93..67b2a5503b729f691bae1ed736ea066eb7733710 100644 --- a/services/core/ability/model/include/device_info.h +++ b/services/core/ability/model/include/device_info.h @@ -18,8 +18,6 @@ #include -#include "nlohmann/json.hpp" - #include "anonymous_utils.h" namespace OHOS { @@ -28,14 +26,6 @@ struct DeviceInfo { public: std::string udid; std::string deviceId; - - nlohmann::ordered_json ToJson(bool isPrint) - { - nlohmann::ordered_json json = nlohmann::ordered_json::object(); - json["udid"] = isPrint ? AnonymousUtils::AnonymousString(udid) : udid; - json["deviceId"] = isPrint ? AnonymousUtils::AnonymousString(deviceId) : deviceId; - return json; - } }; } // namespace UpdateService } // namespace OHOS diff --git a/services/engine/engine_sa.gni b/services/engine/engine_sa.gni index 74a5bb2327b77b825bf96f404dde3ed55f0dd6fa..c32c2dba2c471454c361b571cf53c91ae62ba741 100644 --- a/services/engine/engine_sa.gni +++ b/services/engine/engine_sa.gni @@ -138,7 +138,6 @@ sa_external_deps = [ "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_core", - "json:nlohmann_json_static", "libxml2:libxml2", "mbedtls:mbedtls_shared", "openssl:libcrypto_shared", diff --git a/services/engine/include/update_service_impl_manager.h b/services/engine/include/update_service_impl_manager.h index 4827ed138f6995181838e63f7c3ddc565a5398da..256cacf0a69284f14dab530d1d170095bdcb2bcc 100644 --- a/services/engine/include/update_service_impl_manager.h +++ b/services/engine/include/update_service_impl_manager.h @@ -18,6 +18,7 @@ #include "iservice_online_updater.h" +#include #include #include "upgrade_info.h" diff --git a/services/firmware/check/include/firmware_icheck.h b/services/firmware/check/include/firmware_icheck.h index 0f4041e8bd949e3e501ab1c6b852d1cae2549c5f..0cadca3c63476604635172bea84cacf95b5d4ab8 100644 --- a/services/firmware/check/include/firmware_icheck.h +++ b/services/firmware/check/include/firmware_icheck.h @@ -22,7 +22,6 @@ #include #include "cJSON.h" -#include "nlohmann/json.hpp" #include "openssl/err.h" #include "openssl/ssl.h" #include "parameter.h" @@ -107,10 +106,11 @@ public: if (response.status != static_cast(HttpConstant::SUCCESS) || response.content.empty()) { checkStatus = CheckStatus::CHECK_FAIL; } else { - nlohmann::json root = nlohmann::json::parse(response.content, nullptr, false); - if (!root.is_discarded()) { - FIRMWARE_LONG_LOGI("FirmwareCheck response: %{public}s", root.dump().c_str()); + cJSON *root = cJSON_Parse(response.content.c_str()); + if (!root) { + FIRMWARE_LONG_LOGI("FirmwareCheck response: %{public}s", response.content.c_str()); } + cJSON_Delete(root); FirmwareCheckAnalyzeUtils().DoAnalyze(response.content, checkResultList_, duration_, checkAndAuthInfo_); checkStatus = CheckStatus::CHECK_SUCCESS; } diff --git a/services/firmware/common/include/firmware_common.h b/services/firmware/common/include/firmware_common.h index ce001cecaf9e1eaed9e67c8000a9fc29edc6e9a2..3ac697f4bff4538c17570e032d3b9a933b9ca184 100644 --- a/services/firmware/common/include/firmware_common.h +++ b/services/firmware/common/include/firmware_common.h @@ -18,8 +18,6 @@ #include -#include "nlohmann/json.hpp" - #include "anonymous_utils.h" #include "constant.h" #include "firmware_component.h" @@ -76,14 +74,6 @@ public: std::string versionNumber; std::string displayVersionNumber; std::string versionPackageType; - - nlohmann::ordered_json ToCheckJson() - { - nlohmann::ordered_json json = nlohmann::ordered_json::object(); - json["versionPackageType"] = versionPackageType; - json["versionNumber"] = versionNumber; - return json; - } }; enum VersionIndex { @@ -101,15 +91,6 @@ struct UpdatePackage { std::string versionId; int32_t packageIndex = 0; PackageType versionPackageType = PackageType::DYNAMIC; - - nlohmann::ordered_json ToJson() - { - nlohmann::ordered_json json = nlohmann::ordered_json::object(); - json["versionId"] = versionId; - json["packageIndex"] = packageIndex; - json["versionPackageType"] = CAST_INT(versionPackageType); - return json; - } }; struct TargetBlComponent { diff --git a/services/firmware/common/include/firmware_update_helper.h b/services/firmware/common/include/firmware_update_helper.h index 2545624453f8e128e9e9e5771c95e58aa9dee3b0..3d4d3f82cf0fe5d09df0c67c330b774504a1183a 100644 --- a/services/firmware/common/include/firmware_update_helper.h +++ b/services/firmware/common/include/firmware_update_helper.h @@ -18,8 +18,6 @@ #include -#include "nlohmann/json.hpp" - #include "current_version_info.h" #include "firmware_changelog_utils.h" #include "firmware_constant.h" diff --git a/services/firmware/utils/include/firmware_check_analyze_utils.h b/services/firmware/utils/include/firmware_check_analyze_utils.h index 40005a709e1ac1ecc4cd5a121d4a20380ee3e433..16700f490aed70241893bbef6f1f9cabb1cc7257 100644 --- a/services/firmware/utils/include/firmware_check_analyze_utils.h +++ b/services/firmware/utils/include/firmware_check_analyze_utils.h @@ -35,10 +35,10 @@ public: private: bool IsLegalStatus(int32_t status); - int32_t AnalyzeComponents(nlohmann::json &root); - int32_t ProcessCheckResults(const nlohmann::json &checkResults); - int32_t ProcessDescriptInfo(const nlohmann::json &descriptInfo); - int32_t AnalyzeBlVersionCheckResults(nlohmann::json &root, BlCheckResponse &response); + int32_t AnalyzeComponents(cJSON *root); + int32_t ProcessCheckResults(cJSON *checkResults); + int32_t ProcessDescriptInfo(cJSON *descriptInfo); + int32_t AnalyzeBlVersionCheckResults(cJSON *root, BlCheckResponse &response); private: std::vector components_; diff --git a/services/firmware/utils/src/firmware_check_analyze_utils.cpp b/services/firmware/utils/src/firmware_check_analyze_utils.cpp index 1c5099ac208dbe4b5f97dd10ec394fa07bf6fd2c..bc188faf79af06d2c431f4da044c2dae88a32944 100644 --- a/services/firmware/utils/src/firmware_check_analyze_utils.cpp +++ b/services/firmware/utils/src/firmware_check_analyze_utils.cpp @@ -37,8 +37,8 @@ void FirmwareCheckAnalyzeUtils::DoAnalyze(const std::string &rawJson, std::vecto { BlCheckResponse response; int32_t ret = CAST_INT(JsonParseError::ERR_OK); - nlohmann::json root; - if (!JsonUtils::ParseAndGetJsonObject(rawJson, root)) { + cJSON *root = JsonUtils::ParseAndGetJsonObject(rawJson); + if (!root) { FIRMWARE_LOGE("fail to parse out a json object"); return; } @@ -49,6 +49,7 @@ void FirmwareCheckAnalyzeUtils::DoAnalyze(const std::string &rawJson, std::vecto checkAndAuthInfo.responseStatus = std::to_string(status); if (!IsLegalStatus(status)) { FIRMWARE_LOGI("not found new version!"); + cJSON_Delete(root); return; } if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) { @@ -60,100 +61,118 @@ void FirmwareCheckAnalyzeUtils::DoAnalyze(const std::string &rawJson, std::vecto if (ret == CAST_INT(JsonParseError::ERR_OK)) { components = components_; } + cJSON_Delete(root); } -int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(nlohmann::json &root, BlCheckResponse &response) +int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(cJSON *root, BlCheckResponse &response) { - if (root.find("checkResults") == root.end()) { + cJSON *itemCheckResults = cJSON_GetObjectItemCaseSensitive(root, "checkResults"); + if (!itemCheckResults) { FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults no key checkResults"); return CAST_INT(JsonParseError::MISSING_PROP); } - FIRMWARE_LOGI("checkResults size is %{public}" PRIu64 "", static_cast(root["checkResults"].size())); + + FIRMWARE_LOGI("checkResults size is %{public}" PRIu64 "", + static_cast(cJSON_IsArray(itemCheckResults) ? cJSON_GetArraySize(itemCheckResults) : 0)); + int32_t ret = CAST_INT(JsonParseError::ERR_OK); - for (auto &result : root["checkResults"]) { - int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR); - JsonUtils::GetValueAndSetTo(root, "searchStatus", status); - if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) { + int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR); + JsonUtils::GetValueAndSetTo(root, "searchStatus", status); + if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) + { + for (int i = 0; i < cJSON_GetArraySize(itemCheckResults); i++) + { + cJSON *item = cJSON_GetArrayItem(itemCheckResults, i); BlVersionCheckResult checkResult; - ret += JsonUtils::GetValueAndSetTo(result, "descriptPackageId", checkResult.descriptPackageId); + ret += JsonUtils::GetValueAndSetTo(item, "descriptPackageId", checkResult.descriptPackageId); checkResult.blVersionType = 1; checkResult.status = std::to_string(status); UpdatePackage package; package.versionId = "1"; int32_t versionPackageType = CAST_INT(PackageType::DYNAMIC); - ret += JsonUtils::GetValueAndSetTo(result, "packageType", versionPackageType); + ret += JsonUtils::GetValueAndSetTo(item, "packageType", versionPackageType); package.versionPackageType = static_cast(versionPackageType); package.packageIndex = 0; checkResult.updatePackages.push_back(package); TargetBlComponent component; component.versionPackageType = package.versionPackageType; - ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.displayVersionNumber); - ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.versionNumber); + ret += JsonUtils::GetValueAndSetTo(item, "versionName", component.displayVersionNumber); + ret += JsonUtils::GetValueAndSetTo(item, "versionName", component.versionNumber); checkResult.targetBlComponents.push_back(component); - checkResult.blVersionInfo = result["blVersionInfo"].dump(); + JsonUtils::GetValueAndSetTo(item, "blVersionInfo", checkResult.blVersionInfo); response.blVersionCheckResults.push_back(checkResult); Version version; version.versionId = "1"; - ret += JsonUtils::GetValueAndSetTo(result, "versionCode", version.versionNumber); - ret += JsonUtils::GetValueAndSetTo(result, "url", version.url); + ret += JsonUtils::GetValueAndSetTo(item, "versionCode", version.versionNumber); + ret += JsonUtils::GetValueAndSetTo(item, "url", version.url); response.versionList.push_back(version); } } return ret; } -int32_t FirmwareCheckAnalyzeUtils::AnalyzeComponents(nlohmann::json &root) +int32_t FirmwareCheckAnalyzeUtils::AnalyzeComponents(cJSON *root) { // 检查 "checkResults" 是否存在 - if (root.find("checkResults") == root.end()) { + cJSON *itemCheckResults = cJSON_GetObjectItemCaseSensitive(root, "checkResults"); + if (!itemCheckResults) { FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key checkResults"); return CAST_INT(JsonParseError::MISSING_PROP); } - FIRMWARE_LOGI("checkResults size is %{public}" PRIu64 "", static_cast(root["checkResults"].size())); + FIRMWARE_LOGI("checkResults size is %{public}" PRIu64 "", + static_cast(cJSON_IsArray(itemCheckResults) ? cJSON_GetArraySize(itemCheckResults) : 0)); // 初始化返回值 int32_t ret = CAST_INT(JsonParseError::ERR_OK); // 处理 "checkResults" 部分 - ret += ProcessCheckResults(root["checkResults"]); + ret += ProcessCheckResults(itemCheckResults); // 检查 "descriptInfo" 是否存在 - if (root.find("descriptInfo") == root.end()) { - FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key descriptInfo"); + cJSON *itemDescriptInfo = cJSON_GetObjectItemCaseSensitive(root, "descriptInfo"); + if (!itemDescriptInfo) { + FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key descriptInfo"); return CAST_INT(JsonParseError::MISSING_PROP); } // 处理 "descriptInfo" 部分 - ret += ProcessDescriptInfo(root["descriptInfo"]); + ret += ProcessDescriptInfo(itemDescriptInfo); return ret; } -int32_t FirmwareCheckAnalyzeUtils::ProcessCheckResults(const nlohmann::json &checkResults) +int32_t FirmwareCheckAnalyzeUtils::ProcessCheckResults(cJSON *checkResults) { int32_t ret = CAST_INT(JsonParseError::ERR_OK); std::string componentId; - for (auto &result : checkResults) { + if (!checkResults) { + FIRMWARE_LOGE("AnalyzeComponents no key checkResults"); + return CAST_INT(JsonParseError::MISSING_PROP); + } + + for (int i = 0; i < cJSON_GetArraySize(checkResults); i++) { + cJSON *itemResult = cJSON_GetArrayItem(checkResults, i); + FirmwareComponent component; int32_t componetSize = 0; // 获取组件相关属性 - ret += JsonUtils::GetValueAndSetTo(result, "descriptPackageId", component.descriptPackageId); - ret += JsonUtils::GetValueAndSetTo(result, "url", component.url); - ret += JsonUtils::GetValueAndSetTo(result, "size", componetSize); + ret += JsonUtils::GetValueAndSetTo(itemResult, "descriptPackageId", component.descriptPackageId); + ret += JsonUtils::GetValueAndSetTo(itemResult, "url", component.url); + ret += JsonUtils::GetValueAndSetTo(itemResult, "size", componetSize); component.size = static_cast(componetSize); component.fileName = StringUtils::GetLastSplitString(component.url, "/"); - ret += JsonUtils::GetValueAndSetTo(result, "verifyInfo", component.verifyInfo); - ret += JsonUtils::GetValueAndSetTo(result, "versionCode", component.versionNumber); - ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.targetBlVersionNumber); + ret += JsonUtils::GetValueAndSetTo(itemResult, "verifyInfo", component.verifyInfo); + ret += JsonUtils::GetValueAndSetTo(itemResult, "versionCode", component.versionNumber); + ret += JsonUtils::GetValueAndSetTo(itemResult, "versionName", component.targetBlVersionNumber); int32_t versionPackageType = CAST_INT(PackageType::DYNAMIC); - ret += JsonUtils::GetValueAndSetTo(result, "packageType", versionPackageType); + ret += JsonUtils::GetValueAndSetTo(itemResult, "packageType", versionPackageType); component.versionPackageType = static_cast(versionPackageType); int32_t otaType = CAST_INT(OtaType::REGULAR); - ret += JsonUtils::GetValueAndSetTo(result, "otaType", otaType); + ret += JsonUtils::GetValueAndSetTo(itemResult, "otaType", otaType); component.otaType = static_cast(otaType); component.targetBlDisplayVersionNumber = component.targetBlVersionNumber; @@ -163,23 +182,28 @@ int32_t FirmwareCheckAnalyzeUtils::ProcessCheckResults(const nlohmann::json &che components_.push_back(component); } - return ret; } -int32_t FirmwareCheckAnalyzeUtils::ProcessDescriptInfo(const nlohmann::json &descriptInfo) +int32_t FirmwareCheckAnalyzeUtils::ProcessDescriptInfo(cJSON *descriptInfo) { int32_t ret = CAST_INT(JsonParseError::ERR_OK); std::string componentId = components_.empty() ? "" : components_.back().descriptPackageId; - for (auto &info : descriptInfo) { + if (!descriptInfo) { + FIRMWARE_LOGE("ProcessDescriptInfo no descriptInfo"); + return CAST_INT(JsonParseError::MISSING_PROP); + } + + for (int i = 0; i < cJSON_GetArraySize(descriptInfo); i++) { + cJSON *itemInfo = cJSON_GetArrayItem(descriptInfo, i); int32_t descriptInfoType; std::string descContent; std::string subString = "quota"; std::string replString = "\""; - ret += JsonUtils::GetValueAndSetTo(info, "descriptionType", descriptInfoType); - ret += JsonUtils::GetValueAndSetTo(info, "content", descContent); + ret += JsonUtils::GetValueAndSetTo(itemInfo, "descriptionType", descriptInfoType); + ret += JsonUtils::GetValueAndSetTo(itemInfo, "content", descContent); StringUtils::ReplaceStringAll(descContent, subString, replString); diff --git a/services/startup/manage/include/schedule_config.h b/services/startup/manage/include/schedule_config.h index 5a4e9bb31841f00029b0eea2682cf2710b9105b3..23ef1c735f48cbfbb3020397bb678b6dc20416d3 100644 --- a/services/startup/manage/include/schedule_config.h +++ b/services/startup/manage/include/schedule_config.h @@ -28,7 +28,7 @@ public: static uint64_t GetIdleCheckInterval(); private: - static uint64_t ParseConfig(nlohmann::json &root, const std::string &key, uint64_t defaultValue); + static uint64_t ParseConfig(cJSON *root, const std::string &key, uint64_t defaultValue); private: static uint64_t pullupInterval_; diff --git a/services/startup/manage/src/schedule_config.cpp b/services/startup/manage/src/schedule_config.cpp index 2a1c1789ddafe18b5289c38c43408852beae5445..29750d8514493de1d5474bbb9e83d2c16090efee 100644 --- a/services/startup/manage/src/schedule_config.cpp +++ b/services/startup/manage/src/schedule_config.cpp @@ -27,9 +27,8 @@ uint64_t ScheduleConfig::idleCheckInterval_ = Startup::IDLE_CHECK_INTERVAL; void ScheduleConfig::InitConfig() { ENGINE_LOGI("InitConfig"); - nlohmann::json root = - nlohmann::json::parse(FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH), nullptr, false); - if (root.is_discarded()) { + cJSON* root = cJSON_Parse(FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH).c_str()); + if (!root) { ENGINE_LOGI("InitConfig load fail"); return; } @@ -37,6 +36,7 @@ void ScheduleConfig::InitConfig() idleCheckInterval_ = ParseConfig(root, Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL); ENGINE_LOGI("InitConfig pullupInterval: %{public}s, idleCheckInterval: %{public}s", std::to_string(pullupInterval_).c_str(), std::to_string(idleCheckInterval_).c_str()); + cJSON_Delete(root); } uint64_t ScheduleConfig::GetPullupInterval() @@ -49,7 +49,7 @@ uint64_t ScheduleConfig::GetIdleCheckInterval() return idleCheckInterval_; } -uint64_t ScheduleConfig::ParseConfig(nlohmann::json &root, const std::string &key, uint64_t defaultValue) +uint64_t ScheduleConfig::ParseConfig(cJSON *root, const std::string &key, uint64_t defaultValue) { uint64_t value = 0; int32_t ret = JsonUtils::GetValueAndSetTo(root, key, value);