• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ABILITY_RUNTIME_JSON_UTILS_H
17 #define OHOS_ABILITY_RUNTIME_JSON_UTILS_H
18 
19 #include <string>
20 #include <map>
21 #include <optional>
22 
23 #include "nlohmann/json.hpp"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 /**
29  * @class JsonUtils
30  * provides json utilities.
31  */
32 class JsonUtils {
33 public:
34     /**
35      * GetInstance, get an instance of JsonUtils.
36      *
37      * @return an instance of JsonUtils.
38      */
GetInstance()39     static JsonUtils &GetInstance()
40     {
41         static JsonUtils instance;
42         return instance;
43     }
44 
45     /**
46      * JsonUtils, destructor.
47      *
48      */
49     ~JsonUtils() = default;
50 
51     /**
52      * LoadConfiguration, load configuration.
53      *
54      * @param path The json file path.
55      * @param jsonBuf The json buffer to be returned.
56      * @param defaultPath The default output path.
57      * @return Whether or not the load operation succeeds.
58      */
59     bool LoadConfiguration(const std::string& path, nlohmann::json& jsonBuf, const std::string& defaultPath = "");
60 
61     /**
62      * IsEqual, check if json object contains certain key.
63      *
64      * @param jsonObject The json object.
65      * @param key The key.
66      * @param value The string value.
67      * @param checkEmpty The flag indicates whether the value can be empty.
68      * @return Whether or not the json object contains certain key.
69      */
70     bool IsEqual(nlohmann::json &jsonObject, const std::string &key,
71         const std::string &value, bool checkEmpty = false);
72 
73     /**
74      * IsEqual, check if json object contains certain key.
75      *
76      * @param jsonObject The json object.
77      * @param key The key.
78      * @param value The int32_t value.
79      * @return Whether or not the json object contains certain key.
80      */
81     bool IsEqual(nlohmann::json &jsonObject, const std::string &key, int32_t value);
82 
83     /**
84      * parse json to optional bool.
85      *
86      * @param jsonObject The json object.
87      * @param key The key.
88      * @return optional boolean value.
89      */
90     std::optional<bool> JsonToOptionalBool(const nlohmann::json &jsonObject, const std::string &key);
91 
92 private:
93     std::string GetConfigPath(const std::string& path, const std::string& defaultPath);
94     bool ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf);
95     JsonUtils() = default;
96     DISALLOW_COPY_AND_MOVE(JsonUtils);
97 };
98 }  // namespace AAFwk
99 }  // namespace OHOS
100 #endif  // OHOS_ABILITY_RUNTIME_JSON_UTILS_H