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
20namespace OHOS::Ace {
21namespace {
22std::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
50NodeObject::NodeObject() : uobject_(std::make_shared<UObject>()) {}
51
52bool NodeObject::Contains(const std::string& key) const
53{
54    CHECK_NULL_RETURN(uobject_, false);
55    return uobject_->Contains(key);
56}
57
58bool 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
67int32_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
76uint32_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
85int64_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
94double 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
103std::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
112std::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
123std::unique_ptr<JsonValue> NodeObject::GetObject(const std::string& key) const
124{
125    return GetValue(key);
126}
127
128bool 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
139bool 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
150bool 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
161bool 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
172bool 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
183bool 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
194bool 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
205bool 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
216std::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
225void NodeObject::FromString(const std::string& buffer)
226{
227    CHECK_NULL_VOID(uobject_);
228    uobject_->Deserialize(buffer.data(), buffer.size());
229}
230
231size_t NodeObject::Hash()
232{
233    CHECK_NULL_RETURN(uobject_, 0);
234    return uobject_->Hash();
235}
236
237int32_t NodeObject::EstimateBufferSize()
238{
239    CHECK_NULL_RETURN(uobject_, 0);
240    return uobject_->EstimateBufferSize();
241}
242
243std::unique_ptr<NodeObject> NodeObject::Create()
244{
245    return std::make_unique<NodeObject>();
246}
247
248extern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateNodeObject()
249{
250    return new NodeObject();
251}
252} // namespace OHOS::Ace
253