• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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()39 JsonParser::~JsonParser()
40 {
41     if (cJson_ != nullptr) {
42         cJSON_Delete(cJson_);
43     }
44 }
45 
GetObjectItem(cJSON * json,const std::string & key) const46 cJSON* JsonParser::GetObjectItem(cJSON *json, const std::string& key) const
47 {
48     if (!cJSON_IsObject(json)) {
49         MISC_HILOGE("The json is not object");
50         return nullptr;
51     }
52     if (!cJSON_HasObjectItem(json, key.c_str())) {
53         MISC_HILOGE("The json is not data:%{public}s", key.c_str());
54         return nullptr;
55     }
56     return cJSON_GetObjectItem(json, key.c_str());
57 }
58 
GetObjectItem(const std::string & key) const59 cJSON* JsonParser::GetObjectItem(const std::string& key) const
60 {
61     return GetObjectItem(cJson_, key);
62 }
63 
ParseJsonArray(cJSON * json,const std::string & key,std::vector<std::string> & vals) const64 int32_t JsonParser::ParseJsonArray(cJSON *json, const std::string& key,
65     std::vector<std::string>& vals) const
66 {
67     cJSON* jsonArray = GetObjectItem(json, key);
68     if (!cJSON_IsArray(jsonArray)) {
69         MISC_HILOGE("The value of %{public}s is not array", key.c_str());
70         return ERROR;
71     }
72     int32_t size = cJSON_GetArraySize(jsonArray);
73     for (int32_t i = 0; i < size; ++i) {
74         cJSON* val = cJSON_GetArrayItem(jsonArray, i);
75         if ((!cJSON_IsString(val)) || (val->valuestring == nullptr)) {
76             MISC_HILOGE("The value of index %{public}d is not string", i);
77             return ERROR;
78         }
79         vals.push_back(val->valuestring);
80     }
81     return SUCCESS;
82 }
83 
ParseJsonArray(const std::string & key,std::vector<std::string> & vals) const84 int32_t JsonParser::ParseJsonArray(const std::string& key, std::vector<std::string>& vals) const
85 {
86     return ParseJsonArray(cJson_, key, vals);
87 }
88 }  // namespace Sensors
89 }  // namespace OHOS