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 <cmath>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include "file_utils.h"
22 #include "sensors_errors.h"
23 
24 #undef LOG_TAG
25 #define LOG_TAG "JsonParser"
26 
27 namespace OHOS {
28 namespace Sensors {
29 
JsonParser(const std::string & filePath)30 JsonParser::JsonParser(const std::string &filePath)
31 {
32     std::string jsonStr = ReadJsonFile(filePath);
33     if (jsonStr.empty()) {
34         MISC_HILOGE("Read json file fail");
35         return;
36     }
37     cJson_ = cJSON_Parse(jsonStr.c_str());
38 }
39 
JsonParser(const RawFileDescriptor & rawFd)40 JsonParser::JsonParser(const RawFileDescriptor &rawFd)
41 {
42     std::string jsonStr = ReadFd(rawFd);
43     if (jsonStr.empty()) {
44         MISC_HILOGE("Read fd fail");
45         return;
46     }
47     cJson_ = cJSON_Parse(jsonStr.c_str());
48 }
49 
~JsonParser()50 JsonParser::~JsonParser()
51 {
52     if (cJson_ != nullptr) {
53         cJSON_Delete(cJson_);
54     }
55 }
56 
HasObjectItem(cJSON * json,const std::string & key) const57 bool JsonParser::HasObjectItem(cJSON *json, const std::string &key) const
58 {
59     return cJSON_HasObjectItem(json, key.c_str());
60 }
61 
HasObjectItem(const std::string & key) const62 bool JsonParser::HasObjectItem(const std::string &key) const
63 {
64     return HasObjectItem(cJson_, key.c_str());
65 }
66 
GetObjectItem(cJSON * json,const std::string & key) const67 cJSON *JsonParser::GetObjectItem(cJSON *json, const std::string &key) const
68 {
69     if (!cJSON_IsObject(json)) {
70         MISC_HILOGE("The json is not object");
71         return nullptr;
72     }
73     if (!cJSON_HasObjectItem(json, key.c_str())) {
74         MISC_HILOGE("The json is not data:%{public}s", key.c_str());
75         return nullptr;
76     }
77     return cJSON_GetObjectItem(json, key.c_str());
78 }
79 
GetObjectItem(const std::string & key) const80 cJSON *JsonParser::GetObjectItem(const std::string &key) const
81 {
82     return GetObjectItem(cJson_, key);
83 }
84 
ParseJsonArray(cJSON * json,const std::string & key,std::vector<std::string> & vals) const85 int32_t JsonParser::ParseJsonArray(cJSON *json, const std::string &key,
86     std::vector<std::string> &vals) const
87 {
88     cJSON *jsonArray = GetObjectItem(json, key);
89     if (!cJSON_IsArray(jsonArray)) {
90         MISC_HILOGE("The value of %{public}s is not array", key.c_str());
91         return ERROR;
92     }
93     int32_t size = cJSON_GetArraySize(jsonArray);
94     for (int32_t i = 0; i < size; ++i) {
95         cJSON *val = cJSON_GetArrayItem(jsonArray, i);
96         if ((!cJSON_IsString(val)) || (val->valuestring == nullptr)) {
97             MISC_HILOGE("The value of index %{public}d is not string", i);
98             return ERROR;
99         }
100         vals.push_back(val->valuestring);
101     }
102     return SUCCESS;
103 }
104 
ParseJsonArray(const std::string & key,std::vector<std::string> & vals) const105 int32_t JsonParser::ParseJsonArray(const std::string &key, std::vector<std::string> &vals) const
106 {
107     return ParseJsonArray(cJson_, key, vals);
108 }
109 
IsArray(cJSON * json) const110 bool JsonParser::IsArray(cJSON *json) const
111 {
112     return cJSON_IsArray(json);
113 }
114 
GetArraySize(cJSON * json) const115 int32_t JsonParser::GetArraySize(cJSON *json) const
116 {
117     return cJSON_GetArraySize(json);
118 }
119 
GetArrayItem(cJSON * json,int32_t index) const120 cJSON *JsonParser::GetArrayItem(cJSON *json, int32_t index) const
121 {
122     return cJSON_GetArrayItem(json, index);
123 }
124 
GetIntValue(cJSON * json) const125 int32_t JsonParser::GetIntValue(cJSON *json) const
126 {
127     if (!cJSON_IsNumber(json)) {
128         return static_cast<int32_t>(NAN);
129     }
130     return json->valueint;
131 }
132 
GetDoubleValue(cJSON * json) const133 double JsonParser::GetDoubleValue(cJSON *json) const
134 {
135     if (!cJSON_IsNumber(json)) {
136         return static_cast<double>(NAN);
137     }
138     return json->valuedouble;
139 }
140 
GetStringValue(cJSON * json) const141 std::string JsonParser::GetStringValue(cJSON *json) const
142 {
143     if (!cJSON_IsString(json)) {
144         return NULL;
145     }
146     return json->valuestring;
147 }
148 }  // namespace Sensors
149 }  // namespace OHOS