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 
16 #include "json_node.h"
17 
18 #include "log/log.h"
19 #include "utils.h"
20 
21 namespace Updater {
22 namespace {
GetJsonTypeMap()23 inline const std::unordered_map<uint16_t, NodeType> &GetJsonTypeMap()
24 {
25     static const std::unordered_map<uint16_t, NodeType> jsonTypeMap {
26         { cJSON_Object, NodeType::OBJECT }, { cJSON_Array, NodeType::ARRAY }, { cJSON_Number, NodeType::INT },
27         { cJSON_String, NodeType::STRING }, { cJSON_False, NodeType::BOOL },  { cJSON_True, NodeType::BOOL },
28         { cJSON_NULL, NodeType::NUL },
29     };
30     return jsonTypeMap;
31 }
32 }  // namespace
33 
JsonNode()34 JsonNode::JsonNode() : type_ {NodeType::UNKNOWN}
35 {
36 }
37 
JsonNode(const std::string & str,bool needDelete)38 JsonNode::JsonNode(const std::string &str, bool needDelete) : JsonNode(cJSON_Parse(str.c_str()), needDelete)
39 {
40 }
41 
JsonNode(const Fs::path & path)42 JsonNode::JsonNode(const Fs::path &path)
43 {
44     std::string realPath {};
45     if (!Utils::PathToRealPath(path, realPath)) {
46         return;
47     }
48 
49     std::ifstream ifs(realPath);
50     if (!ifs.is_open()) {
51         LOG(ERROR) << realPath << " not exist";
52         return;
53     }
54 
55     // get root node
56     std::string content {std::istreambuf_iterator<char> {ifs}, {}};
57     cJSON *root = cJSON_Parse(content.c_str());
58     if (root == nullptr || cJSON_IsInvalid(root)) {
59         cJSON_Delete(root);
60         LOG(ERROR) << path << " contained json invalid";
61         return;
62     }
63 
64     Init(root, true);
65 }
66 
JsonNode(const cJSON * root,bool needDelete)67 JsonNode::JsonNode(const cJSON *root, bool needDelete)
68 {
69     if (root != nullptr && !cJSON_IsInvalid(root)) {
70         Init(root, needDelete);
71         return;
72     }
73     LOG(ERROR) << "root is not valid";
74     type_ = NodeType::UNKNOWN;
75 }
76 
77 
Init(const cJSON * root,bool needDelete)78 void JsonNode::Init(const cJSON *root, bool needDelete)
79 {
80     if (auto it = GetJsonTypeMap().find(root->type); it != GetJsonTypeMap().end()) {
81         type_ = it->second;
82     }
83     Parse(root);
84     if (needDelete) {
85         cJSON_Delete(const_cast<cJSON *>(root));
86     }
87 }
88 
Parse(const cJSON * root)89 void JsonNode::Parse(const cJSON *root)
90 {
91     cJSON *element {};
92     size_ = 1;
93     switch (type_) {
94         case NodeType::OBJECT: {
95             innerObj_ = std::make_optional<NodeMap>();
96             auto &optNodeMap = std::get<std::optional<NodeMap>>(innerObj_);
97             cJSON_ArrayForEach(element, root)
98             {
99                 std::unique_ptr<JsonNode> uPtr = std::make_unique<JsonNode>(element, false);
100                 innerNodesList_.push_back(*uPtr);
101                 (*optNodeMap).emplace(element->string, std::move(uPtr));
102             }
103             size_ = static_cast<int>(innerNodesList_.size());
104             break;
105         }
106         case NodeType::ARRAY: {
107             innerObj_ = std::make_optional<NodeVec>();
108             auto &optNodeVec = std::get<std::optional<NodeVec>>(innerObj_);
109             cJSON_ArrayForEach(element, root)
110             {
111                 std::unique_ptr<JsonNode> uPtr = std::make_unique<JsonNode>(element, false);
112                 innerNodesList_.push_back(*uPtr);
113                 (*optNodeVec).push_back(std::move(uPtr));
114             }
115             size_ = static_cast<int>(innerNodesList_.size());
116             break;
117         }
118         case NodeType::INT:
119             innerObj_ = std::make_optional<int>(root->valueint);
120             innerNodesList_.push_back(*this);
121             break;
122         case NodeType::STRING:
123             innerObj_ = std::make_optional<std::string>(root->valuestring);
124             innerNodesList_.push_back(*this);
125             break;
126         case NodeType::BOOL:
127             innerObj_ = std::make_optional<bool>(root->type == cJSON_True);
128             innerNodesList_.push_back(*this);
129             break;
130         case NodeType::NUL:
131             break;
132         default:
133             LOG(ERROR) << "unknown node type";
134             break;
135     }
136     if (root->string) {
137         key_ = root->string;
138     }
139 }
140 
141 JsonNode::~JsonNode() = default;
142 
operator [](int idx)143 JsonNode &JsonNode::operator[](int idx)
144 {
145     return GetNodeByIdx(innerObj_, size_, idx);
146 }
147 
operator [](const std::string & key)148 JsonNode &JsonNode::operator[](const std::string &key)
149 {
150     return GetNodeByKey(innerObj_, key);
151 }
152 
153 const JsonNode &JsonNode::operator[](int idx) const
__anon053b7bda0202(int idx) 154 {
155     return GetNodeByIdx(innerObj_, size_, idx);
156 }
157 
158 const JsonNode &JsonNode::operator[](const std::string &key) const
__anon053b7bda0302(const std::string &key) 159 {
160     return GetNodeByKey(innerObj_, key);
161 }
162 
163 std::list<std::reference_wrapper<JsonNode>>::const_iterator JsonNode::begin() const
__anon053b7bda0402(const std::string &key) 164 {
165     return innerNodesList_.cbegin();
166 }
167 
168 std::list<std::reference_wrapper<JsonNode>>::const_iterator JsonNode::end() const
__anon053b7bda0502(const std::string &key) 169 {
170     return innerNodesList_.cend();
171 }
172 }  // namespace Updater