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 HIVIEW_CJSON_UTILS_H
17 #define HIVIEW_CJSON_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "cJSON.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace CJsonUtil {
27 /**
28  * @brief Get the json root from a json format file
29  * @param configFile path of the json format file.
30  * @return the parsed json root value
31  */
32 cJSON* ParseJsonRoot(const std::string& configFile);
33 
34 /**
35  * @brief try to parse an integer value from json string.
36  * @param json json string.
37  * @param key key defined for the integer value.
38  * @param defaultValue default value when the integer value was parsed failed
39  * @return the parsed integer value.
40  */
41 int64_t GetIntValue(const cJSON* json, const std::string& key, int64_t defaultValue = 0);
42 
43 /**
44  * @brief try to parse a double value from json string.
45  * @param json json string.
46  * @param key key defined for the double value.
47  * @param defaultValue default value when the double value was parsed failed
48  * @return the parsed double value.
49  */
50 double GetDoubleValue(cJSON* json, const std::string& key, double defaultValue = 0.0);
51 
52 /**
53  * @brief try to parse a string value from json string.
54  * @param json json string.
55  * @param key key defined for the string value.
56  * @return the parsed string value.
57  */
58 std::string GetStringValue(cJSON* json, const std::string& key);
59 
60 /**
61  * @brief try to parse a string array value from json string.
62  * @param json json string.
63  * @param key key defined for the string array value.
64  * @param dest the parsed string array value.
65  */
66 void GetStringArray(cJSON* json, const std::string& key, std::vector<std::string>& dest);
67 
68 /**
69  * @brief try to parse an object value from json object.
70  * @param json json object.
71  * @param key key defined for the object value.
72  * @return the parsed object value.
73  */
74 cJSON* GetObjectValue(const cJSON* json, const std::string& key);
75 
76 /**
77  * @brief try to parse an object value from json object.
78  * @param json json object.
79  * @param key key defined for the object value.
80  * @return the parsed object value.
81  */
82 cJSON* GetArrayValue(const cJSON* json, const std::string& key);
83 
84 /**
85  * @brief try to parse an object value from json object.
86  * @param json json object.
87  * @param key key defined for the object value.
88  * @param value parsed value.
89  * @return parsed result.
90  */
91 bool GetBoolValue(const cJSON* json, const std::string& key, bool& value);
92 };
93 } // HiviewDFX
94 } // OHOS
95 
96 #endif // HIVIEW_CJSON_UTILS_H
97