From 54bb8511ab36c11a79e39c96303ea5cee29cbe1d Mon Sep 17 00:00:00 2001 From: chengshichang Date: Sat, 21 Jun 2025 11:41:19 +0800 Subject: [PATCH 1/2] Signed-off-by: csc --- bundle.json | 1 - frameworks/BUILD.gn | 4 - .../src/js_work_scheduler_extension.cpp | 5 +- frameworks/include/work_info.h | 16 +- frameworks/src/work_info.cpp | 143 +++++++++--------- frameworks/test/unittest/BUILD.gn | 2 - .../test/unittest/src/workinfo_test.cpp | 16 +- .../native/include/work_scheduler_service.h | 2 +- .../native/src/work_scheduler_service.cpp | 92 +++++------ services/test/BUILD.gn | 1 - .../test/src/workschedulerservice_test.cpp | 3 +- services/zidl/BUILD.gn | 2 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../workschedulerstartwork_fuzzer/BUILD.gn | 1 - .../BUILD.gn | 1 - .../BUILD.gn | 1 - .../workschedulerstopwork_fuzzer/BUILD.gn | 1 - .../workscheduleservice_fuzzer/BUILD.gn | 1 - utils/native/BUILD.gn | 1 - utils/native/include/work_sched_constants.h | 1 + 26 files changed, 129 insertions(+), 172 deletions(-) diff --git a/bundle.json b/bundle.json index fec03c0..93f12a0 100644 --- a/bundle.json +++ b/bundle.json @@ -48,7 +48,6 @@ "data_share", "config_policy", "hiview", - "jsoncpp", "hicollie", "hitrace", "power_manager", diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index d4c8782..0b8233b 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -60,8 +60,6 @@ ohos_shared_library("workschedclient") { "${worksched_utils_path}:workschedutils", ] - public_external_deps = [ "jsoncpp:jsoncpp" ] - external_deps = [ "ability_base:want", "hilog:libhilog", @@ -93,7 +91,6 @@ ohos_source_set("work_sched_service_proxy") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "jsoncpp:jsoncpp", ] subsystem_name = "resourceschedule" part_name = "${worksched_native_part_name}" @@ -118,7 +115,6 @@ ohos_source_set("work_sched_service_stub") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "jsoncpp:jsoncpp", ] subsystem_name = "resourceschedule" part_name = "${worksched_native_part_name}" diff --git a/frameworks/extension/src/js_work_scheduler_extension.cpp b/frameworks/extension/src/js_work_scheduler_extension.cpp index c3b96be..d1ba868 100644 --- a/frameworks/extension/src/js_work_scheduler_extension.cpp +++ b/frameworks/extension/src/js_work_scheduler_extension.cpp @@ -479,7 +479,7 @@ void JsWorkSchedulerExtension::GetSrcPath(std::string &srcPath) bool JsWorkSchedulerExtension::GetExtrasJsonStr(const WorkInfo& workInfo, std::string& extrasStr) { std::shared_ptr extras = workInfo.GetExtras(); - Json::Value extrasJson; + nlohmann::json extrasJson; if (!extras) { WS_HILOGD("parameter is null."); return false; @@ -495,8 +495,7 @@ bool JsWorkSchedulerExtension::GetExtrasJsonStr(const WorkInfo& workInfo, std::s WS_HILOGE("parameters type not supported."); } } - Json::StreamWriterBuilder builder; - extrasStr = Json::writeString(builder, extrasJson); + extrasStr = extrasJson.dump(JSON_INDENT_WIDTH); return true; } } // namespace WorkScheduler diff --git a/frameworks/include/work_info.h b/frameworks/include/work_info.h index a25c969..f97df3a 100644 --- a/frameworks/include/work_info.h +++ b/frameworks/include/work_info.h @@ -22,11 +22,11 @@ #include #include -#include "json/json.h" #include "want_params.h" #include "refbase.h" #include "work_condition.h" +#include "nlohmann/json.hpp" namespace OHOS { namespace WorkScheduler { @@ -295,14 +295,14 @@ public: * @param value The value. * @return True if success,else false. */ - bool ParseFromJson(const Json::Value &value); + bool ParseFromJson(const nlohmann::json &value); /** * @brief Parse element from json. * * @param value The value. * @return True if success,else false. */ - bool ParseElementFromJson(const Json::Value &value); + bool ParseElementFromJson(const nlohmann::json &value); /** * @brief Set preinstalled flag. */ @@ -380,11 +380,11 @@ private: bool isInnerApply_ {false}; private: static bool UnmarshallCondition(Parcel &parcel, WorkInfo* read, uint32_t mapsize); - void ParseConditionToJsonStr(Json::Value &root); - void ParseConditionFromJsonStr(const Json::Value &value); - void ParseParametersFromJsonStr(const Json::Value &value); - void ParseTimerFormJsonStr(const Json::Value &conditions); - bool IsHasBoolProp(const Json::Value &value, const std::string &key); + void ParseConditionToJsonStr(nlohmann::json &root); + void ParseConditionFromJsonStr(const nlohmann::json &value); + void ParseParametersFromJsonStr(const nlohmann::json &value); + void ParseTimerFormJsonStr(const nlohmann::json &conditions); + bool IsHasBoolProp(const nlohmann::json &value, const std::string &key); }; } // namespace WorkScheduler } // namespace OHOS diff --git a/frameworks/src/work_info.cpp b/frameworks/src/work_info.cpp index 203160b..a5270d3 100644 --- a/frameworks/src/work_info.cpp +++ b/frameworks/src/work_info.cpp @@ -15,10 +15,10 @@ #include "work_info.h" #include "work_sched_hilog.h" +#include "work_sched_constants.h" namespace OHOS { namespace WorkScheduler { -const int32_t INVALID_VALUE = -1; const int32_t INVALID_TIME_VALUE = 0; const uint32_t MAX_SIZE = 1024; const int32_t APPINDEX_INIT_VALUE = 0; @@ -463,7 +463,7 @@ bool WorkInfo::UnmarshallCondition(Parcel &parcel, WorkInfo* read, uint32_t maps std::string WorkInfo::ParseToJsonStr() { - Json::Value root; + nlohmann::json root; if (uid_ != INVALID_VALUE) { root["uid"] = uid_; } @@ -483,8 +483,8 @@ std::string WorkInfo::ParseToJsonStr() root["uriKey"] = uriKey_; ParseConditionToJsonStr(root); if (extras_) { - Json::Value extras; - Json::Value extrasType; + nlohmann::json extras; + nlohmann::json extrasType; std::map> extrasMap = extras_->GetParams(); int typeId = INVALID_VALUE; for (auto it : extrasMap) { @@ -500,17 +500,13 @@ std::string WorkInfo::ParseToJsonStr() root["parameters"] = extras; root["parametersType"] = extrasType; } - Json::StreamWriterBuilder writerBuilder; - std::ostringstream os; - std::unique_ptr jsonWriter(writerBuilder.newStreamWriter()); - jsonWriter->write(root, &os); - std::string result = os.str(); + std::string result = root.dump(JSON_INDENT_WIDTH); return result; } -void WorkInfo::ParseConditionToJsonStr(Json::Value &root) +void WorkInfo::ParseConditionToJsonStr(nlohmann::json &root) { - Json::Value conditions; + nlohmann::json conditions; for (auto it : conditionMap_) { switch (it.first) { case WorkCondition::Type::NETWORK: { @@ -553,126 +549,125 @@ void WorkInfo::ParseConditionToJsonStr(Json::Value &root) root["conditions"] = conditions; } -bool WorkInfo::ParseFromJson(const Json::Value &value) +bool WorkInfo::ParseFromJson(const nlohmann::json &value) { - if (value.empty()) { + if (value.is_null() || value.empty()) { WS_HILOGE("workinfo json is empty"); return false; } - if (!value.isMember("workId") || !value["workId"].isInt()) { + if (!value.contains("workId") || !value["workId"].is_number_integer()) { WS_HILOGE("workinfo json is invalid, workId is missing or not int"); return false; } - this->workId_ = value["workId"].asInt(); - if ((value.isMember("saId") && value["saId"].isInt()) && IsHasBoolProp(value, "residentSa")) { - this->saId_ = value["saId"].asInt(); - this->residentSa_ = value["residentSa"].asBool(); + this->workId_ = value["workId"].get(); + if ((value.contains("saId") && value["saId"].is_number_integer()) && IsHgetProp(value, "residentSa")) { + this->saId_ = value["saId"].get(); + this->residentSa_ = value["residentSa"].get(); } if (!ParseElementFromJson(value)) { return false; } - if (IsHasBoolProp(value, "persisted")) { - this->persisted_ = value["persisted"].asBool(); + if (IsHgetProp(value, "persisted")) { + this->persisted_ = value["persisted"].get(); } - if (IsHasBoolProp(value, "preinstalled")) { - this->preinstalled_ = value["preinstalled"].asBool(); + if (IsHgetProp(value, "preinstalled")) { + this->preinstalled_ = value["preinstalled"].get(); } - if (value.isMember("uriKey") && value["uriKey"].isString()) { - this->uriKey_ = value["uriKey"].asString(); + if (value.contains("uriKey") && value["uriKey"].is_string()) { + this->uriKey_ = value["uriKey"].get(); } - if (IsHasBoolProp(value, "callBySystemApp")) { - this->callBySystemApp_ = value["callBySystemApp"].asBool(); + if (IsHgetProp(value, "callBySystemApp")) { + this->callBySystemApp_ = value["callBySystemApp"].get(); } - if (value.isMember("appIndex") && value["appIndex"].isInt()) { - this->appIndex_ = value["appIndex"].asInt(); + if (value.contains("appIndex") && value["appIndex"].is_number_integer()) { + this->appIndex_ = value["appIndex"].get(); } - if (IsHasBoolProp(value, "extension")) { - this->extension_ = value["extension"].asBool(); + if (IsHgetProp(value, "extension")) { + this->extension_ = value["extension"].get(); } ParseConditionFromJsonStr(value); - if (!value.isMember("parameters")) { + if (!value.contains("parameters")) { return true; } ParseParametersFromJsonStr(value); return true; } -bool WorkInfo::ParseElementFromJson(const Json::Value &value) +bool WorkInfo::ParseElementFromJson(const nlohmann::json &value) { if (!IsSA()) { - if (!value.isMember("bundleName") || !value["bundleName"].isString() || - !value.isMember("abilityName") || !value["abilityName"].isString()) { + if (!value.contains("bundleName") || !value["bundleName"].is_string() || + !value.contains("abilityName") || !value["abilityName"].is_string()) { WS_HILOGE("workinfo json is invalid, bundleName or abilityName is missing or not string"); return false; } - this->bundleName_ = value["bundleName"].asString(); - this->abilityName_ = value["abilityName"].asString(); + this->bundleName_ = value["bundleName"].get(); + this->abilityName_ = value["abilityName"].get(); } return true; } -void WorkInfo::ParseParametersFromJsonStr(const Json::Value &value) +void WorkInfo::ParseParametersFromJsonStr(const nlohmann::json &value) { - Json::Value extrasJson = value["parameters"]; - Json::Value extrasType = value["parametersType"]; + nlohmann::json extrasJson = value["parameters"]; + nlohmann::json extrasType = value["parametersType"]; AAFwk::WantParams extras; - Json::Value::Members keyList = extrasJson.getMemberNames(); int typeId = INVALID_VALUE; - for (auto key : keyList) { - if (extrasType[key].isInt()) { - typeId = extrasType[key].asInt(); + for (const auto &[key, parameter] : extrasJson.items()) { + if (extrasType[key].is_number_integer()) { + typeId = extrasType[key].get(); } - if (typeId != INVALID_VALUE && extrasJson[key].isString()) { + if (typeId != INVALID_VALUE && extrasJson[key].is_string()) { sptr exInterface = AAFwk::WantParams::GetInterfaceByType(typeId, - extrasJson[key].asString()); + extrasJson[key].get()); extras.SetParam(key, exInterface); } } this->RequestExtras(extras); } -void WorkInfo::ParseConditionFromJsonStr(const Json::Value &value) +void WorkInfo::ParseConditionFromJsonStr(const nlohmann::json &value) { - if (value.isMember("uid") && value["uid"].isInt()) { - this->uid_ = value["uid"].asInt(); + if (value.contains("uid") && value["uid"].is_number_integer()) { + this->uid_ = value["uid"].get(); } - Json::Value conditions = value["conditions"]; - if (conditions.isMember("network") && conditions["network"].isInt()) { - this->RequestNetworkType(WorkCondition::Network(conditions["network"].asInt())); + nlohmann::json conditions = value["conditions"]; + if (conditions.contains("network") && conditions["network"].is_number_integer()) { + this->RequestNetworkType(WorkCondition::Network(conditions["network"].get())); } - if (conditions.isMember("isCharging") && conditions["isCharging"].isBool() && - conditions.isMember("chargerType") && conditions["chargerType"].isInt()) { - this->RequestChargerType(conditions["isCharging"].asBool(), - WorkCondition::Charger(conditions["chargerType"].asInt())); + if (conditions.contains("isCharging") && conditions["isCharging"].is_boolean() && + conditions.contains("chargerType") && conditions["chargerType"].is_number_integer()) { + this->RequestChargerType(conditions["isCharging"].get(), + WorkCondition::Charger(conditions["chargerType"].get())); } - if (conditions.isMember("batteryLevel") && conditions["batteryLevel"].isInt()) { - this->RequestBatteryLevel(conditions["batteryLevel"].asInt()); + if (conditions.contains("batteryLevel") && conditions["batteryLevel"].is_number_integer()) { + this->RequestBatteryLevel(conditions["batteryLevel"].get()); } - if (conditions.isMember("batteryStatus") && conditions["batteryStatus"].isInt()) { - this->RequestBatteryStatus(WorkCondition::BatteryStatus(conditions["batteryStatus"].asInt())); + if (conditions.contains("batteryStatus") && conditions["batteryStatus"].is_number_integer()) { + this->RequestBatteryStatus(WorkCondition::BatteryStatus(conditions["batteryStatus"].get())); } - if (conditions.isMember("storage") && conditions["storage"].isInt()) { - this->RequestStorageLevel(WorkCondition::Storage(conditions["storage"].asInt())); + if (conditions.contains("storage") && conditions["storage"].is_number_integer()) { + this->RequestStorageLevel(WorkCondition::Storage(conditions["storage"].get())); } - if (conditions.isMember("isDeepIdle") && conditions["isDeepIdle"].isBool()) { - this->RequestDeepIdle(conditions["isDeepIdle"].asBool()); + if (conditions.contains("isDeepIdle") && conditions["isDeepIdle"].is_boolean()) { + this->RequestDeepIdle(conditions["isDeepIdle"].get()); } ParseTimerFormJsonStr(conditions); } -void WorkInfo::ParseTimerFormJsonStr(const Json::Value &conditions) +void WorkInfo::ParseTimerFormJsonStr(const nlohmann::json &conditions) { - if (conditions.isMember("timer") && conditions["timer"].isInt() && - conditions.isMember("repeat") && conditions["repeat"].isBool()) { - if (conditions["repeat"].asBool()) { - this->RequestRepeatCycle(conditions["timer"].asInt()); + if (conditions.contains("timer") && conditions["timer"].is_number_integer() && + conditions.contains("repeat") && conditions["repeat"].is_boolean()) { + if (conditions["repeat"].get()) { + this->RequestRepeatCycle(conditions["timer"].get()); } else { - if (conditions.isMember("cycle") && conditions["cycle"].isInt()) { - this->RequestRepeatCycle(conditions["timer"].asInt(), conditions["cycle"].asInt()); + if (conditions.contains("cycle") && conditions["cycle"].is_number_integer()) { + this->RequestRepeatCycle(conditions["timer"].get(), conditions["cycle"].get()); } } - if (conditions.isMember("baseTime") && conditions["baseTime"].isInt64()) { - time_t baseTime = (time_t)(conditions["baseTime"].asInt64()); + if (conditions.contains("baseTime") && conditions["baseTime"].is_number_integer()) { + time_t baseTime = (time_t)(conditions["baseTime"].get()); this->RequestBaseTime(baseTime); } } @@ -683,9 +678,9 @@ void WorkInfo::Dump(std::string &result) result.append(ParseToJsonStr()); } -bool WorkInfo::IsHasBoolProp(const Json::Value &value, const std::string &key) +bool WorkInfo::IsHgetProp(const nlohmann::json &value, const std::string &key) { - if (value.isMember(key) && value[key].isBool()) { + if (value.contains(key) && value[key].is_boolean()) { return true; } return false; diff --git a/frameworks/test/unittest/BUILD.gn b/frameworks/test/unittest/BUILD.gn index f27f5be..e0ef2ef 100644 --- a/frameworks/test/unittest/BUILD.gn +++ b/frameworks/test/unittest/BUILD.gn @@ -45,7 +45,6 @@ ohos_systemtest("WorkSchedulerWorkInfoTest") { "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", - "jsoncpp:jsoncpp", ] } @@ -84,7 +83,6 @@ ohos_unittest("WorkSchedulerClientTest") { "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_single", - "jsoncpp:jsoncpp", "samgr:samgr_proxy", ] } diff --git a/frameworks/test/unittest/src/workinfo_test.cpp b/frameworks/test/unittest/src/workinfo_test.cpp index 89d6f1d..51f3afe 100644 --- a/frameworks/test/unittest/src/workinfo_test.cpp +++ b/frameworks/test/unittest/src/workinfo_test.cpp @@ -349,7 +349,7 @@ HWTEST_F (WorkInfoTest, WorkInfoTestJson002, Function | MediumTest | Level2) { using namespace OHOS::WorkScheduler; WorkInfo workInfo = WorkInfo(); - Json::Value root; + nlohmann::json root; bool res = workInfo.ParseFromJson(root); EXPECT_FALSE(res); @@ -405,12 +405,12 @@ HWTEST_F (WorkInfoTest, WorkInfoTestJson002, Function | MediumTest | Level2) HWTEST_F (WorkInfoTest, WorkInfoTestJson003, Function | MediumTest | Level2) { WorkInfo workInfo = WorkInfo(); - Json::Value root; + nlohmann::json root; root["workId"] = 1; root["bundleName"] = "bundleName"; root["abilityName"] = "abilityName"; root["uid"] = 1; - Json::Value conditions; + nlohmann::json conditions; conditions["network"] = 0; conditions["isCharging"] = true; conditions["batteryLevel"] = 0; @@ -462,16 +462,16 @@ HWTEST_F (WorkInfoTest, WorkInfoTestJson003, Function | MediumTest | Level2) HWTEST_F (WorkInfoTest, WorkInfoTestJson004, Function | MediumTest | Level2) { WorkInfo workInfo = WorkInfo(); - Json::Value root; + nlohmann::json root; root["workId"] = 1; root["bundleName"] = "bundleName"; root["abilityName"] = "abilityName"; root["uid"] = 1; - Json::Value extrasJson; + nlohmann::json extrasJson; extrasJson["key1"] = "value1"; extrasJson["key2"] = "value2"; root["parameters"] = extrasJson; - Json::Value extrasType; + nlohmann::json extrasType; extrasType["key1"] = 9; extrasType["key2"] = -1; root["parametersType"] = extrasType; @@ -489,7 +489,7 @@ HWTEST_F (WorkInfoTest, WorkInfoTestJson005, Function | MediumTest | Level2) { using namespace OHOS::WorkScheduler; WorkInfo workInfo = WorkInfo(); - Json::Value root; + nlohmann::json root; root.clear(); root["workId"] = "1"; bool res = workInfo.ParseFromJson(root); @@ -516,7 +516,7 @@ HWTEST_F (WorkInfoTest, WorkInfoTestJson006, Function | MediumTest | Level2) { using namespace OHOS::WorkScheduler; WorkInfo workInfo = WorkInfo(); - Json::Value root; + nlohmann::json root; root.clear(); root["workId"] = 1; root["bundleName"] = "bundleName"; diff --git a/services/native/include/work_scheduler_service.h b/services/native/include/work_scheduler_service.h index dae125d..41d34b2 100644 --- a/services/native/include/work_scheduler_service.h +++ b/services/native/include/work_scheduler_service.h @@ -350,7 +350,7 @@ private: bool AllowDump(); void DumpProcessForEngMode(std::vector& argsInStr, std::string& result); void DumpProcessForUserMode(std::vector& argsInStr, std::string& result); - bool GetJsonFromFile(const char* filePath, Json::Value& root); + bool GetJsonFromFile(const char* filePath, nlohmann::json& root); bool GetUidByBundleName(const std::string& bundleName, int32_t& uid); void InitWorkInner(); void AddWorkInner(WorkInfo& workInfo); diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index ed0af61..b362af5 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -54,7 +54,6 @@ #include "config_policy_utils.h" // for GetOneCfgFile #include "directory_ex.h" #include "event_publisher.h" -#include "json/json.h" #include "policy/app_data_clear_listener.h" #include "policy/memory_policy.h" #include "policy/thermal_policy.h" @@ -225,16 +224,15 @@ void WorkSchedulerService::InitWorkInner() list> WorkSchedulerService::ReadPersistedWorks() { list> workInfos; - Json::Value root; + nlohmann::json root; if (!GetJsonFromFile(PERSISTED_FILE_PATH, root)) { return workInfos; } - if (root.empty() || !root.isObject()) { + if (root.is_null() || root.empty()) { WS_HILOGE("ReadPersistedWorks failed, root is empty or not an object"); return workInfos; } - for (const auto &it : root.getMemberNames()) { - Json::Value workJson = root[it]; + for (const auto &[key, workJson] : root.items()) { shared_ptr workInfo = make_shared(); if (!workInfo->ParseFromJson(workJson)) { WS_HILOGE("ReadPersistedWorks failed, parseFromJson error"); @@ -263,22 +261,21 @@ void WorkSchedulerService::LoadWorksFromFile(const char *path, list workinfo = make_shared(); if (!workinfo->ParseFromJson(workJson)) { WS_HILOGE("LoadWorksFromFile failed, parseFromJson error"); @@ -302,27 +299,27 @@ void WorkSchedulerService::LoadExemptionBundlesFromFile(const char *path) if (!path) { return; } - Json::Value root; - if (!GetJsonFromFile(path, root) || root.empty()) { + nlohmann::json root; + if (!GetJsonFromFile(path, root) || root.is_null() || root.empty()) { WS_HILOGE("file is empty %{private}s", path); return; } - if (!root.isMember(EXEMPTION_BUNDLES_KEY)) { + if (!root.contains(EXEMPTION_BUNDLES_KEY)) { WS_HILOGE("no work_scheduler_eng_exemption_bundles key"); return; } - Json::Value exemptionBundlesRoot = root[EXEMPTION_BUNDLES_KEY]; - if (exemptionBundlesRoot.empty() || !exemptionBundlesRoot.isArray()) { + nlohmann::json exemptionBundlesRoot = root[EXEMPTION_BUNDLES_KEY]; + if (exemptionBundlesRoot.empty() || !exemptionBundlesRoot.is_array()) { WS_HILOGE("work_scheduler_eng_exemption_bundles content is empty"); return; } for (const auto &exemptionBundleName : exemptionBundlesRoot) { - if (exemptionBundleName.empty() || !exemptionBundleName.isString()) { + if (exemptionBundleName.empty() || !exemptionBundleName.is_string()) { WS_HILOGE("Item type error"); } else { - WS_HILOGI("bundle name:%{public}s", exemptionBundleName.asString().c_str()); - exemptionBundles_.insert(exemptionBundleName.asString()); + WS_HILOGI("bundle name:%{public}s", exemptionBundleName.get().c_str()); + exemptionBundles_.insert(exemptionBundleName.get()); } } } @@ -332,49 +329,49 @@ void WorkSchedulerService::LoadMinRepeatTimeFromFile(const char *path) if (!path) { return; } - Json::Value root; - if (!GetJsonFromFile(path, root) || root.empty()) { + nlohmann::json root; + if (!GetJsonFromFile(path, root) || root.is_null() || root.empty()) { WS_HILOGE("file is empty %{private}s", path); return; } - if (!root.isMember(MIN_REPEAT_TIME_KEY)) { + if (!root.contains(MIN_REPEAT_TIME_KEY)) { WS_HILOGE("no work_scheduler_min_repeat_time key"); return; } - Json::Value minRepeatTimeRoot = root[MIN_REPEAT_TIME_KEY]; - if (minRepeatTimeRoot.empty() || !minRepeatTimeRoot.isObject()) { + nlohmann::json minRepeatTimeRoot = root[MIN_REPEAT_TIME_KEY]; + if (minRepeatTimeRoot.empty() || !minRepeatTimeRoot.is_object()) { WS_HILOGE("work_scheduler_min_repeat_time content is empty"); return; } - if (minRepeatTimeRoot.isMember("default") && minRepeatTimeRoot["default"].isInt()) { - minTimeCycle_ = static_cast(minRepeatTimeRoot["default"].asInt()); + if (minRepeatTimeRoot.contains("default") && minRepeatTimeRoot["default"].is_number_unsigned()) { + minTimeCycle_ = minRepeatTimeRoot["default"].get(); } - if (!minRepeatTimeRoot.isMember("special")) { + if (!minRepeatTimeRoot.contains("special")) { WS_HILOGE("no special key"); return; } - Json::Value specialRoot = minRepeatTimeRoot["special"]; - if (specialRoot.empty() || !specialRoot.isArray()) { + nlohmann::json specialRoot = minRepeatTimeRoot["special"]; + if (specialRoot.empty() || !specialRoot.is_array()) { WS_HILOGE("special content is empty"); return; } minCheckTime_ = workQueueManager_->GetTimeCycle(); for (const auto &it : specialRoot) { - if (!it.isMember("bundleName") || !it["bundleName"].isString() || - !it.isMember("time") || !it["time"].isInt()) { + if (!it.contains("bundleName") || !it["bundleName"].is_string() || + !it.contains("time") || !it["time"].is_number_unsigned()) { WS_HILOGE("special content is error"); continue; } - uint32_t time = static_cast(it["time"].asInt()); + uint32_t time = it["time"].get(); if (time < SYS_APP_MIN_REPEAT_TIME) { WS_HILOGE("bundleName: %{public}s set time: %{public}d not available, must more than %{public}d", - it["bundleName"].asString().c_str(), time, SYS_APP_MIN_REPEAT_TIME); + it["bundleName"].get().c_str(), time, SYS_APP_MIN_REPEAT_TIME); continue; } if (minCheckTime_ > time) { minCheckTime_ = time; } - specialMap_.emplace(it["bundleName"].asString(), time); + specialMap_.emplace(it["bundleName"].get(), time); } } @@ -397,7 +394,7 @@ list> WorkSchedulerService::ReadPreinstalledWorks() return workInfos; } -bool WorkSchedulerService::GetJsonFromFile(const char *filePath, Json::Value &root) +bool WorkSchedulerService::GetJsonFromFile(const char *filePath, nlohmann::json &root) { std::string realPath; if (!WorkSchedUtils::ConvertFullPath(filePath, realPath)) { @@ -409,11 +406,8 @@ bool WorkSchedulerService::GetJsonFromFile(const char *filePath, Json::Value &ro std::string data; LoadStringFromFile(realPath.c_str(), data); WS_HILOGI("data read success"); - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - const unique_ptr jsonReader(readerBuilder.newCharReader()); - bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &root, &errs); - if (!res || !errs.empty()) { + const nlohmann::json &jsonObj = nlohmann::json::parse(data, nullptr, false); + if (jsonObj.is_null() || jsonObj.empty()) { WS_HILOGE("parse %{private}s json error", realPath.c_str()); WorkSchedUtil::HiSysEventException(LOAD_WORK, __func__, "json parse failed"); return false; @@ -1316,24 +1310,16 @@ void WorkSchedulerService::DumpParamSet(std::string &key, std::string &value, st void WorkSchedulerService::RefreshPersistedWorks() { - Json::Value root; + nlohmann::json root; for (auto &it : persistedMap_) { auto workInfo = it.second; string data = workInfo->ParseToJsonStr(); - JSONCPP_STRING errs; - Json::Value workJson; - Json::CharReaderBuilder readerBuilder; - const unique_ptr jsonReader(readerBuilder.newCharReader()); - bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &workJson, &errs); - if (res && errs.empty()) { + const nlohmann::json &workJson = nlohmann::json::parse(data, nullptr, false); + if (!workJson.is_null() && !workJson.empty()) { root[it.first] = workJson; } } - Json::StreamWriterBuilder writerBuilder; - ostringstream os; - unique_ptr jsonWriter(writerBuilder.newStreamWriter()); - jsonWriter->write(root, &os); - string result = os.str(); + string result = root.dump(4); CreateNodeDir(PERSISTED_PATH); CreateNodeFile(); ofstream fout; diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn index 5c9385f..62017fa 100644 --- a/services/test/BUILD.gn +++ b/services/test/BUILD.gn @@ -92,7 +92,6 @@ ohos_unittest("WorkScheduleServiceTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "safwk:system_ability_fwk", "samgr:samgr_proxy", "time_service:time_client", diff --git a/services/test/src/workschedulerservice_test.cpp b/services/test/src/workschedulerservice_test.cpp index 8317294..801e9be 100644 --- a/services/test/src/workschedulerservice_test.cpp +++ b/services/test/src/workschedulerservice_test.cpp @@ -29,7 +29,6 @@ #include "conditions/battery_status_listener.h" #include "conditions/charger_listener.h" #include "event_publisher.h" -#include "json/json.h" #include "accesstoken_kit.h" #include "token_setproc.h" @@ -868,7 +867,7 @@ HWTEST_F(WorkSchedulerServiceTest, SendEvent_001, TestSize.Level1) */ HWTEST_F(WorkSchedulerServiceTest, GetJsonFromFile_001, TestSize.Level1) { - Json::Value root; + nlohmann::json root; const char* path = "/a/b/c"; bool ret = workSchedulerService_->GetJsonFromFile(path, root); EXPECT_FALSE(ret); diff --git a/services/zidl/BUILD.gn b/services/zidl/BUILD.gn index b418366..f701ab7 100644 --- a/services/zidl/BUILD.gn +++ b/services/zidl/BUILD.gn @@ -46,7 +46,6 @@ ohos_source_set("workschedservice_zidl_stub") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "jsoncpp:jsoncpp", ] subsystem_name = "resourceschedule" part_name = "${worksched_native_part_name}" @@ -71,7 +70,6 @@ ohos_source_set("workschedservice_zidl_proxy") { "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", - "jsoncpp:jsoncpp", ] subsystem_name = "resourceschedule" part_name = "${worksched_native_part_name}" diff --git a/test/fuzztest/workschedulergetallrunningworks_fuzzer/BUILD.gn b/test/fuzztest/workschedulergetallrunningworks_fuzzer/BUILD.gn index 19904d9..11349af 100644 --- a/test/fuzztest/workschedulergetallrunningworks_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulergetallrunningworks_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerGetAllRunningWorksFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulergetworkstatus_fuzzer/BUILD.gn b/test/fuzztest/workschedulergetworkstatus_fuzzer/BUILD.gn index b673b22..af23e90 100644 --- a/test/fuzztest/workschedulergetworkstatus_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulergetworkstatus_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerGetWorkStatusFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerislastworktimeout_fuzzer/BUILD.gn b/test/fuzztest/workschedulerislastworktimeout_fuzzer/BUILD.gn index 554a651..9a21208 100644 --- a/test/fuzztest/workschedulerislastworktimeout_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerislastworktimeout_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerIsLastWorkTimeOutFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerobtainallworks_fuzzer/BUILD.gn b/test/fuzztest/workschedulerobtainallworks_fuzzer/BUILD.gn index e4db02e..7b7ee2b 100644 --- a/test/fuzztest/workschedulerobtainallworks_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerobtainallworks_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerObtainAllWorksFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerpauserunningworks_fuzzer/BUILD.gn b/test/fuzztest/workschedulerpauserunningworks_fuzzer/BUILD.gn index 1b16c05..aaaa085 100644 --- a/test/fuzztest/workschedulerpauserunningworks_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerpauserunningworks_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerPauseRunningWorksFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerresumepausedworks_fuzzer/BUILD.gn b/test/fuzztest/workschedulerresumepausedworks_fuzzer/BUILD.gn index aa505b8..c0062d6 100644 --- a/test/fuzztest/workschedulerresumepausedworks_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerresumepausedworks_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerResumePausedWorksFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulersetworkschedulerconfig_fuzzer/BUILD.gn b/test/fuzztest/workschedulersetworkschedulerconfig_fuzzer/BUILD.gn index 00e4dac..55b31ba 100644 --- a/test/fuzztest/workschedulersetworkschedulerconfig_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulersetworkschedulerconfig_fuzzer/BUILD.gn @@ -66,7 +66,6 @@ ohos_fuzztest("WorkSchedulerSetWorkSchedulerConfigFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerstartwork_fuzzer/BUILD.gn b/test/fuzztest/workschedulerstartwork_fuzzer/BUILD.gn index fc39109..a641528 100644 --- a/test/fuzztest/workschedulerstartwork_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerstartwork_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerStartWorkFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerstopandcancelwork_fuzzer/BUILD.gn b/test/fuzztest/workschedulerstopandcancelwork_fuzzer/BUILD.gn index 7cad11f..10709ee 100644 --- a/test/fuzztest/workschedulerstopandcancelwork_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerstopandcancelwork_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerStopAndCancelWorkFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerstopandclearworks_fuzzer/BUILD.gn b/test/fuzztest/workschedulerstopandclearworks_fuzzer/BUILD.gn index 51f7d10..932e3b5 100644 --- a/test/fuzztest/workschedulerstopandclearworks_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerstopandclearworks_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerStopAndClearWorksFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workschedulerstopwork_fuzzer/BUILD.gn b/test/fuzztest/workschedulerstopwork_fuzzer/BUILD.gn index 3dc14f6..f9a8bbe 100644 --- a/test/fuzztest/workschedulerstopwork_fuzzer/BUILD.gn +++ b/test/fuzztest/workschedulerstopwork_fuzzer/BUILD.gn @@ -67,7 +67,6 @@ ohos_fuzztest("WorkSchedulerStopWorkFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "resource_management:global_resmgr", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/fuzztest/workscheduleservice_fuzzer/BUILD.gn b/test/fuzztest/workscheduleservice_fuzzer/BUILD.gn index 10bdd48..91c0449 100644 --- a/test/fuzztest/workscheduleservice_fuzzer/BUILD.gn +++ b/test/fuzztest/workscheduleservice_fuzzer/BUILD.gn @@ -64,7 +64,6 @@ ohos_fuzztest("WorkScheduleServiceFuzzTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", - "jsoncpp:jsoncpp", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/utils/native/BUILD.gn b/utils/native/BUILD.gn index 65630b8..af16dc9 100644 --- a/utils/native/BUILD.gn +++ b/utils/native/BUILD.gn @@ -39,7 +39,6 @@ ohos_source_set("workschedutils") { "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_single", - "jsoncpp:jsoncpp", "os_account:os_account_innerkits", ] diff --git a/utils/native/include/work_sched_constants.h b/utils/native/include/work_sched_constants.h index 54766a1..62b0fb7 100644 --- a/utils/native/include/work_sched_constants.h +++ b/utils/native/include/work_sched_constants.h @@ -53,6 +53,7 @@ inline const std::string LOAD_SA = "LOAD_SA"; inline const std::string TOKEN_CHECK = "TOKEN_CHECK"; inline const std::string WORK_CHECK = "WORK_CHECK"; inline const std::string SERVICE_STOP = "SERVICE_STOP"; +inline constexpr uint32_t JSON_INDENT_WIDTH = 4; } // namespace WorkScheduler } // namespace OHOS #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_CONSTANTS_H \ No newline at end of file -- Gitee From 94be6b078b53676f2c153145fe6b1f099dca31d6 Mon Sep 17 00:00:00 2001 From: chengshichang Date: Sat, 21 Jun 2025 14:18:10 +0800 Subject: [PATCH 2/2] Signed-off-by: csc --- .../extension/src/js_work_scheduler_extension.cpp | 3 +-- frameworks/src/work_info.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/frameworks/extension/src/js_work_scheduler_extension.cpp b/frameworks/extension/src/js_work_scheduler_extension.cpp index d1ba868..5a66335 100644 --- a/frameworks/extension/src/js_work_scheduler_extension.cpp +++ b/frameworks/extension/src/js_work_scheduler_extension.cpp @@ -24,11 +24,10 @@ #include "js_work_scheduler_extension_context.h" #include "work_scheduler_stub_imp.h" #include "hitrace_meter.h" +#include "work_sched_constants.h" namespace OHOS { namespace WorkScheduler { -const int32_t INVALID_VALUE = -1; - JsWorkSchedulerExtension* JsWorkSchedulerExtension::Create(const std::unique_ptr& runtime) { return new JsWorkSchedulerExtension(static_cast(*runtime)); diff --git a/frameworks/src/work_info.cpp b/frameworks/src/work_info.cpp index a5270d3..8014d07 100644 --- a/frameworks/src/work_info.cpp +++ b/frameworks/src/work_info.cpp @@ -560,29 +560,29 @@ bool WorkInfo::ParseFromJson(const nlohmann::json &value) return false; } this->workId_ = value["workId"].get(); - if ((value.contains("saId") && value["saId"].is_number_integer()) && IsHgetProp(value, "residentSa")) { + if ((value.contains("saId") && value["saId"].is_number_integer()) && IsHasBoolProp(value, "residentSa")) { this->saId_ = value["saId"].get(); this->residentSa_ = value["residentSa"].get(); } if (!ParseElementFromJson(value)) { return false; } - if (IsHgetProp(value, "persisted")) { + if (IsHasBoolProp(value, "persisted")) { this->persisted_ = value["persisted"].get(); } - if (IsHgetProp(value, "preinstalled")) { + if (IsHasBoolProp(value, "preinstalled")) { this->preinstalled_ = value["preinstalled"].get(); } if (value.contains("uriKey") && value["uriKey"].is_string()) { this->uriKey_ = value["uriKey"].get(); } - if (IsHgetProp(value, "callBySystemApp")) { + if (IsHasBoolProp(value, "callBySystemApp")) { this->callBySystemApp_ = value["callBySystemApp"].get(); } if (value.contains("appIndex") && value["appIndex"].is_number_integer()) { this->appIndex_ = value["appIndex"].get(); } - if (IsHgetProp(value, "extension")) { + if (IsHasBoolProp(value, "extension")) { this->extension_ = value["extension"].get(); } ParseConditionFromJsonStr(value); @@ -678,7 +678,7 @@ void WorkInfo::Dump(std::string &result) result.append(ParseToJsonStr()); } -bool WorkInfo::IsHgetProp(const nlohmann::json &value, const std::string &key) +bool WorkInfo::IsHasBoolProp(const nlohmann::json &value, const std::string &key) { if (value.contains(key) && value[key].is_boolean()) { return true; -- Gitee