1 /*
2 * Copyright (c) 2022-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 #include "json_parser.h"
16
17 #include "file_utils.h"
18 #include "sensors_errors.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "JsonParser"
22
23 namespace OHOS {
24 namespace Sensors {
25
JsonParser(const std::string & filePath)26 JsonParser::JsonParser(const std::string &filePath)
27 {
28 std::string jsonStr = ReadJsonFile(filePath);
29 if (jsonStr.empty()) {
30 MISC_HILOGE("Read json file fail");
31 return;
32 }
33 cJson_ = cJSON_Parse(jsonStr.c_str());
34 }
35
JsonParser(const RawFileDescriptor & rawFd)36 JsonParser::JsonParser(const RawFileDescriptor &rawFd)
37 {
38 std::string jsonStr = ReadFd(rawFd);
39 if (jsonStr.empty()) {
40 MISC_HILOGE("Read fd fail");
41 return;
42 }
43 cJson_ = cJSON_Parse(jsonStr.c_str());
44 }
45
~JsonParser()46 JsonParser::~JsonParser()
47 {
48 if (cJson_ != nullptr) {
49 cJSON_Delete(cJson_);
50 }
51 }
52
HasObjectItem(cJSON * json,const std::string & key) const53 bool JsonParser::HasObjectItem(cJSON *json, const std::string &key) const
54 {
55 return cJSON_HasObjectItem(json, key.c_str());
56 }
57
HasObjectItem(const std::string & key) const58 bool JsonParser::HasObjectItem(const std::string &key) const
59 {
60 return HasObjectItem(cJson_, key.c_str());
61 }
62
GetObjectItem(cJSON * json,const std::string & key) const63 cJSON *JsonParser::GetObjectItem(cJSON *json, const std::string &key) const
64 {
65 if (!cJSON_IsObject(json)) {
66 MISC_HILOGE("The json is not object");
67 return nullptr;
68 }
69 if (!cJSON_HasObjectItem(json, key.c_str())) {
70 MISC_HILOGE("The json is not data:%{public}s", key.c_str());
71 return nullptr;
72 }
73 return cJSON_GetObjectItem(json, key.c_str());
74 }
75
GetObjectItem(const std::string & key) const76 cJSON *JsonParser::GetObjectItem(const std::string &key) const
77 {
78 return GetObjectItem(cJson_, key);
79 }
80
ParseJsonArray(cJSON * json,const std::string & key,std::vector<std::string> & vals) const81 int32_t JsonParser::ParseJsonArray(cJSON *json, const std::string &key,
82 std::vector<std::string> &vals) const
83 {
84 cJSON *jsonArray = GetObjectItem(json, key);
85 if (!cJSON_IsArray(jsonArray)) {
86 MISC_HILOGE("The value of %{public}s is not array", key.c_str());
87 return ERROR;
88 }
89 int32_t size = cJSON_GetArraySize(jsonArray);
90 for (int32_t i = 0; i < size; ++i) {
91 cJSON *val = cJSON_GetArrayItem(jsonArray, i);
92 if ((!cJSON_IsString(val)) || (val->valuestring == nullptr)) {
93 MISC_HILOGE("The value of index %{public}d is not string", i);
94 return ERROR;
95 }
96 vals.push_back(val->valuestring);
97 }
98 return SUCCESS;
99 }
100
ParseJsonArray(const std::string & key,std::vector<std::string> & vals) const101 int32_t JsonParser::ParseJsonArray(const std::string &key, std::vector<std::string> &vals) const
102 {
103 return ParseJsonArray(cJson_, key, vals);
104 }
105
IsArray(cJSON * json) const106 bool JsonParser::IsArray(cJSON *json) const
107 {
108 return cJSON_IsArray(json);
109 }
110
GetArraySize(cJSON * json) const111 int32_t JsonParser::GetArraySize(cJSON *json) const
112 {
113 return cJSON_GetArraySize(json);
114 }
115
GetArrayItem(cJSON * json,int32_t index) const116 cJSON *JsonParser::GetArrayItem(cJSON *json, int32_t index) const
117 {
118 return cJSON_GetArrayItem(json, index);
119 }
120
GetIntValue(cJSON * json) const121 int32_t JsonParser::GetIntValue(cJSON *json) const
122 {
123 if (!cJSON_IsNumber(json)) {
124 return static_cast<int32_t>(NAN);
125 }
126 return json->valueint;
127 }
128
GetDoubleValue(cJSON * json) const129 double JsonParser::GetDoubleValue(cJSON *json) const
130 {
131 if (!cJSON_IsNumber(json)) {
132 return static_cast<double>(NAN);
133 }
134 return json->valuedouble;
135 }
136
GetStringValue(cJSON * json) const137 std::string JsonParser::GetStringValue(cJSON *json) const
138 {
139 if (!cJSON_IsString(json)) {
140 return NULL;
141 }
142 return json->valuestring;
143 }
144 } // namespace Sensors
145 } // namespace OHOS