• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "nlohmann/json.hpp"
21 
22 #include "standby_service_errors.h"
23 #include "standby_service_log.h"
24 
25 namespace OHOS {
26 namespace DevStandbyMgr {
27 class JsonUtils {
28 public:
29     JsonUtils() = delete;
30 
31     /**
32      * @brief load json value from file
33      *
34      * @param jsonValue json value from the file content
35      * @param filePath the absolute file path
36      * @return true true if succeed
37      */
38     static bool LoadJsonValueFromFile(nlohmann::json& jsonValue, const std::string& filePath);
39 
40     /**
41      * @brief dump json value to file
42      *
43      * @param jsonValue json value to be dumped to the file
44      * @param filePath the absolute file path to dump json value
45      * @return true true true if succeed
46      */
47     static bool DumpJsonValueToFile(const nlohmann::json& jsonValue, const std::string& filePath);
48 
49     /**
50      * @brief get the 32-bit int from json value object
51      *
52      * @param jsonValue json object
53      * @param key the name of int object
54      * @param value value of the int object
55      * @return true true if succeed
56      */
57     static bool GetInt32FromJsonValue(const nlohmann::json& jsonValue, const std::string& key, int32_t& value);
58 
59     /**
60      * @brief get the bool from json value object
61      *
62      * @param jsonValue json object
63      * @param key the name of bool object
64      * @param value value of the bool object
65      * @return true if succeed
66      */
67     static bool GetBoolFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, bool& value);
68 
69     /**
70      * @brief get the string from json value object
71      *
72      * @param jsonValue json object
73      * @param key the name of string object
74      * @param value value of the string object
75      * @return true if succeed
76      */
77     static bool GetStringFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, std::string& value);
78 
79     /**
80      * @brief Get the Obj from json value object
81      *
82      * @param jsonValue json object
83      * @param key the name of json object
84      * @param value c
85      * @return true if succeed
86      */
87     static bool GetObjFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value);
88 
89     /**
90      * @brief Get the Array from json value object
91      *
92      * @param jsonValue json object
93      * @param key the name of array object
94      * @param value value of array object
95      * @return true if succeed
96      */
97     static bool GetArrayFromJsonValue(const nlohmann::json& jsonValue, const std::string& key, nlohmann::json& value);
98 
99     /**
100      * @brief Get the String Array from json value object
101      *
102      * @param jsonValue json object
103      * @param key the name of string array object
104      * @param strArray string array
105      * @return true if succeed
106      */
107     static bool GetStrArrFromJsonValue(const nlohmann::json& jsonValue, const std::string& key,
108         std::vector<std::string>& strArray);
109     static bool GetRealPath(const std::string& partialPath, std::string& fullPath);
110 private:
111     static bool CreateNodeFile(const std::string &filePath);
112     static bool GetFileContent(const std::string& filePath, std::string& content);
113 };
114 }  // namespace DevStandbyMgr
115 }  // namespace OHOS
116 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_UTILS_POLICY_INCLUDE_JSON_UTILS_H
117