1 /* 2 * Copyright (c) 2024 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 RESSCHED_COMMON_INCLUDE_RES_SCHED_JSON_UTIL 17 #define RESSCHED_COMMON_INCLUDE_RES_SCHED_JSON_UTIL 18 #include <string> 19 20 #include "nlohmann/json.hpp" 21 22 namespace OHOS { 23 namespace ResourceSchedule { 24 namespace ResCommonUtil { 25 26 /** 27 * @brief Parse a int value with key name from the json object. 28 * 29 * @param name the key of the the target item 30 * @param value the value of the target item 31 * @param jsonObj the target object 32 * @return 'true' if parse success 33 * 34 */ 35 bool ParseIntParameterFromJson(const std::string& name, int32_t &value, const nlohmann::json& jsonObj); 36 37 /** 38 * @brief Parse a string value with key name from the json object. 39 * 40 * @param name the key of the the target item 41 * @param value the value of the target item 42 * @param jsonObj the target object 43 * @return 'true' if parse success 44 * 45 */ 46 bool ParseStringParameterFromJson(const std::string& name, std::string &value, const nlohmann::json& jsonObj); 47 48 /** 49 * @brief Parse a bool value with key name from the json object. 50 * 51 * @param name the key of the the target item 52 * @param value the value of the target item 53 * @param jsonObj the target object 54 * @return 'true' if parse success 55 * 56 */ 57 bool ParseBoolParameterFromJson(const std::string& name, bool& value, const nlohmann::json& jsonObj); 58 59 /** 60 * @brief Get a array value with key name from the json object. 61 * 62 * @param jsonObj the target object 63 * @param name the key of the the target item 64 * @param value the value of the target item 65 * @return 'true' if get success 66 * 67 */ 68 bool GetArrayFromJson(const nlohmann::json& jsonObj, const std::string& name, nlohmann::json& value); 69 70 /** 71 * @brief Get a obj value with key name from the json object. 72 * 73 * @param jsonObj the target object 74 * @param name the key of the the target item 75 * @param value the value of the target item 76 * @return 'true' if get success 77 * 78 */ 79 bool GetObjFromJson(const nlohmann::json& jsonObj, const std::string& name, nlohmann::json& value); 80 81 /** 82 * @brief Load the file's content to a json object. 83 * 84 * @param absolutePath the file's absolutePath path 85 * @param jsonObj the target object 86 * @return 'true' if parse success 87 * 88 */ 89 bool LoadFileToJsonObj(const std::string& absolutePath, nlohmann::json& jsonObj); 90 91 /** 92 * @brief Load the config file's content to a json object. 93 * 94 * @param relativeFilePath the config file's relative path 95 * @param jsonObj the target object 96 * @return 'true' if parse success 97 * 98 */ 99 bool LoadOneCfgFileToJsonObj(const std::string& relativeFilePath, nlohmann::json& jsonObj); 100 101 /** 102 * @brief Load the string content to a json object. 103 * 104 * @param content the string content 105 * @param jsonObj the target object 106 * @return 'true' if parse success 107 * 108 */ 109 bool LoadContentToJsonObj(const std::string& content, nlohmann::json& jsonObj); 110 111 /** 112 * @brief Dump json to string. 113 * 114 * @param jsonObj the json object 115 * @param content the string format content 116 * 117 */ 118 void DumpJsonToString(const nlohmann::json& jsonObj, std::string& content); 119 } 120 } // namespace ResourceSchedule 121 } // namespace OHOS 122 123 #endif // RESSCHED_COMMON_INCLUDE_RES_SCHED_JSON_UTIL