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