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 #include "json_utils.h"
17
18 #include <fcntl.h>
19 #include <fstream>
20 #include <unistd.h>
21 #include <climits>
22 #include <sstream>
23 #include <fstream>
24
25 namespace OHOS {
26 namespace DevStandbyMgr {
27 namespace {
28 constexpr uint32_t JSON_FORMAT = 4;
29 }
30
LoadJsonValueFromFile(nlohmann::json & jsonValue,const std::string & filePath)31 bool JsonUtils::LoadJsonValueFromFile(nlohmann::json& jsonValue, const std::string& filePath)
32 {
33 std::string content;
34 if (!GetFileContent(filePath, content)) {
35 STANDBYSERVICE_LOGE("failed to load content from %{public}s", filePath.c_str());
36 return false;
37 }
38 if (content.empty()) {
39 STANDBYSERVICE_LOGE("content of %{public}s is empty", filePath.c_str());
40 return false;
41 }
42 jsonValue = nlohmann::json::parse(content, nullptr, false);
43 if (jsonValue.is_discarded()) {
44 STANDBYSERVICE_LOGE("failed to parse content from %{public}s", filePath.c_str());
45 return false;
46 }
47 if (!jsonValue.is_object()) {
48 STANDBYSERVICE_LOGE("the content of %{public}s is not an object ", filePath.c_str());
49 return false;
50 }
51 return true;
52 }
53
DumpJsonValueToFile(const nlohmann::json & jsonValue,const std::string & filePath)54 bool JsonUtils::DumpJsonValueToFile(const nlohmann::json& jsonValue, const std::string& filePath)
55 {
56 if (!CreateNodeFile(filePath)) {
57 STANDBYSERVICE_LOGE("create file failed.");
58 return false;
59 }
60 std::ofstream fout;
61 std::string realPath;
62 if (!GetRealPath(filePath, realPath)) {
63 STANDBYSERVICE_LOGE("get real file path: %{public}s failed", filePath.c_str());
64 return false;
65 }
66 fout.open(realPath, std::ios::out);
67 if (!fout.is_open()) {
68 STANDBYSERVICE_LOGE("open file: %{public}s failed.", filePath.c_str());
69 return false;
70 }
71 fout << jsonValue.dump(JSON_FORMAT).c_str() << std::endl;
72 fout.close();
73 return true;
74 }
75
CreateNodeFile(const std::string & filePath)76 bool JsonUtils::CreateNodeFile(const std::string &filePath)
77 {
78 if (access(filePath.c_str(), F_OK) == ERR_OK) {
79 STANDBYSERVICE_LOGD("the standby service config file: %{public}s already exists.", filePath.c_str());
80 return true;
81 }
82 std::string fullpath {""};
83 if (!GetRealPath(filePath, fullpath)) {
84 STANDBYSERVICE_LOGD("the standby service config file: %{public}s not exists.", filePath.c_str());
85 fullpath = filePath;
86 }
87 int32_t fd = open(fullpath.c_str(), O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
88 if (fd < ERR_OK) {
89 STANDBYSERVICE_LOGE("Fail to open file: %{public}s", fullpath.c_str());
90 return false;
91 }
92 close(fd);
93 return true;
94 }
95
GetInt32FromJsonValue(const nlohmann::json & jsonValue,const std::string & key,int32_t & value)96 bool JsonUtils::GetInt32FromJsonValue(const nlohmann::json& jsonValue, const std::string& key, int32_t& value)
97 {
98 if (jsonValue.empty() || key.empty()) {
99 return false;
100 }
101 if (jsonValue.contains(key) == 0 || !jsonValue.at(key).is_number_integer()) {
102 return false;
103 }
104 value = jsonValue.at(key).get<int32_t>();
105 return true;
106 }
107
GetBoolFromJsonValue(const nlohmann::json & jsonValue,const std::string & key,bool & value)108 bool JsonUtils::GetBoolFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, bool& value)
109 {
110 if (jsonValue.empty() || key.empty()) {
111 return false;
112 }
113 if (jsonValue.contains(key) == 0 || !jsonValue.at(key).is_boolean()) {
114 return false;
115 }
116 value = jsonValue.at(key).get<bool>();
117 return true;
118 }
119
GetStringFromJsonValue(const nlohmann::json & jsonValue,const std::string & key,std::string & value)120 bool JsonUtils::GetStringFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, std::string& value)
121 {
122 if (jsonValue.empty() || key.empty()) {
123 return false;
124 }
125 if (jsonValue.contains(key) == 0 || !jsonValue.at(key).is_string()) {
126 return false;
127 }
128 value = jsonValue.at(key).get<std::string>();
129 return true;
130 }
131
GetObjFromJsonValue(const nlohmann::json & jsonValue,const std::string & key,nlohmann::json & value)132 bool JsonUtils::GetObjFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value)
133 {
134 if (jsonValue.empty() || key.empty()) {
135 return false;
136 }
137 if (jsonValue.contains(key) == 0 || !jsonValue.at(key).is_object()) {
138 return false;
139 }
140 value = jsonValue.at(key);
141 return true;
142 }
143
GetArrayFromJsonValue(const nlohmann::json & jsonValue,const std::string & key,nlohmann::json & value)144 bool JsonUtils::GetArrayFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value)
145 {
146 if (jsonValue.empty() || key.empty()) {
147 return false;
148 }
149 if (jsonValue.contains(key) == 0 || !jsonValue.at(key).is_array()) {
150 return false;
151 }
152 value = jsonValue.at(key);
153 return true;
154 }
155
GetStrArrFromJsonValue(const nlohmann::json & jsonValue,const std::string & key,std::vector<std::string> & strArray)156 bool JsonUtils::GetStrArrFromJsonValue(const nlohmann::json& jsonValue, const std::string& key,
157 std::vector<std::string>& strArray)
158 {
159 nlohmann::json strArrayValue;
160 if (!JsonUtils::GetArrayFromJsonValue(jsonValue, key, strArrayValue)) {
161 return false;
162 }
163 strArray.clear();
164 for (const auto &strItem : strArrayValue) {
165 if (!strItem.is_string()) {
166 return false;
167 }
168 strArray.emplace_back(strItem.get<std::string>());
169 }
170 return true;
171 }
172
GetFileContent(const std::string & filePath,std::string & content)173 bool JsonUtils::GetFileContent(const std::string& filePath, std::string& content)
174 {
175 std::string fullPath;
176 if (!GetRealPath(filePath, fullPath)) {
177 return false;
178 }
179 STANDBYSERVICE_LOGD("full path of standby service config file is: %{public}s ", fullPath.c_str());
180 std::ifstream fin(fullPath);
181 if (!fin.is_open()) {
182 return false;
183 }
184 std::ostringstream ss;
185 ss << fin.rdbuf();
186 content = ss.str();
187 return true;
188 }
189
GetRealPath(const std::string & partialPath,std::string & fullPath)190 bool JsonUtils::GetRealPath(const std::string& partialPath, std::string& fullPath)
191 {
192 char tmpPath[PATH_MAX] = {0};
193 if (partialPath.size() > PATH_MAX || !realpath(partialPath.c_str(), tmpPath)) {
194 return false;
195 }
196 fullPath = tmpPath;
197 return true;
198 }
199 } // namespace DevStandbyMgr
200 } // namespace OHOS