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 "cjson_util.h"
17
18 #include <fstream>
19 #include <string>
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace CJsonUtil {
ParseJsonRoot(const std::string & configFile)24 cJSON* ParseJsonRoot(const std::string& configFile)
25 {
26 std::ifstream jsonFileStream(configFile, std::ios::in);
27 if (!jsonFileStream.is_open()) {
28 return nullptr;
29 }
30 std::string content((std::istreambuf_iterator<char>(jsonFileStream)), std::istreambuf_iterator<char>());
31 cJSON* root = cJSON_Parse(content.c_str());
32 jsonFileStream.close();
33 return root;
34 }
35
GetIntValue(const cJSON * json,const std::string & key,int64_t defaultValue)36 int64_t GetIntValue(const cJSON* json, const std::string& key, int64_t defaultValue)
37 {
38 if (json == nullptr || !cJSON_IsObject(json)) {
39 return defaultValue;
40 }
41 cJSON* intJson = cJSON_GetObjectItem(json, key.c_str());
42 if (intJson == nullptr || !cJSON_IsNumber(intJson)) {
43 return defaultValue;
44 }
45 return static_cast<int64_t>(intJson->valuedouble);
46 }
47
GetDoubleValue(cJSON * json,const std::string & key,double defaultValue)48 double GetDoubleValue(cJSON* json, const std::string& key, double defaultValue)
49 {
50 if (json == nullptr || !cJSON_IsObject(json)) {
51 return defaultValue;
52 }
53 cJSON* doubleJson = cJSON_GetObjectItem(json, key.c_str());
54 if (doubleJson == nullptr || !cJSON_IsNumber(doubleJson)) {
55 return defaultValue;
56 }
57 return doubleJson->valuedouble;
58 }
59
GetStringValue(cJSON * json,const std::string & key)60 std::string GetStringValue(cJSON* json, const std::string& key)
61 {
62 if (json == nullptr || !cJSON_IsObject(json)) {
63 return "";
64 }
65 cJSON* str = cJSON_GetObjectItem(json, key.c_str());
66 if (str == nullptr || !cJSON_IsString(str)) {
67 return "";
68 }
69 return str->valuestring;
70 }
71
GetStringArray(cJSON * json,const std::string & key,std::vector<std::string> & dest)72 void GetStringArray(cJSON* json, const std::string& key, std::vector<std::string>& dest)
73 {
74 if (json == nullptr || !cJSON_IsObject(json)) {
75 return;
76 }
77 cJSON* strArray = cJSON_GetObjectItem(json, key.c_str());
78 if (strArray == nullptr || !cJSON_IsArray(strArray)) {
79 return;
80 }
81 int size = cJSON_GetArraySize(strArray);
82 if (size <= 0) {
83 return;
84 }
85 for (int index = 0; index < size; ++index) {
86 cJSON* strItem = cJSON_GetArrayItem(strArray, index);
87 if (strItem == nullptr || !cJSON_IsString(strItem)) {
88 continue;
89 }
90 dest.emplace_back(strItem->valuestring);
91 }
92 }
93
GetObjectValue(const cJSON * json,const std::string & key)94 cJSON* GetObjectValue(const cJSON* json, const std::string& key)
95 {
96 if (json == nullptr || !cJSON_IsObject(json)) {
97 return nullptr;
98 }
99 cJSON* obj = cJSON_GetObjectItem(json, key.c_str());
100 if (obj == nullptr || !cJSON_IsObject(obj)) {
101 return nullptr;
102 }
103 return obj;
104 }
105
GetArrayValue(const cJSON * json,const std::string & key)106 cJSON* GetArrayValue(const cJSON* json, const std::string& key)
107 {
108 if (json == nullptr || !cJSON_IsObject(json)) {
109 return nullptr;
110 }
111 cJSON* value = cJSON_GetObjectItem(json, key.c_str());
112 if (value == nullptr || !cJSON_IsArray(value)) {
113 return nullptr;
114 }
115 return value;
116 }
117
GetBoolValue(const cJSON * json,const std::string & key,bool & value)118 bool GetBoolValue(const cJSON* json, const std::string& key, bool& value)
119 {
120 if (json == nullptr || !cJSON_IsObject(json)) {
121 return false;
122 }
123 cJSON* boolJson = cJSON_GetObjectItem(json, key.c_str());
124 if (boolJson == nullptr || !cJSON_IsBool(boolJson)) {
125 return false;
126 }
127 value = cJSON_IsTrue(boolJson);
128 return true;
129 }
130 } // namespace CJsonUtil
131 } // namespace HiviewDFX
132 } // namespace OHOS