123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "base/json/node_object.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "base/utils/utils.h" 1923b3eb3cSopenharmony_ci 2023b3eb3cSopenharmony_cinamespace OHOS::Ace { 2123b3eb3cSopenharmony_cinamespace { 2223b3eb3cSopenharmony_cistd::shared_ptr<UObject> FromJsonObject(const std::unique_ptr<JsonValue>& json) 2323b3eb3cSopenharmony_ci{ 2423b3eb3cSopenharmony_ci if (!json->IsObject()) { 2523b3eb3cSopenharmony_ci return nullptr; 2623b3eb3cSopenharmony_ci } 2723b3eb3cSopenharmony_ci 2823b3eb3cSopenharmony_ci auto object = std::make_shared<UObject>(); 2923b3eb3cSopenharmony_ci auto jsonSize = json->GetArraySize(); 3023b3eb3cSopenharmony_ci 3123b3eb3cSopenharmony_ci for (auto i = 0; i < jsonSize; ++i) { 3223b3eb3cSopenharmony_ci auto item = json->GetArrayItem(i); 3323b3eb3cSopenharmony_ci if (item->IsString()) { 3423b3eb3cSopenharmony_ci object->AddItemToObject(item->GetKey(), item->GetString()); 3523b3eb3cSopenharmony_ci } else if (item->IsBool()) { 3623b3eb3cSopenharmony_ci object->AddItemToObject(item->GetKey(), item->GetBool()); 3723b3eb3cSopenharmony_ci } else if (item->IsNumber()) { 3823b3eb3cSopenharmony_ci object->AddItemToObject(item->GetKey(), item->GetDouble()); 3923b3eb3cSopenharmony_ci } else if (item->IsObject()) { 4023b3eb3cSopenharmony_ci object->AddItemToObject(item->GetKey(), FromJsonObject(item)); 4123b3eb3cSopenharmony_ci } else { 4223b3eb3cSopenharmony_ci LOGE("UITree |ERROR| not match key=%{public}s", item->GetKey().c_str()); 4323b3eb3cSopenharmony_ci } 4423b3eb3cSopenharmony_ci } 4523b3eb3cSopenharmony_ci 4623b3eb3cSopenharmony_ci return object; 4723b3eb3cSopenharmony_ci} 4823b3eb3cSopenharmony_ci} // namespace 4923b3eb3cSopenharmony_ci 5023b3eb3cSopenharmony_ciNodeObject::NodeObject() : uobject_(std::make_shared<UObject>()) {} 5123b3eb3cSopenharmony_ci 5223b3eb3cSopenharmony_cibool NodeObject::Contains(const std::string& key) const 5323b3eb3cSopenharmony_ci{ 5423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 5523b3eb3cSopenharmony_ci return uobject_->Contains(key); 5623b3eb3cSopenharmony_ci} 5723b3eb3cSopenharmony_ci 5823b3eb3cSopenharmony_cibool NodeObject::GetBool(const std::string& key, bool defaultValue) const 5923b3eb3cSopenharmony_ci{ 6023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 6123b3eb3cSopenharmony_ci if (Contains(key)) { 6223b3eb3cSopenharmony_ci return uobject_->GetBool(key); 6323b3eb3cSopenharmony_ci } 6423b3eb3cSopenharmony_ci return defaultValue; 6523b3eb3cSopenharmony_ci} 6623b3eb3cSopenharmony_ci 6723b3eb3cSopenharmony_ciint32_t NodeObject::GetInt(const std::string& key, int32_t defaultVal) const 6823b3eb3cSopenharmony_ci{ 6923b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 7023b3eb3cSopenharmony_ci if (Contains(key)) { 7123b3eb3cSopenharmony_ci return uobject_->GetInt32(key); 7223b3eb3cSopenharmony_ci } 7323b3eb3cSopenharmony_ci return defaultVal; 7423b3eb3cSopenharmony_ci} 7523b3eb3cSopenharmony_ci 7623b3eb3cSopenharmony_ciuint32_t NodeObject::GetUInt(const std::string& key, uint32_t defaultVal) const 7723b3eb3cSopenharmony_ci{ 7823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 7923b3eb3cSopenharmony_ci if (Contains(key)) { 8023b3eb3cSopenharmony_ci return uobject_->GetInt32(key); 8123b3eb3cSopenharmony_ci } 8223b3eb3cSopenharmony_ci return defaultVal; 8323b3eb3cSopenharmony_ci} 8423b3eb3cSopenharmony_ci 8523b3eb3cSopenharmony_ciint64_t NodeObject::GetInt64(const std::string& key, int64_t defaultVal) const 8623b3eb3cSopenharmony_ci{ 8723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 8823b3eb3cSopenharmony_ci if (Contains(key)) { 8923b3eb3cSopenharmony_ci return uobject_->GetInt64(key); 9023b3eb3cSopenharmony_ci } 9123b3eb3cSopenharmony_ci return defaultVal; 9223b3eb3cSopenharmony_ci} 9323b3eb3cSopenharmony_ci 9423b3eb3cSopenharmony_cidouble NodeObject::GetDouble(const std::string& key, double defaultVal) const 9523b3eb3cSopenharmony_ci{ 9623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 9723b3eb3cSopenharmony_ci if (Contains(key)) { 9823b3eb3cSopenharmony_ci return uobject_->GetDouble(key); 9923b3eb3cSopenharmony_ci } 10023b3eb3cSopenharmony_ci return defaultVal; 10123b3eb3cSopenharmony_ci} 10223b3eb3cSopenharmony_ci 10323b3eb3cSopenharmony_cistd::string NodeObject::GetString(const std::string& key, const std::string& defaultVal) const 10423b3eb3cSopenharmony_ci{ 10523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, ""); 10623b3eb3cSopenharmony_ci if (Contains(key)) { 10723b3eb3cSopenharmony_ci return uobject_->GetString(key); 10823b3eb3cSopenharmony_ci } 10923b3eb3cSopenharmony_ci return defaultVal; 11023b3eb3cSopenharmony_ci} 11123b3eb3cSopenharmony_ci 11223b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> NodeObject::GetValue(const std::string& key) const 11323b3eb3cSopenharmony_ci{ 11423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, std::make_unique<NodeObject>()); 11523b3eb3cSopenharmony_ci if (Contains(key)) { 11623b3eb3cSopenharmony_ci auto object = std::make_unique<NodeObject>(); 11723b3eb3cSopenharmony_ci object->uobject_ = uobject_->GetObject(key); 11823b3eb3cSopenharmony_ci return object; 11923b3eb3cSopenharmony_ci } 12023b3eb3cSopenharmony_ci return std::make_unique<NodeObject>(); 12123b3eb3cSopenharmony_ci} 12223b3eb3cSopenharmony_ci 12323b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> NodeObject::GetObject(const std::string& key) const 12423b3eb3cSopenharmony_ci{ 12523b3eb3cSopenharmony_ci return GetValue(key); 12623b3eb3cSopenharmony_ci} 12723b3eb3cSopenharmony_ci 12823b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, const char* value) 12923b3eb3cSopenharmony_ci{ 13023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 13123b3eb3cSopenharmony_ci if (!value || !key) { 13223b3eb3cSopenharmony_ci return false; 13323b3eb3cSopenharmony_ci } 13423b3eb3cSopenharmony_ci 13523b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), std::string(value)); 13623b3eb3cSopenharmony_ci return true; 13723b3eb3cSopenharmony_ci} 13823b3eb3cSopenharmony_ci 13923b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, size_t value) 14023b3eb3cSopenharmony_ci{ 14123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 14223b3eb3cSopenharmony_ci if (!key) { 14323b3eb3cSopenharmony_ci return false; 14423b3eb3cSopenharmony_ci } 14523b3eb3cSopenharmony_ci 14623b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value); 14723b3eb3cSopenharmony_ci return true; 14823b3eb3cSopenharmony_ci} 14923b3eb3cSopenharmony_ci 15023b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, int32_t value) 15123b3eb3cSopenharmony_ci{ 15223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 15323b3eb3cSopenharmony_ci if (!key) { 15423b3eb3cSopenharmony_ci return false; 15523b3eb3cSopenharmony_ci } 15623b3eb3cSopenharmony_ci 15723b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value); 15823b3eb3cSopenharmony_ci return true; 15923b3eb3cSopenharmony_ci} 16023b3eb3cSopenharmony_ci 16123b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, int64_t value) 16223b3eb3cSopenharmony_ci{ 16323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 16423b3eb3cSopenharmony_ci if (!key) { 16523b3eb3cSopenharmony_ci return false; 16623b3eb3cSopenharmony_ci } 16723b3eb3cSopenharmony_ci 16823b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value); 16923b3eb3cSopenharmony_ci return true; 17023b3eb3cSopenharmony_ci} 17123b3eb3cSopenharmony_ci 17223b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, double value) 17323b3eb3cSopenharmony_ci{ 17423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 17523b3eb3cSopenharmony_ci if (!key) { 17623b3eb3cSopenharmony_ci return false; 17723b3eb3cSopenharmony_ci } 17823b3eb3cSopenharmony_ci 17923b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value); 18023b3eb3cSopenharmony_ci return true; 18123b3eb3cSopenharmony_ci} 18223b3eb3cSopenharmony_ci 18323b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, bool value) 18423b3eb3cSopenharmony_ci{ 18523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 18623b3eb3cSopenharmony_ci if (!key) { 18723b3eb3cSopenharmony_ci return false; 18823b3eb3cSopenharmony_ci } 18923b3eb3cSopenharmony_ci 19023b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value); 19123b3eb3cSopenharmony_ci return true; 19223b3eb3cSopenharmony_ci} 19323b3eb3cSopenharmony_ci 19423b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, const std::unique_ptr<JsonValue>& value) 19523b3eb3cSopenharmony_ci{ 19623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 19723b3eb3cSopenharmony_ci if (!value || !key) { 19823b3eb3cSopenharmony_ci return false; 19923b3eb3cSopenharmony_ci } 20023b3eb3cSopenharmony_ci 20123b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), FromJsonObject(value)); 20223b3eb3cSopenharmony_ci return true; 20323b3eb3cSopenharmony_ci} 20423b3eb3cSopenharmony_ci 20523b3eb3cSopenharmony_cibool NodeObject::Put(const char* key, const std::unique_ptr<NodeObject>& value) 20623b3eb3cSopenharmony_ci{ 20723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, false); 20823b3eb3cSopenharmony_ci if (!value || !key) { 20923b3eb3cSopenharmony_ci return false; 21023b3eb3cSopenharmony_ci } 21123b3eb3cSopenharmony_ci 21223b3eb3cSopenharmony_ci uobject_->AddItemToObject(std::string(key), value->uobject_); 21323b3eb3cSopenharmony_ci return true; 21423b3eb3cSopenharmony_ci} 21523b3eb3cSopenharmony_ci 21623b3eb3cSopenharmony_cistd::string NodeObject::ToString() 21723b3eb3cSopenharmony_ci{ 21823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, ""); 21923b3eb3cSopenharmony_ci int32_t objectSize = uobject_->EstimateBufferSize(); 22023b3eb3cSopenharmony_ci std::string buffer("", objectSize); 22123b3eb3cSopenharmony_ci uobject_->Serialize(buffer.data(), objectSize); 22223b3eb3cSopenharmony_ci return buffer; 22323b3eb3cSopenharmony_ci} 22423b3eb3cSopenharmony_ci 22523b3eb3cSopenharmony_civoid NodeObject::FromString(const std::string& buffer) 22623b3eb3cSopenharmony_ci{ 22723b3eb3cSopenharmony_ci CHECK_NULL_VOID(uobject_); 22823b3eb3cSopenharmony_ci uobject_->Deserialize(buffer.data(), buffer.size()); 22923b3eb3cSopenharmony_ci} 23023b3eb3cSopenharmony_ci 23123b3eb3cSopenharmony_cisize_t NodeObject::Hash() 23223b3eb3cSopenharmony_ci{ 23323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 23423b3eb3cSopenharmony_ci return uobject_->Hash(); 23523b3eb3cSopenharmony_ci} 23623b3eb3cSopenharmony_ci 23723b3eb3cSopenharmony_ciint32_t NodeObject::EstimateBufferSize() 23823b3eb3cSopenharmony_ci{ 23923b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uobject_, 0); 24023b3eb3cSopenharmony_ci return uobject_->EstimateBufferSize(); 24123b3eb3cSopenharmony_ci} 24223b3eb3cSopenharmony_ci 24323b3eb3cSopenharmony_cistd::unique_ptr<NodeObject> NodeObject::Create() 24423b3eb3cSopenharmony_ci{ 24523b3eb3cSopenharmony_ci return std::make_unique<NodeObject>(); 24623b3eb3cSopenharmony_ci} 24723b3eb3cSopenharmony_ci 24823b3eb3cSopenharmony_ciextern "C" ACE_FORCE_EXPORT void* OHOS_ACE_CreateNodeObject() 24923b3eb3cSopenharmony_ci{ 25023b3eb3cSopenharmony_ci return new NodeObject(); 25123b3eb3cSopenharmony_ci} 25223b3eb3cSopenharmony_ci} // namespace OHOS::Ace 253