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 LOG(ERROR) << path << " contained json invalid";
60 return;
61 }
62
63 Init(root, true);
64 }
65
JsonNode(const cJSON * root,bool needDelete)66 JsonNode::JsonNode(const cJSON *root, bool needDelete)
67 {
68 if (root != nullptr && !cJSON_IsInvalid(root)) {
69 Init(root, needDelete);
70 return;
71 }
72 LOG(ERROR) << "root is not valid";
73 type_ = NodeType::UNKNOWN;
74 }
75
76
Init(const cJSON * root,bool needDelete)77 void JsonNode::Init(const cJSON *root, bool needDelete)
78 {
79 if (auto it = GetJsonTypeMap().find(root->type); it != GetJsonTypeMap().end()) {
80 type_ = it->second;
81 }
82 Parse(root);
83 if (needDelete) {
84 cJSON_Delete(const_cast<cJSON *>(root));
85 }
86 }
87
Parse(const cJSON * root)88 void JsonNode::Parse(const cJSON *root)
89 {
90 cJSON *element {};
91 size_ = 1;
92 switch (type_) {
93 case NodeType::OBJECT: {
94 innerObj_ = std::make_optional<NodeMap>();
95 auto &optNodeMap = std::get<std::optional<NodeMap>>(innerObj_);
96 cJSON_ArrayForEach(element, root)
97 {
98 std::unique_ptr<JsonNode> uPtr = std::make_unique<JsonNode>(element, false);
99 innerNodesList_.push_back(*uPtr);
100 (*optNodeMap).emplace(element->string, std::move(uPtr));
101 }
102 size_ = static_cast<int>(innerNodesList_.size());
103 break;
104 }
105 case NodeType::ARRAY: {
106 innerObj_ = std::make_optional<NodeVec>();
107 auto &optNodeVec = std::get<std::optional<NodeVec>>(innerObj_);
108 cJSON_ArrayForEach(element, root)
109 {
110 std::unique_ptr<JsonNode> uPtr = std::make_unique<JsonNode>(element, false);
111 innerNodesList_.push_back(*uPtr);
112 (*optNodeVec).push_back(std::move(uPtr));
113 }
114 size_ = static_cast<int>(innerNodesList_.size());
115 break;
116 }
117 case NodeType::INT:
118 innerObj_ = std::make_optional<int>(root->valueint);
119 innerNodesList_.push_back(*this);
120 break;
121 case NodeType::STRING:
122 innerObj_ = std::make_optional<std::string>(root->valuestring);
123 innerNodesList_.push_back(*this);
124 break;
125 case NodeType::BOOL:
126 innerObj_ = std::make_optional<bool>(root->type == cJSON_True);
127 innerNodesList_.push_back(*this);
128 break;
129 case NodeType::NUL:
130 break;
131 default:
132 LOG(ERROR) << "unknown node type";
133 break;
134 }
135 if (root->string) {
136 key_ = root->string;
137 }
138 }
139
140 JsonNode::~JsonNode() = default;
141
operator [](int idx) const142 const JsonNode &JsonNode::operator[](int idx) const
143 {
144 auto optVec = std::get_if<std::optional<NodeVec>>(&innerObj_);
145 if (optVec == nullptr || *optVec == std::nullopt) {
146 return GetInvalidNode(); // type not matched
147 }
148 const NodeVec &nodeVec = **optVec;
149 if (idx < 0 || idx >= size_) {
150 return GetInvalidNode();
151 }
152 return *nodeVec[idx];
153 }
154
operator [](const std::string & key) const155 const JsonNode &JsonNode::operator[](const std::string &key) const
156 {
157 auto optMap = std::get_if<std::optional<NodeMap>>(&innerObj_);
158 if (optMap == nullptr || *optMap == std::nullopt) {
159 return GetInvalidNode(); // type not matched
160 }
161 const NodeMap &nodeMap = **optMap;
162 if (auto it = nodeMap.find(key); it != nodeMap.end()) {
163 return *(it->second);
164 }
165 return GetInvalidNode();
166 }
167
begin() const168 std::list<std::reference_wrapper<JsonNode>>::const_iterator JsonNode::begin() const
169 {
170 return innerNodesList_.cbegin();
171 }
172
end() const173 std::list<std::reference_wrapper<JsonNode>>::const_iterator JsonNode::end() const
174 {
175 return innerNodesList_.cend();
176 }
177 } // namespace Updater