• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/stat.h>
18 #include <unistd.h>
19 
20 #include "file_utils.h"
21 #include "sensors_errors.h"
22 
23 namespace OHOS {
24 namespace Sensors {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "JsonParser" };
27 }  // namespace
28 
JsonParser(const std::string & filePath)29 JsonParser::JsonParser(const std::string &filePath)
30 {
31     std::string jsonStr = ReadJsonFile(filePath);
32     if (jsonStr.empty()) {
33         MISC_HILOGE("Read json file fail");
34         return;
35     }
36     cJson_ = cJSON_Parse(jsonStr.c_str());
37 }
38 
JsonParser(const RawFileDescriptor & rawFd)39 JsonParser::JsonParser(const RawFileDescriptor &rawFd)
40 {
41     std::string jsonStr = ReadFd(rawFd);
42     if (jsonStr.empty()) {
43         MISC_HILOGE("Read fd fail");
44         return;
45     }
46     cJson_ = cJSON_Parse(jsonStr.c_str());
47 }
48 
~JsonParser()49 JsonParser::~JsonParser()
50 {
51     if (cJson_ != nullptr) {
52         cJSON_Delete(cJson_);
53     }
54 }
55 
GetObjectItem(cJSON * json,const std::string & key) const56 cJSON* JsonParser::GetObjectItem(cJSON *json, const std::string& key) const
57 {
58     if (!cJSON_IsObject(json)) {
59         MISC_HILOGE("The json is not object");
60         return nullptr;
61     }
62     if (!cJSON_HasObjectItem(json, key.c_str())) {
63         MISC_HILOGE("The json is not data:%{public}s", key.c_str());
64         return nullptr;
65     }
66     return cJSON_GetObjectItem(json, key.c_str());
67 }
68 
GetObjectItem(const std::string & key) const69 cJSON* JsonParser::GetObjectItem(const std::string& key) const
70 {
71     return GetObjectItem(cJson_, key);
72 }
73 
ParseJsonArray(cJSON * json,const std::string & key,std::vector<std::string> & vals) const74 int32_t JsonParser::ParseJsonArray(cJSON *json, const std::string& key,
75     std::vector<std::string>& vals) const
76 {
77     cJSON* jsonArray = GetObjectItem(json, key);
78     if (!cJSON_IsArray(jsonArray)) {
79         MISC_HILOGE("The value of %{public}s is not array", key.c_str());
80         return ERROR;
81     }
82     int32_t size = cJSON_GetArraySize(jsonArray);
83     for (int32_t i = 0; i < size; ++i) {
84         cJSON* val = cJSON_GetArrayItem(jsonArray, i);
85         if ((!cJSON_IsString(val)) || (val->valuestring == nullptr)) {
86             MISC_HILOGE("The value of index %{public}d is not string", i);
87             return ERROR;
88         }
89         vals.push_back(val->valuestring);
90     }
91     return SUCCESS;
92 }
93 
ParseJsonArray(const std::string & key,std::vector<std::string> & vals) const94 int32_t JsonParser::ParseJsonArray(const std::string& key, std::vector<std::string>& vals) const
95 {
96     return ParseJsonArray(cJson_, key, vals);
97 }
98 
IsArray(cJSON * json) const99 bool JsonParser::IsArray(cJSON *json) const
100 {
101     return cJSON_IsArray(json);
102 }
103 
GetArraySize(cJSON * json) const104 int32_t JsonParser::GetArraySize(cJSON *json) const
105 {
106     return cJSON_GetArraySize(json);
107 }
108 
GetArrayItem(cJSON * json,int32_t index) const109 cJSON* JsonParser::GetArrayItem(cJSON *json, int32_t index) const
110 {
111     return cJSON_GetArrayItem(json, index);
112 }
113 }  // namespace Sensors
114 }  // namespace OHOS