1 /*
2 * Copyright (c) 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
16 #include "base/json/node_object.h"
17
18 #include <cstring>
19
20 #include "securec.h"
21
22 #include "base/log/log_wrapper.h"
23 #include "base/utils/utils.h"
24
25 namespace OHOS::Ace {
26 namespace {
FromJsonObject(const std::unique_ptr<JsonValue> & json)27 std::shared_ptr<UObject> FromJsonObject(const std::unique_ptr<JsonValue>& json)
28 {
29 if (!json->IsObject()) {
30 return nullptr;
31 }
32
33 auto object = std::make_shared<UObject>();
34 auto jsonSize = json->GetArraySize();
35
36 for (auto i = 0; i < jsonSize; ++i) {
37 auto item = json->GetArrayItem(i);
38 if (item->IsString()) {
39 object->AddItemToObject(item->GetKey(), item->GetString());
40 } else if (item->IsBool()) {
41 object->AddItemToObject(item->GetKey(), item->GetBool());
42 } else if (item->IsNumber()) {
43 object->AddItemToObject(item->GetKey(), item->GetDouble());
44 } else if (item->IsObject()) {
45 object->AddItemToObject(item->GetKey(), FromJsonObject(item));
46 } else {
47 LOGE("UITree |ERROR| not match key=%{public}s", item->GetKey().c_str());
48 }
49 }
50
51 return object;
52 }
53 } // namespace
54
NodeObject()55 NodeObject::NodeObject() : uobject_(std::make_shared<UObject>()) {}
56
Contains(const std::string & key) const57 bool NodeObject::Contains(const std::string& key) const
58 {
59 CHECK_NULL_RETURN_NOLOG(uobject_, false);
60 return uobject_->Contains(key);
61 }
62
GetBool(const std::string & key,bool defaultValue) const63 bool NodeObject::GetBool(const std::string& key, bool defaultValue) const
64 {
65 CHECK_NULL_RETURN_NOLOG(uobject_, false);
66 if (Contains(key)) {
67 return uobject_->GetBool(key);
68 }
69 return defaultValue;
70 }
71
GetInt(const std::string & key,int32_t defaultVal) const72 int32_t NodeObject::GetInt(const std::string& key, int32_t defaultVal) const
73 {
74 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
75 if (Contains(key)) {
76 return uobject_->GetInt32(key);
77 }
78 return defaultVal;
79 }
80
GetUInt(const std::string & key,uint32_t defaultVal) const81 uint32_t NodeObject::GetUInt(const std::string& key, uint32_t defaultVal) const
82 {
83 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
84 if (Contains(key)) {
85 return uobject_->GetInt32(key);
86 }
87 return defaultVal;
88 }
89
GetInt64(const std::string & key,int64_t defaultVal) const90 int64_t NodeObject::GetInt64(const std::string& key, int64_t defaultVal) const
91 {
92 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
93 if (Contains(key)) {
94 return uobject_->GetInt64(key);
95 }
96 return defaultVal;
97 }
98
GetDouble(const std::string & key,double defaultVal) const99 double NodeObject::GetDouble(const std::string& key, double defaultVal) const
100 {
101 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
102 if (Contains(key)) {
103 return uobject_->GetDouble(key);
104 }
105 return defaultVal;
106 }
107
GetString(const std::string & key,const std::string & defaultVal) const108 std::string NodeObject::GetString(const std::string& key, const std::string& defaultVal) const
109 {
110 CHECK_NULL_RETURN_NOLOG(uobject_, "");
111 if (Contains(key)) {
112 return uobject_->GetString(key);
113 }
114 return defaultVal;
115 }
116
GetValue(const std::string & key) const117 std::unique_ptr<JsonValue> NodeObject::GetValue(const std::string& key) const
118 {
119 CHECK_NULL_RETURN_NOLOG(uobject_, std::make_unique<NodeObject>());
120 if (Contains(key)) {
121 auto object = std::make_unique<NodeObject>();
122 object->uobject_ = uobject_->GetObject(key);
123 return object;
124 }
125 return std::make_unique<NodeObject>();
126 }
127
GetObject(const std::string & key) const128 std::unique_ptr<JsonValue> NodeObject::GetObject(const std::string& key) const
129 {
130 return GetValue(key);
131 }
132
Put(const char * key,const char * value)133 bool NodeObject::Put(const char* key, const char* value)
134 {
135 CHECK_NULL_RETURN_NOLOG(uobject_, false);
136 if (!value || !key) {
137 return false;
138 }
139
140 uobject_->AddItemToObject(std::string(key), std::string(value));
141 return true;
142 }
143
Put(const char * key,size_t value)144 bool NodeObject::Put(const char* key, size_t value)
145 {
146 CHECK_NULL_RETURN_NOLOG(uobject_, false);
147 if (!key) {
148 return false;
149 }
150
151 uobject_->AddItemToObject(std::string(key), value);
152 return true;
153 }
154
Put(const char * key,int32_t value)155 bool NodeObject::Put(const char* key, int32_t value)
156 {
157 CHECK_NULL_RETURN_NOLOG(uobject_, false);
158 if (!key) {
159 return false;
160 }
161
162 uobject_->AddItemToObject(std::string(key), value);
163 return true;
164 }
165
Put(const char * key,int64_t value)166 bool NodeObject::Put(const char* key, int64_t value)
167 {
168 CHECK_NULL_RETURN_NOLOG(uobject_, false);
169 if (!key) {
170 return false;
171 }
172
173 uobject_->AddItemToObject(std::string(key), value);
174 return true;
175 }
176
Put(const char * key,double value)177 bool NodeObject::Put(const char* key, double value)
178 {
179 CHECK_NULL_RETURN_NOLOG(uobject_, false);
180 if (!key) {
181 return false;
182 }
183
184 uobject_->AddItemToObject(std::string(key), value);
185 return true;
186 }
187
Put(const char * key,bool value)188 bool NodeObject::Put(const char* key, bool value)
189 {
190 CHECK_NULL_RETURN_NOLOG(uobject_, false);
191 if (!key) {
192 return false;
193 }
194
195 uobject_->AddItemToObject(std::string(key), value);
196 return true;
197 }
198
Put(const char * key,const std::unique_ptr<JsonValue> & value)199 bool NodeObject::Put(const char* key, const std::unique_ptr<JsonValue>& value)
200 {
201 CHECK_NULL_RETURN_NOLOG(uobject_, false);
202 if (!value || !key) {
203 return false;
204 }
205
206 uobject_->AddItemToObject(std::string(key), FromJsonObject(value));
207 return true;
208 }
209
Put(const char * key,const std::unique_ptr<NodeObject> & value)210 bool NodeObject::Put(const char* key, const std::unique_ptr<NodeObject>& value)
211 {
212 CHECK_NULL_RETURN_NOLOG(uobject_, false);
213 if (!value || !key) {
214 return false;
215 }
216
217 uobject_->AddItemToObject(std::string(key), value->uobject_);
218 return true;
219 }
220
ToString()221 std::string NodeObject::ToString()
222 {
223 CHECK_NULL_RETURN_NOLOG(uobject_, "");
224 int32_t objectSize = uobject_->EstimateBufferSize();
225 std::string buffer("", objectSize);
226 uobject_->Serialize(buffer.data(), objectSize);
227 return buffer;
228 }
229
FromString(const std::string & buffer)230 void NodeObject::FromString(const std::string& buffer)
231 {
232 CHECK_NULL_VOID_NOLOG(uobject_);
233 uobject_->Deserialize(buffer.data(), buffer.size());
234 }
235
Hash()236 size_t NodeObject::Hash()
237 {
238 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
239 return uobject_->Hash();
240 }
241
EstimateBufferSize()242 int32_t NodeObject::EstimateBufferSize()
243 {
244 CHECK_NULL_RETURN_NOLOG(uobject_, 0);
245 return uobject_->EstimateBufferSize();
246 }
247
Create()248 std::unique_ptr<NodeObject> NodeObject::Create()
249 {
250 return std::make_unique<NodeObject>();
251 }
252
OHOS_ACE_CreateNodeObject()253 extern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateNodeObject()
254 {
255 return new NodeObject();
256 }
257 } // namespace OHOS::Ace
258