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 "json_utils.h"
17
18 #include <iostream>
19 #include <fstream>
20
21 #include "log.h"
22 #include "utils.h"
23
24 namespace OHOS {
25 namespace AppPackingTool {
26 namespace {
27 const std::string MODULE_JSON = "module.json";
28 const std::string CONFIG_JSON = "config.json";
29 const std::string PATCH_JSON = "patch.json";
30 }
31
IsModuleJson(const std::string & filePath)32 bool JsonUtils::IsModuleJson(const std::string& filePath)
33 {
34 return Utils::CheckFileName(filePath, MODULE_JSON);
35 }
36
IsConfigJson(const std::string & filePath)37 bool JsonUtils::IsConfigJson(const std::string& filePath)
38 {
39 return Utils::CheckFileName(filePath, CONFIG_JSON);
40 }
41
IsPatchJson(const std::string & filePath)42 bool JsonUtils::IsPatchJson(const std::string& filePath)
43 {
44 return Utils::CheckFileName(filePath, PATCH_JSON);
45 }
46
JsonFromFile(const std::string & filePath)47 std::unique_ptr<PtJson> JsonUtils::JsonFromFile(const std::string& filePath)
48 {
49 std::string realFilePath;
50 if (!Utils::GetRealPath(filePath, realFilePath)) {
51 LOGE("get real file path failed! jsonFile=%s", filePath.c_str());
52 return nullptr;
53 }
54 std::ifstream inFile(realFilePath, std::ios::in);
55 if (!inFile.is_open()) {
56 LOGE("open file failed![filePath=%s][realfilePath=%s]", filePath.c_str(), realFilePath.c_str());
57 return nullptr;
58 }
59 std::string fileContent((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
60 inFile.close();
61 return PtJson::Parse(fileContent);
62 }
63
JsonToFile(const std::unique_ptr<PtJson> & json,const std::string & filePath)64 bool JsonUtils::JsonToFile(const std::unique_ptr<PtJson>& json, const std::string& filePath)
65 {
66 if (json.get() == nullptr) {
67 return false;
68 }
69 return StrToFile(json->Stringify(), filePath);
70 }
71
StrToFile(const std::string & str,const std::string & filePath)72 bool JsonUtils::StrToFile(const std::string& str, const std::string& filePath)
73 {
74 std::string realFilePath;
75 if (!Utils::GetRealPathOfNoneExistFile(filePath, realFilePath)) {
76 LOGE("get real file path failed! jsonFile=%s", filePath.c_str());
77 return false;
78 }
79 std::ofstream outFile(realFilePath, std::ios::out);
80 if (!outFile.is_open()) {
81 LOGE("open file failed![filePath=%s][realfilePath=%s]", filePath.c_str(), realFilePath.c_str());
82 return false;
83 }
84 outFile << str;
85 outFile.close();
86 return true;
87 }
88 } // namespace AppPackingTool
89 } // namespace OHOS
90