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 #include "res_sched_json_util.h"
17
18 #include <fstream>
19
20 #include "res_sched_file_util.h"
21 #include "res_sched_log.h"
22 #include "res_sched_string_util.h"
23
24 namespace OHOS {
25 namespace ResourceSchedule {
26 namespace ResCommonUtil {
ParseIntParameterFromJson(const std::string & name,int32_t & value,const nlohmann::json & jsonObj)27 bool ParseIntParameterFromJson(const std::string& name, int32_t &value, const nlohmann::json& jsonObj)
28 {
29 // if target exist.
30 if (!jsonObj.is_null() && !name.empty() && jsonObj.contains(name)) {
31 // target value is string, convert is to int.
32 if (jsonObj.at(name).is_string() && StrToInt32(jsonObj[name].get<std::string>(), value)) {
33 return true;
34 // target value is int, get and return directly.
35 } else if (jsonObj.at(name).is_number_integer()) {
36 value = jsonObj[name].get<int32_t>();
37 return true;
38 }
39 }
40 return false;
41 }
42
ParseStringParameterFromJson(const std::string & name,std::string & value,const nlohmann::json & jsonObj)43 bool ParseStringParameterFromJson(const std::string& name, std::string &value, const nlohmann::json& jsonObj)
44 {
45 if (!jsonObj.is_null() && !name.empty() && jsonObj.contains(name) && jsonObj.at(name).is_string()) {
46 value = jsonObj[name].get<std::string>();
47 return true;
48 }
49 return false;
50 }
51
ParseBoolParameterFromJson(const std::string & name,bool & value,const nlohmann::json & jsonObj)52 bool ParseBoolParameterFromJson(const std::string& name, bool& value, const nlohmann::json& jsonObj)
53 {
54 if (!jsonObj.is_null() && !name.empty() && jsonObj.contains(name) && jsonObj.at(name).is_boolean()) {
55 value = jsonObj[name].get<bool>();
56 return true;
57 }
58 return false;
59 }
60
LoadOneCfgFileToJsonObj(const std::string & relativeFilePath,nlohmann::json & jsonObj)61 bool LoadOneCfgFileToJsonObj(const std::string& relativeFilePath, nlohmann::json& jsonObj)
62 {
63 std::string absolutePath;
64 // judge input path exist.
65 if (!GetRealConfigPath(relativeFilePath, absolutePath) || absolutePath.empty()) {
66 RESSCHED_LOGE("%{public}s:get config file path failed.", __func__);
67 return false;
68 }
69 return LoadFileToJsonObj(absolutePath, jsonObj);
70 }
71
LoadFileToJsonObj(const std::string & absolutePath,nlohmann::json & jsonObj)72 bool LoadFileToJsonObj(const std::string& absolutePath, nlohmann::json& jsonObj)
73 {
74 char tmpPath[PATH_MAX + 1] = {0};
75 auto len = absolutePath.length();
76 if (len == 0 || len > PATH_MAX || !realpath(absolutePath.c_str(), tmpPath)) {
77 RESSCHED_LOGE("%{public}s: file path invalid", __func__);
78 return false;
79 }
80 std::string realConfigFile(tmpPath);
81 std::ifstream fileStream(realConfigFile);
82 // judge open success.
83 if (!fileStream.is_open()) {
84 RESSCHED_LOGE("%{public}s:open file fail.", __func__);
85 return false;
86 }
87 // parse file stream to json obj
88 jsonObj = nlohmann::json::parse(fileStream, nullptr, false);
89 fileStream.close();
90 // if parse fialed
91 if (jsonObj.is_discarded()) {
92 RESSCHED_LOGE("%{public}s:parse to json fail.", __func__);
93 return false;
94 }
95 return true;
96 }
97
GetArrayFromJson(const nlohmann::json & jsonObj,const std::string & name,nlohmann::json & value)98 bool GetArrayFromJson(const nlohmann::json& jsonObj, const std::string& name, nlohmann::json& value)
99 {
100 // check whether input is vaild.
101 if (jsonObj.is_null() || name.empty()) {
102 RESSCHED_LOGE("%{public}s:input err.", __func__);
103 return false;
104 }
105 // check json whether has this name's key.
106 if (jsonObj.find(name) == jsonObj.end() || !jsonObj[name].is_array()) {
107 RESSCHED_LOGE("%{public}s:parse err.", __func__);
108 return false;
109 }
110 value = jsonObj[name];
111 return true;
112 }
113
GetObjFromJson(const nlohmann::json & jsonObj,const std::string & name,nlohmann::json & value)114 bool GetObjFromJson(const nlohmann::json& jsonObj, const std::string& name, nlohmann::json& value)
115 {
116 // check whether input is vaild.
117 if (jsonObj.is_null() || name.empty()) {
118 RESSCHED_LOGE("%{public}s:input err.", __func__);
119 return false;
120 }
121 // check json whether has this name's key.
122 if (jsonObj.find(name) == jsonObj.end() || !jsonObj[name].is_object()) {
123 RESSCHED_LOGE("%{public}s:parse err.", __func__);
124 return false;
125 }
126 value = jsonObj[name];
127 return true;
128 }
129
LoadContentToJsonObj(const std::string & content,nlohmann::json & jsonObj)130 bool LoadContentToJsonObj(const std::string& content, nlohmann::json& jsonObj)
131 {
132 // check whether input is vaild.
133 if (content.empty()) {
134 RESSCHED_LOGE("%{public}s:input err.", __func__);
135 return false;
136 }
137 // parse content to a json obj.
138 jsonObj = nlohmann::json::parse(content, nullptr, false);
139 // check whether parse success.
140 if (jsonObj.is_discarded()) {
141 RESSCHED_LOGE("%{public}s:parse fail.", __func__);
142 return false;
143 }
144 // check json obj whether an obj.
145 if (!jsonObj.is_object()) {
146 RESSCHED_LOGE("%{public}s:the content is not an object.", __func__);
147 return false;
148 }
149 return true;
150 }
151
DumpJsonToString(const nlohmann::json & jsonObj,std::string & content)152 void DumpJsonToString(const nlohmann::json& jsonObj, std::string& content)
153 {
154 if (!jsonObj.is_object()) {
155 RESSCHED_LOGE("%{public}s:the input json is not an object.", __func__);
156 return;
157 }
158 content = jsonObj.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace);
159 }
160 }
161 } // namespace ResourceSchedule
162 } // namespace OHOS
163