From d675cd00c44e7d3ee2a63e215eacb0956761a37e Mon Sep 17 00:00:00 2001 From: gaorui Date: Thu, 15 May 2025 15:24:41 +0800 Subject: [PATCH] fix: jsoncpp is replaced with cJSON Signed-off-by: gaorui Change-Id: Ied8491b3aac820e50fe9c45267aae31df8a63e45 --- bundle.json | 2 +- services/BUILD.gn | 2 +- services/native/src/power_mode_policy.cpp | 48 +++++-- .../native/src/shutdown/shutdown_dialog.cpp | 42 +++--- .../src/suspend/suspend_source_parser.cpp | 44 +++--- .../src/suspend/suspend_source_parser.h | 4 +- .../native/src/wakeup/wakeup_controller.cpp | 126 ++++++++++++------ .../src/wakeup/wakeup_source_parser.cpp | 46 ++++--- .../native/src/wakeup/wakeup_source_parser.h | 4 +- .../wakeup_action_source_parser.cpp | 51 +++---- .../wakeup_action_source_parser.h | 4 +- .../customized_screen_event_rules.cpp | 2 +- test/apitest/inner_api/shutdown/BUILD.gn | 1 + .../asyncshutdowncallback_fuzzer/BUILD.gn | 2 +- .../createrunninglock_fuzzer/BUILD.gn | 2 +- .../forcesuspenddevice_fuzzer/BUILD.gn | 2 +- .../fuzztest/getsetdevicemode_fuzzer/BUILD.gn | 2 +- test/fuzztest/getstate_fuzzer/BUILD.gn | 2 +- test/fuzztest/hibernate_fuzzer/BUILD.gn | 2 +- .../iscollaborationscreenon_fuzzer/BUILD.gn | 2 +- test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn | 2 +- .../BUILD.gn | 2 +- test/fuzztest/isscreenon_fuzzer/BUILD.gn | 2 +- test/fuzztest/isstandby_fuzzer/BUILD.gn | 2 +- .../lockscreenaftertimingout_fuzzer/BUILD.gn | 2 +- .../fuzztest/lockunlockisused_fuzzer/BUILD.gn | 2 +- .../overridescreenofftime_fuzzer/BUILD.gn | 2 +- .../powermodecallback_fuzzer/BUILD.gn | 2 +- .../powerstatecallback_fuzzer/BUILD.gn | 2 +- .../fuzztest/proxyrunninglock_fuzzer/BUILD.gn | 2 +- .../proxyrunninglocks_fuzzer/BUILD.gn | 2 +- .../queryrunninglocklists_fuzzer/BUILD.gn | 2 +- test/fuzztest/rebootdevice_fuzzer/BUILD.gn | 2 +- test/fuzztest/refreshactivity_fuzzer/BUILD.gn | 2 +- .../releaserunninglock_fuzzer/BUILD.gn | 2 +- .../resetrunninglocks_fuzzer/BUILD.gn | 2 +- .../runninglockcallback_fuzzer/BUILD.gn | 2 +- .../screenstatecallback_fuzzer/BUILD.gn | 2 +- .../setdisplaysuspend_fuzzer/BUILD.gn | 2 +- .../setforcetimingout_fuzzer/BUILD.gn | 2 +- test/fuzztest/setsuspendtag_fuzzer/BUILD.gn | 2 +- test/fuzztest/shelldump_fuzzer/BUILD.gn | 2 +- test/fuzztest/shutdowndevice_fuzzer/BUILD.gn | 2 +- test/fuzztest/suspenddevice_fuzzer/BUILD.gn | 2 +- .../synchibernatecallback_fuzzer/BUILD.gn | 2 +- .../syncshutdowncallback_fuzzer/BUILD.gn | 2 +- .../syncsleepcallback_fuzzer/BUILD.gn | 2 +- .../takeovershutdowncallback_fuzzer/BUILD.gn | 2 +- test/fuzztest/wakeupdevice_fuzzer/BUILD.gn | 2 +- test/systemtest/BUILD.gn | 1 + test/unittest/BUILD.gn | 2 +- .../power_wakeup_controller_test.cpp | 30 +++-- .../src/power_parsesources_mock_test.cpp | 4 +- .../wakeup_suspend/power_wakeup_test.cpp | 3 +- test/unittest/src/servicetest/BUILD.gn | 1 + utils/vibrator/BUILD.gn | 2 +- .../vibrator/include/vibrator_source_parser.h | 4 +- utils/vibrator/src/vibrator_source_parser.cpp | 66 ++++++--- 58 files changed, 346 insertions(+), 215 deletions(-) diff --git a/bundle.json b/bundle.json index fa3dc112..4e968f38 100644 --- a/bundle.json +++ b/bundle.json @@ -64,6 +64,7 @@ "battery_manager", "common_event_service", "config_policy", + "cJSON", "c_utils", "data_share", "display_manager", @@ -82,7 +83,6 @@ "input", "ipc", "image_framework", - "jsoncpp", "libxml2", "napi", "os_account", diff --git a/services/BUILD.gn b/services/BUILD.gn index c1495f03..22e404ad 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -107,6 +107,7 @@ ohos_shared_library("powermgrservice") { "ability_base:want", "ability_runtime:ability_connect_callback_stub", "bundle_framework:appexecfwk_core", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", @@ -123,7 +124,6 @@ ohos_shared_library("powermgrservice") { "image_framework:image_native", "init:libbegetutil", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/native/src/power_mode_policy.cpp b/services/native/src/power_mode_policy.cpp index 469871e9..3c41558d 100644 --- a/services/native/src/power_mode_policy.cpp +++ b/services/native/src/power_mode_policy.cpp @@ -13,13 +13,13 @@ * limitations under the License. */ +#include #include "power_mode_policy.h" #include "power_mgr_service.h" #include "power_log.h" #include "power_save_mode.h" #include "singleton.h" #include "setting_helper.h" -#include "json/json.h" using namespace std; namespace OHOS { @@ -68,20 +68,30 @@ void PowerModePolicy::ComparePowerModePolicy() bool PowerModePolicy::InitRecoverMap() { std::string jsonStr = SettingHelper::ReadPowerModeRecoverMap(); - Json::Value recoverJson; - Json::Reader reader; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), recoverJson)) { + + cJSON* recoverJson = cJSON_Parse(jsonStr.c_str()); + if (!recoverJson) { POWER_HILOGW(FEATURE_POWER_MODE, "parse recover json str error"); return false; } - for (const auto &member : recoverJson.getMemberNames()) { - int32_t key = std::stoi(member); - if (!recoverJson[member].isInt()) { + if (!cJSON_IsObject(recoverJson)) { + POWER_HILOGW(FEATURE_POWER_MODE, "recover json root is not an object"); + cJSON_Delete(recoverJson); + return false; + } + cJSON* item = nullptr; + cJSON_ArrayForEach(item, recoverJson) { + const char* keyStr = item->string; + if (!keyStr || !cJSON_IsNumber(item)) { continue; } - int32_t value = recoverJson[member].asInt(); + + int32_t key = std::stoi(keyStr); + int32_t value = static_cast(item->valueint); recoverMap_[key] = value; } + + cJSON_Delete(recoverJson); POWER_HILOGI(FEATURE_POWER_MODE, "init recover map succeed"); return true; } @@ -193,11 +203,27 @@ void PowerModePolicy::RemoveBackupMapSettingSwitch(uint32_t switchId) void PowerModePolicy::SavePowerModeRecoverMap() { - Json::Value recoverJson; + cJSON* recoverJson = cJSON_CreateObject(); + if (!recoverJson) { + POWER_HILOGE(FEATURE_POWER_MODE, "Failed to create cJSON object"); + return; + } + for (const auto& pair : recoverMap_) { - recoverJson[to_string(pair.first)] = pair.second; + std::string keyStr = std::to_string(pair.first); + cJSON_AddNumberToObject(recoverJson, keyStr.c_str(), pair.second); + } + + char* jsonStr = cJSON_Print(recoverJson); + if (!jsonStr) { + POWER_HILOGE(FEATURE_POWER_MODE, "Failed to print cJSON to string"); + cJSON_Delete(recoverJson); + return; } - SettingHelper::SavePowerModeRecoverMap(recoverJson.toStyledString()); + std::string jsonConfig = std::string(jsonStr); + SettingHelper::SavePowerModeRecoverMap(jsonConfig); + cJSON_free(jsonStr); + cJSON_Delete(recoverJson); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp index 536f8820..ab3d15c1 100644 --- a/services/native/src/shutdown/shutdown_dialog.cpp +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -27,11 +27,10 @@ #include #include #endif +#include #include #include "config_policy_utils.h" -#include "json/reader.h" -#include "json/value.h" #include "power_log.h" #include "power_mgr_service.h" #include "power_vibrator.h" @@ -187,30 +186,41 @@ void ShutdownDialog::LoadDialogConfig() std::ifstream inputStream(configPath, std::ios::in | std::ios::binary); std::string contentStr(std::istreambuf_iterator {inputStream}, std::istreambuf_iterator {}); - Json::Reader reader; - Json::Value root; - if (!reader.parse(contentStr.data(), contentStr.data() + contentStr.size(), root)) { - POWER_HILOGE(COMP_UTILS, "json parse error"); + if (contentStr.empty()) { + POWER_HILOGE(COMP_UTILS, "json file is empty"); return; } - if (root.isNull() || !root.isObject()) { - POWER_HILOGE(COMP_UTILS, "json root invalid[%{public}s]", contentStr.c_str()); + cJSON* root = cJSON_Parse(contentStr.c_str()); + if (!root) { + POWER_HILOGE(COMP_UTILS, "json parse error[%{public}s]", contentStr.c_str()); return; } - - if (!root["bundleName"].isString() || - !root["abilityName"].isString() || !root["uiExtensionType"].isString()) { - POWER_HILOGE(COMP_UTILS, "json varibale not support"); + + if (!cJSON_IsObject(root)) { + POWER_HILOGE(COMP_UTILS, "json root invalid"); + cJSON_Delete(root); + return; + } + + cJSON* bundleNameItem = cJSON_GetObjectItemCaseSensitive(root, "bundleName"); + cJSON* abilityNameItem = cJSON_GetObjectItemCaseSensitive(root, "abilityName"); + cJSON* uiExtensionTypeItem = cJSON_GetObjectItemCaseSensitive(root, "uiExtensionType"); + if (!bundleNameItem || !cJSON_IsString(bundleNameItem) || + !abilityNameItem || !cJSON_IsString(abilityNameItem) || + !uiExtensionTypeItem || !cJSON_IsString(uiExtensionTypeItem)) { + POWER_HILOGE(COMP_UTILS, "json variable not supported"); + cJSON_Delete(root); return; } - bundleName_ = root["bundleName"].asString(); - abilityName_ = root["abilityName"].asString(); - uiExtensionType_ = root["uiExtensionType"].asString(); - dialogBundleName_ = root["bundleName"].asString(); + bundleName_ = bundleNameItem->valuestring; + abilityName_ = abilityNameItem->valuestring; + uiExtensionType_ = uiExtensionTypeItem->valuestring; + dialogBundleName_ = bundleNameItem->valuestring; dialogAbilityName_ = "com.ohos.sceneboard.systemdialog"; POWER_HILOGI(COMP_UTILS, "PowerOff variables have changed"); + cJSON_Delete(root); } void ShutdownDialog::DialogAbilityConnection::OnAbilityConnectDone( diff --git a/services/native/src/suspend/suspend_source_parser.cpp b/services/native/src/suspend/suspend_source_parser.cpp index 6d835c74..66f9f749 100644 --- a/services/native/src/suspend/suspend_source_parser.cpp +++ b/services/native/src/suspend/suspend_source_parser.cpp @@ -18,11 +18,11 @@ #include #include +#include + #include "config_policy_utils.h" #include "power_log.h" #include "setting_helper.h" -#include "json/reader.h" -#include "json/value.h" #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING #include "power_mgr_service.h" #endif @@ -152,36 +152,40 @@ bool SuspendSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr SuspendSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - std::string errors; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_SUSPEND, "json parse error"); parseSources->SetParseErrorFlag(true); return parseSources; } - - if (root.isNull() || !root.isObject()) { + if (!cJSON_IsObject(root)) { POWER_HILOGE(FEATURE_SUSPEND, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); + cJSON_Delete(root); return parseSources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - bool ret = ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_SUSPEND, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_SUSPEND, "lost map config key"); continue; } } + + cJSON_Delete(root); return parseSources; } bool SuspendSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { if (parseSources == nullptr) { POWER_HILOGE(FEATURE_SUSPEND, "parseSources is nullptr"); @@ -195,12 +199,12 @@ bool SuspendSourceParser::ParseSourcesProc( uint32_t action = 0; uint32_t delayMs = 0; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value actionValue = valueObj[SuspendSource::ACTION_KEY]; - Json::Value delayValue = valueObj[SuspendSource::DELAY_KEY]; - if (actionValue.isUInt() && delayValue.isUInt()) { - action = actionValue.asUInt(); - delayMs = delayValue.asUInt(); + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::ACTION_KEY); + cJSON* delayValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::DELAY_KEY); + if (actionValue && cJSON_IsNumber(actionValue) && delayValue && cJSON_IsNumber(delayValue)) { + action = static_cast(actionValue->valueint); + delayMs = static_cast(delayValue->valueint); if (action >= ILLEGAL_ACTION) { action = 0; } diff --git a/services/native/src/suspend/suspend_source_parser.h b/services/native/src/suspend/suspend_source_parser.h index f209d4d6..6b2e5b65 100644 --- a/services/native/src/suspend/suspend_source_parser.h +++ b/services/native/src/suspend/suspend_source_parser.h @@ -19,8 +19,8 @@ #include #include +#include #include "suspend_sources.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(const std::string& config); static bool GetTargetPath(std::string& targetPath); static bool ParseSourcesProc( - std::shared_ptr &parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr &parseSources, cJSON* valueObj, std::string& key); static const std::string GetSuspendSourcesByConfig(); }; } // namespace PowerMgr diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index bb2e2a1f..6e23002a 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -23,9 +23,9 @@ #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART #include #endif +#include #include #include -#include #include #include "permission.h" #include "power_errors.h" @@ -198,33 +198,46 @@ void WakeupController::ChangeWakeupSourceConfig(bool updateEnable) return; } POWER_HILOGI(COMP_SVC, "the origin ccmJson is: %{public}s", jsonStr.c_str()); - Json::Value root; - Json::Reader reader; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(COMP_SVC, "json parse error"); return; } - if (root["touchscreen"].isNull()) { - POWER_HILOGE(COMP_SVC, "this touchscreenNode is empty"); + if (!cJSON_IsObject(root)) { + POWER_HILOGW(COMP_SVC, "json root is not an object"); + cJSON_Delete(root); return; } - if (root["touchscreen"]["enable"].isNull()) { - POWER_HILOGE(COMP_SVC, "the touchscreenNode is empty"); + cJSON* touchscreenNode = cJSON_GetObjectItemCaseSensitive(root, "touchscreen"); + if (!touchscreenNode || !cJSON_IsObject(touchscreenNode)) { + POWER_HILOGE(COMP_SVC, "this touchscreenNode is empty"); + cJSON_Delete(root); return; } - if (!root["touchscreen"]["enable"].isBool()) { - POWER_HILOGE(COMP_SVC, "the origin touchscreenEnable value is invalid"); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(touchscreenNode, "enable"); + if (!enableNode || !cJSON_IsBool(enableNode)) { + POWER_HILOGE(COMP_SVC, "the touchscreenNode enable value is invalid"); + cJSON_Delete(root); return; } - bool originEnable = root["touchscreen"]["enable"].asBool(); + bool originEnable = cJSON_IsTrue(enableNode); if (originEnable == updateEnable) { POWER_HILOGI(COMP_SVC, "no need change jsonConfig value"); + cJSON_Delete(root); return; } - - root["touchscreen"]["enable"] = updateEnable; - POWER_HILOGI(COMP_SVC, "the new doubleJsonConfig is: %{public}s", root.toStyledString().c_str()); - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + enableNode->valueint = updateEnable ? 1 : 0; + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; + } + POWER_HILOGI(COMP_SVC, "the new doubleJsonConfig is: %{public}s", jsonUpdatedStr); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } static const char* POWER_MANAGER_EXT_PATH = "libpower_manager_ext.z.so"; @@ -307,33 +320,46 @@ void WakeupController::ChangePickupWakeupSourceConfig(bool updataEnable) return; } POWER_HILOGI(COMP_SVC, "%{public}s(%{public}d)", __func__, updataEnable); - Json::Value root; - Json::Reader reader; - reader.parse(jsonStr, root); - if (!reader.parse(jsonStr, root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(COMP_SVC, "Failed to parse json string"); return; } - if (root["pickup"].isNull()) { - POWER_HILOGE(COMP_SVC, "this pickNode is empty"); + if (!cJSON_IsObject(root)) { + POWER_HILOGW(COMP_SVC, "json root is not an object"); + cJSON_Delete(root); return; } - if (root["pickup"]["enable"].isNull()) { - POWER_HILOGE(COMP_SVC, "the pickupNode is empty"); + cJSON* pickupNode = cJSON_GetObjectItemCaseSensitive(root, "pickup"); + if (!pickupNode || !cJSON_IsObject(pickupNode)) { + POWER_HILOGE(COMP_SVC, "this pickNode is empty"); + cJSON_Delete(root); return; } - if (!root["pickup"]["enable"].isBool()) { - POWER_HILOGE(COMP_SVC, "the origin pickupEnable value is invalid"); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(pickupNode, "enable"); + if (!enableNode || !cJSON_IsBool(enableNode)) { + POWER_HILOGE(COMP_SVC, "the pickupNode enable value is invalid"); + cJSON_Delete(root); return; } - bool originEnable = root["pickup"]["enable"].asBool(); + bool originEnable = cJSON_IsTrue(enableNode); if (originEnable == updataEnable) { POWER_HILOGI(COMP_SVC, "no need change jsonconfig_value"); + cJSON_Delete(root); return; } - root["pickup"]["enable"] = updataEnable; - POWER_HILOGI(COMP_SVC, "the new pickupJsonConfig is: %{public}s", root.toStyledString().c_str()); - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + enableNode->valueint = updataEnable ? 1 : 0; + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; + } + POWER_HILOGI(COMP_SVC, "the new pickupJsonConfig is: %{public}s", jsonUpdatedStr); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } #endif @@ -342,29 +368,49 @@ void WakeupController::ChangeLidWakeupSourceConfig(bool updataEnable) std::lock_guard lock(sourceUpdateMutex_); std::string jsonStr = SettingHelper::GetSettingWakeupSources(); POWER_HILOGI(FEATURE_POWER_STATE, "%{public}s", jsonStr.c_str()); - Json::Value root; - Json::Reader reader; - reader.parse(jsonStr, root); - if (!reader.parse(jsonStr, root)) { + + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_POWER_STATE, "Failed to parse json string"); return; } + if (!cJSON_IsObject(root)) { + POWER_HILOGW(FEATURE_POWER_STATE, "json root is not an object"); + cJSON_Delete(root); + return; + } + cJSON* lidNode = cJSON_GetObjectItemCaseSensitive(root, "lid"); + if (!lidNode || !cJSON_IsObject(lidNode)) { + POWER_HILOGE(FEATURE_POWER_STATE, "this lidNode is empty or not an object"); + cJSON_Delete(root); + return; + } bool originEnable = true; - if (root["lid"]["enable"].isBool()) { - originEnable = root["lid"]["enable"].asBool(); + cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(lidNode, "enable"); + if (enableNode && cJSON_IsBool(enableNode)) { + originEnable = cJSON_IsTrue(enableNode); } - if (originEnable == updataEnable) { POWER_HILOGI(FEATURE_POWER_STATE, "no need change jsonConfig value"); + cJSON_Delete(root); return; } - if (root["lid"]["enable"].isBool()) { - root["lid"]["enable"] = updataEnable; + + if (enableNode && cJSON_IsBool(enableNode)) { + enableNode->valueint = updataEnable ? 1 : 0; + } + char* jsonUpdatedStr = cJSON_Print(root); + if (!jsonUpdatedStr) { + POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); + cJSON_Delete(root); + return; } - SettingHelper::SetSettingWakeupSources(root.toStyledString()); + std::string jsonConfig = std::string(jsonUpdatedStr); + SettingHelper::SetSettingWakeupSources(jsonConfig); + cJSON_free(jsonUpdatedStr); + cJSON_Delete(root); } - void WakeupController::ExecWakeupMonitorByReason(WakeupDeviceType reason) { FFRTUtils::SubmitTask([this, reason] { diff --git a/services/native/src/wakeup/wakeup_source_parser.cpp b/services/native/src/wakeup/wakeup_source_parser.cpp index 9df7dad1..337a787f 100644 --- a/services/native/src/wakeup/wakeup_source_parser.cpp +++ b/services/native/src/wakeup/wakeup_source_parser.cpp @@ -16,12 +16,11 @@ #include #include +#include #include "config_policy_utils.h" #include "power_log.h" #include "setting_helper.h" #include "wakeup_source_parser.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -109,51 +108,54 @@ bool WakeupSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr WakeupSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - std::string errors; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { POWER_HILOGE(FEATURE_WAKEUP, "json parse error"); parseSources->SetParseErrorFlag(true); return parseSources; } - if (root.isNull() || !root.isObject()) { + if (!cJSON_IsObject(root)) { POWER_HILOGE(FEATURE_WAKEUP, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); + cJSON_Delete(root); return parseSources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - - bool ret = ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_WAKEUP, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_WAKEUP, "lost map config key"); continue; } } + cJSON_Delete(root); return parseSources; } bool WakeupSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { bool enable = true; uint32_t click = DOUBLE_CLICK; WakeupDeviceType wakeupDeviceType = WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value enableValue = valueObj[WakeupSource::ENABLE_KEY]; - Json::Value clickValue = valueObj[WakeupSource::KEYS_KEY]; - if (!clickValue.isNull() && clickValue.isUInt()) { - click = (clickValue.asUInt() == SINGLE_CLICK || clickValue.asUInt() == DOUBLE_CLICK) ? clickValue.asUInt() : - DOUBLE_CLICK; + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* enableValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::ENABLE_KEY); + if (enableValue && cJSON_IsBool(enableValue)) { + enable = cJSON_IsTrue(enableValue); } - if (enableValue.isBool()) { - enable = enableValue.asBool(); + cJSON* clickValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::KEYS_KEY); + if (clickValue && cJSON_IsNumber(clickValue)) { + uint32_t clickInt = static_cast(clickValue->valueint); + click = (clickInt == SINGLE_CLICK || clickInt == DOUBLE_CLICK) ? clickInt : DOUBLE_CLICK; } } diff --git a/services/native/src/wakeup/wakeup_source_parser.h b/services/native/src/wakeup/wakeup_source_parser.h index d5ff08ec..8e195329 100644 --- a/services/native/src/wakeup/wakeup_source_parser.h +++ b/services/native/src/wakeup/wakeup_source_parser.h @@ -21,7 +21,7 @@ #include #include -#include "json/value.h" +#include namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(); static std::shared_ptr ParseSources(const std::string& config); static bool ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key); static bool GetTargetPath(std::string& targetPath); static void SetSettingsToDatabase(WakeupDeviceType type, bool enable); static const std::string GetWakeupSourcesByConfig(); diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp index 9ea26405..5cf07df3 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp @@ -18,10 +18,9 @@ #include #include +#include #include "config_policy_utils.h" #include "power_log.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -79,46 +78,52 @@ bool WakeupActionSourceParser::GetTargetPath(std::string& targetPath) std::shared_ptr WakeupActionSourceParser::ParseSources(const std::string& jsonStr) { std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { - POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error"); + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { + POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json parse error[%{public}s]", jsonStr.c_str()); return parseSources; } - if (root.isNull() || !root.isObject()) { - POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid[%{public}s]", jsonStr.c_str()); + if (!cJSON_IsObject(root)) { + POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid"); + cJSON_Delete(root); return parseSources; } - - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key.c_str()); - bool ret = ParseSourcesProc(parseSources, valueObj, key); + + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGI(FEATURE_WAKEUP_ACTION, "invalid key in json object"); + continue; + } + POWER_HILOGI(FEATURE_WAKEUP_ACTION, "key=%{public}s", key); + std::string keyStr = std::string(key); + bool ret = ParseSourcesProc(parseSources, item, keyStr); if (ret == false) { POWER_HILOGI(FEATURE_WAKEUP_ACTION, "lost map config key"); continue; } } + + cJSON_Delete(root); return parseSources; } bool WakeupActionSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { std::string scene{""}; uint32_t action = 0; - if (!valueObj.isNull() && valueObj.isObject()) { - Json::Value sceneValue = valueObj[WakeupActionSource::SCENE_KEY]; - Json::Value actionValue = valueObj[WakeupActionSource::ACTION_KEY]; - if (sceneValue.isString()) { - scene = sceneValue.asString(); + if (valueObj && cJSON_IsObject(valueObj)) { + cJSON* sceneValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::SCENE_KEY); + if (sceneValue && cJSON_IsString(sceneValue)) { + scene = sceneValue->valuestring; POWER_HILOGI(FEATURE_WAKEUP_ACTION, "scene=%{public}s", scene.c_str()); } - if (actionValue.isUInt()) { - action = actionValue.asUInt(); + cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::ACTION_KEY); + if (actionValue && cJSON_IsNumber(actionValue)) { + action = static_cast(actionValue->valueint); POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action); if (action >= ILLEGAL_ACTION) { action = 0; diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.h b/services/native/src/wakeup_action/wakeup_action_source_parser.h index b7f13063..2203ecbf 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.h +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.h @@ -19,8 +19,8 @@ #include #include +#include #include "wakeup_action_sources.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -30,7 +30,7 @@ public: static std::shared_ptr ParseSources(const std::string& jsonStr); static bool GetTargetPath(std::string& targetPath); static bool ParseSourcesProc( - std::shared_ptr &parseSources, Json::Value& valueObj, std::string& key); + std::shared_ptr &parseSources, cJSON* valueObj, std::string& key); }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp b/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp index 15b28b76..14558709 100644 --- a/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp +++ b/services/native/src/watch_customized_screen_common_event/customized_screen_event_rules.cpp @@ -15,7 +15,7 @@ #include "customized_screen_event_rules.h" #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES -#include "json/json.h" +#include #include "power_ext_intf_wrapper.h" #endif diff --git a/test/apitest/inner_api/shutdown/BUILD.gn b/test/apitest/inner_api/shutdown/BUILD.gn index d1e798e3..32ae2602 100644 --- a/test/apitest/inner_api/shutdown/BUILD.gn +++ b/test/apitest/inner_api/shutdown/BUILD.gn @@ -45,6 +45,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn index 99ce3f02..e1b464e5 100644 --- a/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/asyncshutdowncallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("AsyncShutdownCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/createrunninglock_fuzzer/BUILD.gn b/test/fuzztest/createrunninglock_fuzzer/BUILD.gn index 613c2053..88bf822b 100644 --- a/test/fuzztest/createrunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/createrunninglock_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("CreateRunningLockFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn b/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn index 56d43b64..2c8d1a3a 100644 --- a/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn +++ b/test/fuzztest/forcesuspenddevice_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ForceSuspendDeviceFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn b/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn index b271f91d..70eda90d 100644 --- a/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn +++ b/test/fuzztest/getsetdevicemode_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("GetSetDeviceModeFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/getstate_fuzzer/BUILD.gn b/test/fuzztest/getstate_fuzzer/BUILD.gn index 78a57d7b..14049f5d 100644 --- a/test/fuzztest/getstate_fuzzer/BUILD.gn +++ b/test/fuzztest/getstate_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("GetStateFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/hibernate_fuzzer/BUILD.gn b/test/fuzztest/hibernate_fuzzer/BUILD.gn index c18fe1fa..c8822fad 100644 --- a/test/fuzztest/hibernate_fuzzer/BUILD.gn +++ b/test/fuzztest/hibernate_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("HibernateFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn b/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn index 12fae589..7f46109f 100644 --- a/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/iscollaborationscreenon_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("IsCollaborationScreenOnFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn b/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn index 33538705..d5f0a9c7 100644 --- a/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/isfoldscreenon_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("IsFoldScreenOnFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn b/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn index 8af4f6b1..8816956d 100644 --- a/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn +++ b/test/fuzztest/isrunninglocktypesupported_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("IsRunningLockTypeSupportedFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/isscreenon_fuzzer/BUILD.gn b/test/fuzztest/isscreenon_fuzzer/BUILD.gn index 92caaaea..b9e9d0f3 100644 --- a/test/fuzztest/isscreenon_fuzzer/BUILD.gn +++ b/test/fuzztest/isscreenon_fuzzer/BUILD.gn @@ -55,12 +55,12 @@ ohos_fuzztest("IsScreenOnFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", "c_utils:utils", + "cJSON:cjson", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", diff --git a/test/fuzztest/isstandby_fuzzer/BUILD.gn b/test/fuzztest/isstandby_fuzzer/BUILD.gn index 44ca320f..b9ed4b7c 100644 --- a/test/fuzztest/isstandby_fuzzer/BUILD.gn +++ b/test/fuzztest/isstandby_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("IsStandbyFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn b/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn index f99f36ff..1b2a35f8 100644 --- a/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn +++ b/test/fuzztest/lockscreenaftertimingout_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("LockScreenAfterTimingOutFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn b/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn index aaf55df9..05311192 100644 --- a/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn +++ b/test/fuzztest/lockunlockisused_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("LockUnLockIsUsedFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn b/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn index be8a9d70..aaeb5c7d 100644 --- a/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn +++ b/test/fuzztest/overridescreenofftime_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("OverrideScreenOffTimeFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/powermodecallback_fuzzer/BUILD.gn b/test/fuzztest/powermodecallback_fuzzer/BUILD.gn index 05e624bc..8f889612 100644 --- a/test/fuzztest/powermodecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/powermodecallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("PowerModeCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn b/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn index 99631610..3f267ada 100644 --- a/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/powerstatecallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("PowerStateCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn b/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn index 0d7ec59c..f9da0f0b 100644 --- a/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/proxyrunninglock_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ProxyRunningLockFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn b/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn index cc75a3c7..d595909b 100644 --- a/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn +++ b/test/fuzztest/proxyrunninglocks_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ProxyRunningLocksFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn b/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn index 87da8785..e606441d 100644 --- a/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn +++ b/test/fuzztest/queryrunninglocklists_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("QueryRunningLockListsFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/rebootdevice_fuzzer/BUILD.gn b/test/fuzztest/rebootdevice_fuzzer/BUILD.gn index 35dbb3df..fc9bc91b 100644 --- a/test/fuzztest/rebootdevice_fuzzer/BUILD.gn +++ b/test/fuzztest/rebootdevice_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("RebootDeviceFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/refreshactivity_fuzzer/BUILD.gn b/test/fuzztest/refreshactivity_fuzzer/BUILD.gn index f0b99caf..eb4b2051 100644 --- a/test/fuzztest/refreshactivity_fuzzer/BUILD.gn +++ b/test/fuzztest/refreshactivity_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("RefreshActivityFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn b/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn index f0e581a8..c0e92730 100644 --- a/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn +++ b/test/fuzztest/releaserunninglock_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ReleaseRunningLockFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn b/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn index 99b237fb..5042f7f9 100644 --- a/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn +++ b/test/fuzztest/resetrunninglocks_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ResetRunningLocksFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn b/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn index 06c7d790..4f79f85a 100644 --- a/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn +++ b/test/fuzztest/runninglockcallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("RunningLockCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn b/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn index f8bb7697..404ea65f 100644 --- a/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/screenstatecallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("ScreenStateCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn b/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn index 98e7ad48..14e9056d 100644 --- a/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn +++ b/test/fuzztest/setdisplaysuspend_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("SetDisplaySuspendFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn b/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn index 5d9d5a1e..a789cdc0 100644 --- a/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn +++ b/test/fuzztest/setforcetimingout_fuzzer/BUILD.gn @@ -60,12 +60,12 @@ ohos_fuzztest("SetForceTimingOutFuzzTest") { external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn b/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn index 7b5936de..909e6c3f 100644 --- a/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn +++ b/test/fuzztest/setsuspendtag_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("SetSuspendTagFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/shelldump_fuzzer/BUILD.gn b/test/fuzztest/shelldump_fuzzer/BUILD.gn index e9a29917..6cd02553 100644 --- a/test/fuzztest/shelldump_fuzzer/BUILD.gn +++ b/test/fuzztest/shelldump_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("ShellDumpFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn b/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn index 3347d1af..0baf9e91 100644 --- a/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn +++ b/test/fuzztest/shutdowndevice_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("ShutDownDeviceFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/suspenddevice_fuzzer/BUILD.gn b/test/fuzztest/suspenddevice_fuzzer/BUILD.gn index c1060f4e..280be3ca 100644 --- a/test/fuzztest/suspenddevice_fuzzer/BUILD.gn +++ b/test/fuzztest/suspenddevice_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("SuspendDeviceFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn b/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn index bcf4fc42..8b3c7f1e 100644 --- a/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn +++ b/test/fuzztest/synchibernatecallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("SyncHibernateCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn index 434bbde6..faff00c5 100644 --- a/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/syncshutdowncallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("SyncShutdownCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn b/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn index b5f014b7..f936cee1 100644 --- a/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn +++ b/test/fuzztest/syncsleepcallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("SyncSleepCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn b/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn index 924e544b..02130828 100644 --- a/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn +++ b/test/fuzztest/takeovershutdowncallback_fuzzer/BUILD.gn @@ -56,11 +56,11 @@ ohos_fuzztest("TakeOverShutdownCallbackFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn b/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn index e3a62225..62db1f4b 100644 --- a/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn +++ b/test/fuzztest/wakeupdevice_fuzzer/BUILD.gn @@ -55,11 +55,11 @@ ohos_fuzztest("WakeupDeviceFuzzTest") { "${powermgr_service_path}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/systemtest/BUILD.gn b/test/systemtest/BUILD.gn index b5b9d977..f4d4d47e 100644 --- a/test/systemtest/BUILD.gn +++ b/test/systemtest/BUILD.gn @@ -43,6 +43,7 @@ deps_ex = [ "ability_runtime:ability_manager", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c0587a5c..db48d9cf 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -61,6 +61,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "config_policy:configpolicy_util", @@ -70,7 +71,6 @@ deps_ex = [ "hilog:libhilog", "input:libmmi-client", "ipc:ipc_core", - "jsoncpp:jsoncpp", "libxml2:libxml2", "power_manager:power_ffrt", "safwk:system_ability_fwk", diff --git a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp index aa598bce..6389ced3 100644 --- a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp +++ b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp @@ -16,7 +16,7 @@ #include #include #include - +#include #include "axis_event.h" #include "input_device.h" #include "pointer_event.h" @@ -28,7 +28,6 @@ #include "power_mgr_service.h" #include "power_state_machine.h" #include "setting_helper.h" -#include "json/reader.h" using namespace testing::ext; using namespace OHOS::PowerMgr; @@ -380,19 +379,30 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest011, TestSize.Level "false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": true},\"xxx\": {\"enable\": false}}"; std::shared_ptr parseSources = std::make_shared(); - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: json parse error"; + return; + } + if (!cJSON_IsObject(root)) { + GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: root is not object"; + cJSON_Delete(root); + return; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - WakeupSourceParser::ParseSourcesProc(parseSources, valueObj, key); + cJSON* item = NULL; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + continue; + } + std::string keyStr = std::string(key); + WakeupSourceParser::ParseSourcesProc(parseSources, item, keyStr); } + + cJSON_Delete(root); + EXPECT_TRUE(parseSources->GetSourceList().size() != 0); GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest011 function end!"); diff --git a/test/unittest/src/power_parsesources_mock_test.cpp b/test/unittest/src/power_parsesources_mock_test.cpp index 36408109..9aad63d6 100644 --- a/test/unittest/src/power_parsesources_mock_test.cpp +++ b/test/unittest/src/power_parsesources_mock_test.cpp @@ -38,13 +38,13 @@ bool SettingHelper::IsSuspendSourcesSettingValid() } bool SuspendSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { return false; } bool WakeupSourceParser::ParseSourcesProc( - std::shared_ptr& parseSources, Json::Value& valueObj, std::string& key) + std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { return false; } diff --git a/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp b/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp index 4a527fcd..9e4e9992 100644 --- a/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp +++ b/test/unittest/src/scenario_test/wakeup_suspend/power_wakeup_test.cpp @@ -16,7 +16,7 @@ #include #include #include - +#include #include "axis_event.h" #include "input_device.h" #include "pointer_event.h" @@ -27,7 +27,6 @@ #include "power_mgr_client.h" #include "power_state_machine.h" #include "setting_helper.h" -#include "json/reader.h" using namespace testing::ext; using namespace OHOS::PowerMgr; diff --git a/test/unittest/src/servicetest/BUILD.gn b/test/unittest/src/servicetest/BUILD.gn index e1b4b1da..6e0ae1fc 100644 --- a/test/unittest/src/servicetest/BUILD.gn +++ b/test/unittest/src/servicetest/BUILD.gn @@ -52,6 +52,7 @@ deps_ex = [ "ability_base:base", "ability_base:want", "ability_runtime:ability_manager", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", diff --git a/utils/vibrator/BUILD.gn b/utils/vibrator/BUILD.gn index 8fc1dc9a..fa13dcbe 100644 --- a/utils/vibrator/BUILD.gn +++ b/utils/vibrator/BUILD.gn @@ -35,10 +35,10 @@ ohos_shared_library("power_vibrator") { public_configs = [ ":public_config" ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "config_policy:configpolicy_util", "hilog:libhilog", - "jsoncpp:jsoncpp", ] if (defined(global_parts_info) && defined(global_parts_info.sensors_miscdevice)) { diff --git a/utils/vibrator/include/vibrator_source_parser.h b/utils/vibrator/include/vibrator_source_parser.h index d2cad83b..6e3b10da 100644 --- a/utils/vibrator/include/vibrator_source_parser.h +++ b/utils/vibrator/include/vibrator_source_parser.h @@ -18,7 +18,7 @@ #include #include -#include "json/value.h" +#include namespace OHOS { namespace PowerMgr { @@ -56,7 +56,7 @@ private: std::vector ParseSources(const std::string& config); void GetTargetPath(std::string& targetPath, const std::string& etcPath, const std::string& vendorPath, const std::string& systemPath); - void ParseSourcesProc(std::vector& sources, Json::Value& valueObj, std::string& key); + void ParseSourcesProc(std::vector& sources, cJSON* valueObj, std::string& key); }; } // namespace PowerMgr } // namespace OHOS diff --git a/utils/vibrator/src/vibrator_source_parser.cpp b/utils/vibrator/src/vibrator_source_parser.cpp index 20f4e4b8..51f594fa 100644 --- a/utils/vibrator/src/vibrator_source_parser.cpp +++ b/utils/vibrator/src/vibrator_source_parser.cpp @@ -18,10 +18,9 @@ #include #include #include +#include #include "config_policy_utils.h" #include "power_log.h" -#include "json/reader.h" -#include "json/value.h" namespace OHOS { namespace PowerMgr { @@ -71,37 +70,64 @@ void VibratorSourceParser::GetTargetPath( std::vector VibratorSourceParser::ParseSources(const std::string& jsonStr) { std::vector sources; - Json::Reader reader; - Json::Value root; - if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), root)) { - POWER_HILOGE(COMP_UTILS, "json parse error"); + + if (jsonStr.empty()) { + POWER_HILOGE(COMP_UTILS, "Input JSON string is empty"); return sources; } - Json::Value::Members members = root.getMemberNames(); - for (auto iter = members.begin(); iter != members.end(); iter++) { - std::string key = *iter; - Json::Value valueObj = root[key]; - POWER_HILOGI(COMP_UTILS, "key=%{public}s", key.c_str()); - ParseSourcesProc(sources, valueObj, key); + + cJSON* root = cJSON_Parse(jsonStr.c_str()); + if (!root) { + POWER_HILOGE(COMP_UTILS, "JSON parse error"); + return sources; + } + if (!cJSON_IsObject(root)) { + POWER_HILOGE(COMP_UTILS, "JSON root is not object"); + cJSON_Delete(root); + return sources; } + + cJSON* item = nullptr; + cJSON_ArrayForEach(item, root) { + const char* key = item->string; + if (!key) { + POWER_HILOGE(COMP_UTILS, "invalid key in json object"); + continue; + } + std::string keyStr = std::string(key); + POWER_HILOGI(COMP_UTILS, "key=%{public}s", keyStr.c_str()); + ParseSourcesProc(sources, item, keyStr); + } + + cJSON_Delete(root); return sources; } void VibratorSourceParser::ParseSourcesProc( - std::vector& sources, Json::Value& valueObj, std::string& key) + std::vector& sources, cJSON* valueObj, std::string& key) { - if (!valueObj.isObject()) { + if (!cJSON_IsObject(valueObj)) { + POWER_HILOGE(COMP_UTILS, "ValueObj is not a json object."); return; } - std::string type; + bool enable = false; - Json::Value enableValue = valueObj[VibratorSource::ENABLE_KEY]; - Json::Value typeValue = valueObj[VibratorSource::TYPE_KEY]; - if (!typeValue.isString() || !enableValue.isBool()) { + std::string type; + + cJSON* enableItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::ENABLE_KEY); + if (!enableItem || !cJSON_IsBool(enableItem)) { + POWER_HILOGE(COMP_UTILS, "Parse enable error."); return; } - enable = enableValue.asBool(); - type = typeValue.asString(); + enable = cJSON_IsTrue(enableItem); + + cJSON* typeItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::TYPE_KEY); + if (!typeItem || !cJSON_IsString(typeItem)) { + POWER_HILOGE(COMP_UTILS, "Parse type error."); + return; + } + type = typeItem->valuestring; + if (!enable || type.empty()) { return; } -- Gitee