1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H 17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H 18 19 #include <string> 20 21 #include "nlohmann/json.hpp" 22 #include "standby_service_errors.h" 23 24 namespace OHOS { 25 namespace DevStandbyMgr { 26 class JsonUtils { 27 public: 28 JsonUtils() = delete; 29 30 /** 31 * @brief load json value from file 32 * 33 * @param jsonValue json value from the file content 34 * @param filePath the absolute file path 35 * @return true true if succeed 36 */ 37 static bool LoadJsonValueFromFile(nlohmann::json& jsonValue, const std::string& filePath); 38 39 /** 40 * @brief load json value from string 41 * 42 * @param jsonValue json value from the string 43 * @param content string content 44 * @return true true if succeed 45 */ 46 static bool LoadJsonValueFromContent(nlohmann::json& jsonValue, const std::string& content); 47 48 /** 49 * @brief dump json value to file 50 * 51 * @param jsonValue json value to be dumped to the file 52 * @param filePath the absolute file path to dump json value 53 * @return true true true if succeed 54 */ 55 static bool DumpJsonValueToFile(const nlohmann::json& jsonValue, const std::string& filePath); 56 57 /** 58 * @brief get the 32-bit int from json value object 59 * 60 * @param jsonValue json object 61 * @param key the name of int object 62 * @param value value of the int object 63 * @return true true if succeed 64 */ 65 static bool GetInt32FromJsonValue(const nlohmann::json& jsonValue, const std::string& key, int32_t& value); 66 67 /** 68 * @brief get the bool from json value object 69 * 70 * @param jsonValue json object 71 * @param key the name of bool object 72 * @param value value of the bool object 73 * @return true if succeed 74 */ 75 static bool GetBoolFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, bool& value); 76 77 /** 78 * @brief get the string from json value object 79 * 80 * @param jsonValue json object 81 * @param key the name of string object 82 * @param value value of the string object 83 * @return true if succeed 84 */ 85 static bool GetStringFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, std::string& value); 86 87 /** 88 * @brief Get the Obj from json value object 89 * 90 * @param jsonValue json object 91 * @param key the name of json object 92 * @param value c 93 * @return true if succeed 94 */ 95 static bool GetObjFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value); 96 97 /** 98 * @brief Get the Array from json value object 99 * 100 * @param jsonValue json object 101 * @param key the name of array object 102 * @param value value of array object 103 * @return true if succeed 104 */ 105 static bool GetArrayFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value); 106 107 /** 108 * @brief Get the String Array from json value object 109 * 110 * @param jsonValue json object 111 * @param key the name of string array object 112 * @param strArray string array 113 * @return true if succeed 114 */ 115 static bool GetStrArrFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, 116 std::vector<std::string>& strArray); 117 static bool GetRealPath(const std::string& partialPath, std::string& fullPath); 118 static std::vector<std::string> SplitVersion(const std::string& versionStr, char versionDelim); 119 private: 120 static bool CreateNodeFile(const std::string &filePath); 121 static bool GetFileContent(const std::string& filePath, std::string& content); 122 }; 123 } // namespace DevStandbyMgr 124 } // namespace OHOS 125 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H 126