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