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/uobject.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "securec.h" 1923b3eb3cSopenharmony_ci#include "base/log/log_wrapper.h" 2023b3eb3cSopenharmony_ci 2123b3eb3cSopenharmony_cinamespace OHOS { 2223b3eb3cSopenharmony_cinamespace { 2323b3eb3cSopenharmony_citemplate<typename T> 2423b3eb3cSopenharmony_cisize_t HashItem(const std::string& key, const T& value) 2523b3eb3cSopenharmony_ci{ 2623b3eb3cSopenharmony_ci return std::hash<std::string>()(key) + std::hash<T>()(value); 2723b3eb3cSopenharmony_ci} 2823b3eb3cSopenharmony_ci} // namespace 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, const char* value) 3123b3eb3cSopenharmony_ci{ 3223b3eb3cSopenharmony_ci if (value) { 3323b3eb3cSopenharmony_ci stringItems_[key] = value; 3423b3eb3cSopenharmony_ci } 3523b3eb3cSopenharmony_ci} 3623b3eb3cSopenharmony_ci 3723b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, const std::string& value) 3823b3eb3cSopenharmony_ci{ 3923b3eb3cSopenharmony_ci stringItems_[key] = value; 4023b3eb3cSopenharmony_ci} 4123b3eb3cSopenharmony_ci 4223b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, size_t value) 4323b3eb3cSopenharmony_ci{ 4423b3eb3cSopenharmony_ci sizetItems_[key] = value; 4523b3eb3cSopenharmony_ci} 4623b3eb3cSopenharmony_ci 4723b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, int32_t value) 4823b3eb3cSopenharmony_ci{ 4923b3eb3cSopenharmony_ci int32Items_[key] = value; 5023b3eb3cSopenharmony_ci} 5123b3eb3cSopenharmony_ci 5223b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, int64_t value) 5323b3eb3cSopenharmony_ci{ 5423b3eb3cSopenharmony_ci int64Items_[key] = value; 5523b3eb3cSopenharmony_ci} 5623b3eb3cSopenharmony_ci 5723b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, double value) 5823b3eb3cSopenharmony_ci{ 5923b3eb3cSopenharmony_ci doubleItems_[key] = value; 6023b3eb3cSopenharmony_ci} 6123b3eb3cSopenharmony_ci 6223b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, bool value) 6323b3eb3cSopenharmony_ci{ 6423b3eb3cSopenharmony_ci boolItems_[key] = value; 6523b3eb3cSopenharmony_ci} 6623b3eb3cSopenharmony_ci 6723b3eb3cSopenharmony_civoid UObject::AddItemToObject(const std::string& key, const std::shared_ptr<UObject>& value) 6823b3eb3cSopenharmony_ci{ 6923b3eb3cSopenharmony_ci if (value) { 7023b3eb3cSopenharmony_ci children_[key] = std::move(value); 7123b3eb3cSopenharmony_ci } 7223b3eb3cSopenharmony_ci} 7323b3eb3cSopenharmony_ci 7423b3eb3cSopenharmony_cistd::string UObject::GetString(const std::string& key) const 7523b3eb3cSopenharmony_ci{ 7623b3eb3cSopenharmony_ci auto iter = stringItems_.find(key); 7723b3eb3cSopenharmony_ci if (iter != stringItems_.end()) { 7823b3eb3cSopenharmony_ci return iter->second; 7923b3eb3cSopenharmony_ci } 8023b3eb3cSopenharmony_ci return ""; 8123b3eb3cSopenharmony_ci} 8223b3eb3cSopenharmony_ci 8323b3eb3cSopenharmony_cisize_t UObject::GetSizeT(const std::string& key) const 8423b3eb3cSopenharmony_ci{ 8523b3eb3cSopenharmony_ci auto sizetIter = sizetItems_.find(key); 8623b3eb3cSopenharmony_ci if (sizetIter != sizetItems_.end()) { 8723b3eb3cSopenharmony_ci return sizetIter->second; 8823b3eb3cSopenharmony_ci } 8923b3eb3cSopenharmony_ci auto doubleIter = doubleItems_.find(key); 9023b3eb3cSopenharmony_ci if (doubleIter != doubleItems_.end()) { 9123b3eb3cSopenharmony_ci return static_cast<size_t>(doubleIter->second); 9223b3eb3cSopenharmony_ci } 9323b3eb3cSopenharmony_ci return 0; 9423b3eb3cSopenharmony_ci} 9523b3eb3cSopenharmony_ci 9623b3eb3cSopenharmony_ciint32_t UObject::GetInt32(const std::string& key) const 9723b3eb3cSopenharmony_ci{ 9823b3eb3cSopenharmony_ci auto int32Iter = int32Items_.find(key); 9923b3eb3cSopenharmony_ci if (int32Iter != int32Items_.end()) { 10023b3eb3cSopenharmony_ci return int32Iter->second; 10123b3eb3cSopenharmony_ci } 10223b3eb3cSopenharmony_ci auto doubleIter = doubleItems_.find(key); 10323b3eb3cSopenharmony_ci if (doubleIter != doubleItems_.end()) { 10423b3eb3cSopenharmony_ci return static_cast<int32_t>(doubleIter->second); 10523b3eb3cSopenharmony_ci } 10623b3eb3cSopenharmony_ci return 0; 10723b3eb3cSopenharmony_ci} 10823b3eb3cSopenharmony_ci 10923b3eb3cSopenharmony_ciint64_t UObject::GetInt64(const std::string& key) const 11023b3eb3cSopenharmony_ci{ 11123b3eb3cSopenharmony_ci auto int64Iter = int64Items_.find(key); 11223b3eb3cSopenharmony_ci if (int64Iter != int64Items_.end()) { 11323b3eb3cSopenharmony_ci return int64Iter->second; 11423b3eb3cSopenharmony_ci } 11523b3eb3cSopenharmony_ci auto doubleIter = doubleItems_.find(key); 11623b3eb3cSopenharmony_ci if (doubleIter != doubleItems_.end()) { 11723b3eb3cSopenharmony_ci return static_cast<int64_t>(doubleIter->second); 11823b3eb3cSopenharmony_ci } 11923b3eb3cSopenharmony_ci return 0; 12023b3eb3cSopenharmony_ci} 12123b3eb3cSopenharmony_ci 12223b3eb3cSopenharmony_cidouble UObject::GetDouble(const std::string& key) const 12323b3eb3cSopenharmony_ci{ 12423b3eb3cSopenharmony_ci auto iter = doubleItems_.find(key); 12523b3eb3cSopenharmony_ci if (iter != doubleItems_.end()) { 12623b3eb3cSopenharmony_ci return iter->second; 12723b3eb3cSopenharmony_ci } 12823b3eb3cSopenharmony_ci return 0; 12923b3eb3cSopenharmony_ci} 13023b3eb3cSopenharmony_ci 13123b3eb3cSopenharmony_cibool UObject::GetBool(const std::string& key) const 13223b3eb3cSopenharmony_ci{ 13323b3eb3cSopenharmony_ci auto iter = boolItems_.find(key); 13423b3eb3cSopenharmony_ci if (iter != boolItems_.end()) { 13523b3eb3cSopenharmony_ci return iter->second; 13623b3eb3cSopenharmony_ci } 13723b3eb3cSopenharmony_ci return false; 13823b3eb3cSopenharmony_ci} 13923b3eb3cSopenharmony_ci 14023b3eb3cSopenharmony_cistd::shared_ptr<UObject> UObject::GetObject(const std::string& key) const 14123b3eb3cSopenharmony_ci{ 14223b3eb3cSopenharmony_ci auto iter = children_.find(key); 14323b3eb3cSopenharmony_ci if (iter != children_.end()) { 14423b3eb3cSopenharmony_ci return iter->second; 14523b3eb3cSopenharmony_ci } 14623b3eb3cSopenharmony_ci return std::make_shared<UObject>(); 14723b3eb3cSopenharmony_ci} 14823b3eb3cSopenharmony_ci 14923b3eb3cSopenharmony_cibool UObject::Contains(const std::string& key) const 15023b3eb3cSopenharmony_ci{ 15123b3eb3cSopenharmony_ci return stringItems_.count(key) || sizetItems_.count(key) || int32Items_.count(key) || int64Items_.count(key) || 15223b3eb3cSopenharmony_ci doubleItems_.count(key) || boolItems_.count(key) || children_.count(key); 15323b3eb3cSopenharmony_ci} 15423b3eb3cSopenharmony_ci 15523b3eb3cSopenharmony_civoid UObject::Serialize(char* buffer, int32_t bufferLen) 15623b3eb3cSopenharmony_ci{ 15723b3eb3cSopenharmony_ci if (!buffer) { 15823b3eb3cSopenharmony_ci LOGE("|ERROR| buffer is null"); 15923b3eb3cSopenharmony_ci return; 16023b3eb3cSopenharmony_ci } 16123b3eb3cSopenharmony_ci 16223b3eb3cSopenharmony_ci buffer_ = buffer; 16323b3eb3cSopenharmony_ci bufferLen_ = bufferLen; 16423b3eb3cSopenharmony_ci offset_ = 0; 16523b3eb3cSopenharmony_ci 16623b3eb3cSopenharmony_ci for (const auto& item : stringItems_) { 16723b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 16823b3eb3cSopenharmony_ci } 16923b3eb3cSopenharmony_ci for (const auto& item : sizetItems_) { 17023b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 17123b3eb3cSopenharmony_ci } 17223b3eb3cSopenharmony_ci for (const auto& item : int32Items_) { 17323b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 17423b3eb3cSopenharmony_ci } 17523b3eb3cSopenharmony_ci for (const auto& item : int64Items_) { 17623b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 17723b3eb3cSopenharmony_ci } 17823b3eb3cSopenharmony_ci for (const auto& item : doubleItems_) { 17923b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 18023b3eb3cSopenharmony_ci } 18123b3eb3cSopenharmony_ci for (const auto& item : boolItems_) { 18223b3eb3cSopenharmony_ci WriteKV(item.first, item.second); 18323b3eb3cSopenharmony_ci } 18423b3eb3cSopenharmony_ci for (const auto& item : children_) { 18523b3eb3cSopenharmony_ci WriteObj(item.first, item.second); 18623b3eb3cSopenharmony_ci } 18723b3eb3cSopenharmony_ci} 18823b3eb3cSopenharmony_ci 18923b3eb3cSopenharmony_civoid UObject::Deserialize(const char* buffer, int32_t bufferLen) 19023b3eb3cSopenharmony_ci{ 19123b3eb3cSopenharmony_ci if (!buffer) { 19223b3eb3cSopenharmony_ci LOGE("|ERROR| buffer is null"); 19323b3eb3cSopenharmony_ci return; 19423b3eb3cSopenharmony_ci } 19523b3eb3cSopenharmony_ci 19623b3eb3cSopenharmony_ci constBuffer_ = buffer; 19723b3eb3cSopenharmony_ci offset_ = 0; 19823b3eb3cSopenharmony_ci 19923b3eb3cSopenharmony_ci while (offset_ < bufferLen) { 20023b3eb3cSopenharmony_ci ReadKV(); 20123b3eb3cSopenharmony_ci } 20223b3eb3cSopenharmony_ci} 20323b3eb3cSopenharmony_ci 20423b3eb3cSopenharmony_cisize_t UObject::Hash() 20523b3eb3cSopenharmony_ci{ 20623b3eb3cSopenharmony_ci hashValue_ = 0; 20723b3eb3cSopenharmony_ci 20823b3eb3cSopenharmony_ci for (const auto& item : stringItems_) { 20923b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 21023b3eb3cSopenharmony_ci } 21123b3eb3cSopenharmony_ci for (const auto& item : sizetItems_) { 21223b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 21323b3eb3cSopenharmony_ci } 21423b3eb3cSopenharmony_ci for (const auto& item : int32Items_) { 21523b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 21623b3eb3cSopenharmony_ci } 21723b3eb3cSopenharmony_ci for (const auto& item : int64Items_) { 21823b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 21923b3eb3cSopenharmony_ci } 22023b3eb3cSopenharmony_ci for (const auto& item : doubleItems_) { 22123b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 22223b3eb3cSopenharmony_ci } 22323b3eb3cSopenharmony_ci for (const auto& item : boolItems_) { 22423b3eb3cSopenharmony_ci hashValue_ += HashItem(item.first, item.second); 22523b3eb3cSopenharmony_ci } 22623b3eb3cSopenharmony_ci for (const auto& item : children_) { 22723b3eb3cSopenharmony_ci hashValue_ += item.second->Hash(); 22823b3eb3cSopenharmony_ci } 22923b3eb3cSopenharmony_ci 23023b3eb3cSopenharmony_ci return hashValue_; 23123b3eb3cSopenharmony_ci} 23223b3eb3cSopenharmony_ci 23323b3eb3cSopenharmony_ciint32_t UObject::EstimateBufferSize() 23423b3eb3cSopenharmony_ci{ 23523b3eb3cSopenharmony_ci size_t buffsize = 0; 23623b3eb3cSopenharmony_ci 23723b3eb3cSopenharmony_ci for (auto& item : stringItems_) { 23823b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(int32_t) + item.second.length(); 23923b3eb3cSopenharmony_ci } 24023b3eb3cSopenharmony_ci for (auto& item : sizetItems_) { 24123b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(size_t); 24223b3eb3cSopenharmony_ci } 24323b3eb3cSopenharmony_ci for (auto& item : int32Items_) { 24423b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(int32_t); 24523b3eb3cSopenharmony_ci } 24623b3eb3cSopenharmony_ci for (auto& item : int64Items_) { 24723b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(int64_t); 24823b3eb3cSopenharmony_ci } 24923b3eb3cSopenharmony_ci for (auto& item : doubleItems_) { 25023b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(double); 25123b3eb3cSopenharmony_ci } 25223b3eb3cSopenharmony_ci for (auto& item : boolItems_) { 25323b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + item.first.length() + sizeof(bool); 25423b3eb3cSopenharmony_ci } 25523b3eb3cSopenharmony_ci for (auto& child : children_) { 25623b3eb3cSopenharmony_ci buffsize += sizeof(uint8_t) + sizeof(int32_t) + child.first.length() + sizeof(int32_t) + 25723b3eb3cSopenharmony_ci static_cast<size_t>(child.second->EstimateBufferSize()); 25823b3eb3cSopenharmony_ci } 25923b3eb3cSopenharmony_ci 26023b3eb3cSopenharmony_ci return static_cast<int32_t>(buffsize); 26123b3eb3cSopenharmony_ci} 26223b3eb3cSopenharmony_ci 26323b3eb3cSopenharmony_civoid UObject::WriteChar(char value) 26423b3eb3cSopenharmony_ci{ 26523b3eb3cSopenharmony_ci buffer_[0] = value; 26623b3eb3cSopenharmony_ci offset_++; 26723b3eb3cSopenharmony_ci buffer_++; 26823b3eb3cSopenharmony_ci} 26923b3eb3cSopenharmony_ci 27023b3eb3cSopenharmony_civoid UObject::WriteInt32(int32_t value) 27123b3eb3cSopenharmony_ci{ 27223b3eb3cSopenharmony_ci if (memcpy_s(buffer_, bufferLen_ - offset_, &value, sizeof(int32_t)) != 0) { 27323b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 27423b3eb3cSopenharmony_ci return; 27523b3eb3cSopenharmony_ci } 27623b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(int32_t)); 27723b3eb3cSopenharmony_ci buffer_ += sizeof(int32_t); 27823b3eb3cSopenharmony_ci} 27923b3eb3cSopenharmony_ci 28023b3eb3cSopenharmony_civoid UObject::WriteSizeT(size_t value) 28123b3eb3cSopenharmony_ci{ 28223b3eb3cSopenharmony_ci if (memcpy_s(buffer_, bufferLen_ - offset_, &value, sizeof(size_t)) != 0) { 28323b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 28423b3eb3cSopenharmony_ci return; 28523b3eb3cSopenharmony_ci } 28623b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(size_t)); 28723b3eb3cSopenharmony_ci buffer_ += sizeof(size_t); 28823b3eb3cSopenharmony_ci} 28923b3eb3cSopenharmony_ci 29023b3eb3cSopenharmony_civoid UObject::WriteInt64(int64_t value) 29123b3eb3cSopenharmony_ci{ 29223b3eb3cSopenharmony_ci if (memcpy_s(buffer_, bufferLen_ - offset_, &value, sizeof(int64_t)) != 0) { 29323b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 29423b3eb3cSopenharmony_ci return; 29523b3eb3cSopenharmony_ci } 29623b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(int64_t)); 29723b3eb3cSopenharmony_ci buffer_ += sizeof(int64_t); 29823b3eb3cSopenharmony_ci} 29923b3eb3cSopenharmony_ci 30023b3eb3cSopenharmony_civoid UObject::WriteDouble(double value) 30123b3eb3cSopenharmony_ci{ 30223b3eb3cSopenharmony_ci if (memcpy_s(buffer_, bufferLen_ - offset_, &value, sizeof(double)) != 0) { 30323b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 30423b3eb3cSopenharmony_ci return; 30523b3eb3cSopenharmony_ci } 30623b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(double)); 30723b3eb3cSopenharmony_ci buffer_ += sizeof(double); 30823b3eb3cSopenharmony_ci} 30923b3eb3cSopenharmony_ci 31023b3eb3cSopenharmony_civoid UObject::WriteString(const std::string& value) 31123b3eb3cSopenharmony_ci{ 31223b3eb3cSopenharmony_ci if (value.empty()) { 31323b3eb3cSopenharmony_ci return; 31423b3eb3cSopenharmony_ci } 31523b3eb3cSopenharmony_ci if (memcpy_s(buffer_, bufferLen_ - offset_, value.c_str(), value.length()) != 0) { 31623b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 31723b3eb3cSopenharmony_ci return; 31823b3eb3cSopenharmony_ci } 31923b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(value.length()); 32023b3eb3cSopenharmony_ci buffer_ += value.length(); 32123b3eb3cSopenharmony_ci} 32223b3eb3cSopenharmony_ci 32323b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, const std::string& value) 32423b3eb3cSopenharmony_ci{ 32523b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::STRING)); 32623b3eb3cSopenharmony_ci WriteInt32(key.length()); 32723b3eb3cSopenharmony_ci WriteString(key); 32823b3eb3cSopenharmony_ci WriteInt32(value.length()); 32923b3eb3cSopenharmony_ci WriteString(value); 33023b3eb3cSopenharmony_ci} 33123b3eb3cSopenharmony_ci 33223b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, size_t value) 33323b3eb3cSopenharmony_ci{ 33423b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::SIZE_T)); 33523b3eb3cSopenharmony_ci WriteInt32(key.length()); 33623b3eb3cSopenharmony_ci WriteString(key); 33723b3eb3cSopenharmony_ci WriteSizeT(value); 33823b3eb3cSopenharmony_ci} 33923b3eb3cSopenharmony_ci 34023b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, int32_t value) 34123b3eb3cSopenharmony_ci{ 34223b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::INT32)); 34323b3eb3cSopenharmony_ci WriteInt32(key.length()); 34423b3eb3cSopenharmony_ci WriteString(key); 34523b3eb3cSopenharmony_ci WriteInt32(value); 34623b3eb3cSopenharmony_ci} 34723b3eb3cSopenharmony_ci 34823b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, int64_t value) 34923b3eb3cSopenharmony_ci{ 35023b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::INT64)); 35123b3eb3cSopenharmony_ci WriteInt32(key.length()); 35223b3eb3cSopenharmony_ci WriteString(key); 35323b3eb3cSopenharmony_ci WriteInt64(value); 35423b3eb3cSopenharmony_ci} 35523b3eb3cSopenharmony_ci 35623b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, double value) 35723b3eb3cSopenharmony_ci{ 35823b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::DOUBLE)); 35923b3eb3cSopenharmony_ci WriteInt32(key.length()); 36023b3eb3cSopenharmony_ci WriteString(key); 36123b3eb3cSopenharmony_ci WriteDouble(value); 36223b3eb3cSopenharmony_ci} 36323b3eb3cSopenharmony_ci 36423b3eb3cSopenharmony_civoid UObject::WriteKV(const std::string& key, bool value) 36523b3eb3cSopenharmony_ci{ 36623b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::BOOL)); 36723b3eb3cSopenharmony_ci WriteInt32(key.length()); 36823b3eb3cSopenharmony_ci WriteString(key); 36923b3eb3cSopenharmony_ci WriteChar(value); 37023b3eb3cSopenharmony_ci} 37123b3eb3cSopenharmony_ci 37223b3eb3cSopenharmony_civoid UObject::WriteObj(const std::string& key, const std::shared_ptr<UObject>& obj) 37323b3eb3cSopenharmony_ci{ 37423b3eb3cSopenharmony_ci WriteChar(static_cast<char>(ItemType::UOBJECT)); 37523b3eb3cSopenharmony_ci WriteInt32(key.length()); 37623b3eb3cSopenharmony_ci WriteString(key); 37723b3eb3cSopenharmony_ci int32_t len = obj->EstimateBufferSize(); 37823b3eb3cSopenharmony_ci WriteInt32(len); 37923b3eb3cSopenharmony_ci obj->Serialize(buffer_, len); 38023b3eb3cSopenharmony_ci buffer_ += len; 38123b3eb3cSopenharmony_ci offset_ += len; 38223b3eb3cSopenharmony_ci} 38323b3eb3cSopenharmony_ci 38423b3eb3cSopenharmony_cichar UObject::ReadChar() 38523b3eb3cSopenharmony_ci{ 38623b3eb3cSopenharmony_ci char result = constBuffer_[0]; 38723b3eb3cSopenharmony_ci offset_++; 38823b3eb3cSopenharmony_ci constBuffer_++; 38923b3eb3cSopenharmony_ci return result; 39023b3eb3cSopenharmony_ci} 39123b3eb3cSopenharmony_ci 39223b3eb3cSopenharmony_ciint32_t UObject::ReadInt32() 39323b3eb3cSopenharmony_ci{ 39423b3eb3cSopenharmony_ci int32_t result; 39523b3eb3cSopenharmony_ci if (memcpy_s(&result, sizeof(int32_t), constBuffer_, sizeof(int32_t)) != 0) { 39623b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 39723b3eb3cSopenharmony_ci return 0; 39823b3eb3cSopenharmony_ci } 39923b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(int32_t)); 40023b3eb3cSopenharmony_ci constBuffer_ += sizeof(int32_t); 40123b3eb3cSopenharmony_ci return result; 40223b3eb3cSopenharmony_ci} 40323b3eb3cSopenharmony_ci 40423b3eb3cSopenharmony_ciint64_t UObject::ReadInt64() 40523b3eb3cSopenharmony_ci{ 40623b3eb3cSopenharmony_ci int64_t result; 40723b3eb3cSopenharmony_ci if (memcpy_s(&result, sizeof(int64_t), constBuffer_, sizeof(int64_t)) != 0) { 40823b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 40923b3eb3cSopenharmony_ci return 0; 41023b3eb3cSopenharmony_ci } 41123b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(int64_t)); 41223b3eb3cSopenharmony_ci constBuffer_ += sizeof(int64_t); 41323b3eb3cSopenharmony_ci return result; 41423b3eb3cSopenharmony_ci} 41523b3eb3cSopenharmony_ci 41623b3eb3cSopenharmony_cisize_t UObject::ReadSizeT() 41723b3eb3cSopenharmony_ci{ 41823b3eb3cSopenharmony_ci size_t result; 41923b3eb3cSopenharmony_ci if (memcpy_s(&result, sizeof(size_t), constBuffer_, sizeof(size_t)) != 0) { 42023b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 42123b3eb3cSopenharmony_ci return 0; 42223b3eb3cSopenharmony_ci } 42323b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(size_t)); 42423b3eb3cSopenharmony_ci constBuffer_ += sizeof(size_t); 42523b3eb3cSopenharmony_ci return result; 42623b3eb3cSopenharmony_ci} 42723b3eb3cSopenharmony_ci 42823b3eb3cSopenharmony_cidouble UObject::ReadDouble() 42923b3eb3cSopenharmony_ci{ 43023b3eb3cSopenharmony_ci double result; 43123b3eb3cSopenharmony_ci if (memcpy_s(&result, sizeof(double), constBuffer_, sizeof(double)) != 0) { 43223b3eb3cSopenharmony_ci LOGE("memcpy overflow."); 43323b3eb3cSopenharmony_ci return 0; 43423b3eb3cSopenharmony_ci } 43523b3eb3cSopenharmony_ci offset_ += static_cast<int32_t>(sizeof(double)); 43623b3eb3cSopenharmony_ci constBuffer_ += sizeof(double); 43723b3eb3cSopenharmony_ci return result; 43823b3eb3cSopenharmony_ci} 43923b3eb3cSopenharmony_ci 44023b3eb3cSopenharmony_cistd::string UObject::ReadString(int32_t len) 44123b3eb3cSopenharmony_ci{ 44223b3eb3cSopenharmony_ci std::string result(constBuffer_, len); 44323b3eb3cSopenharmony_ci offset_ += len; 44423b3eb3cSopenharmony_ci constBuffer_ += len; 44523b3eb3cSopenharmony_ci return result; 44623b3eb3cSopenharmony_ci} 44723b3eb3cSopenharmony_ci 44823b3eb3cSopenharmony_cistd::shared_ptr<UObject> UObject::ReadObj(int32_t len) 44923b3eb3cSopenharmony_ci{ 45023b3eb3cSopenharmony_ci std::shared_ptr<UObject> obj = std::make_shared<UObject>(); 45123b3eb3cSopenharmony_ci obj->Deserialize(constBuffer_, len); 45223b3eb3cSopenharmony_ci offset_ += len; 45323b3eb3cSopenharmony_ci constBuffer_ += len; 45423b3eb3cSopenharmony_ci return obj; 45523b3eb3cSopenharmony_ci} 45623b3eb3cSopenharmony_ci 45723b3eb3cSopenharmony_cistd::string UObject::ReadKey() 45823b3eb3cSopenharmony_ci{ 45923b3eb3cSopenharmony_ci int32_t keyLen = ReadInt32(); 46023b3eb3cSopenharmony_ci std::string key = ReadString(keyLen); 46123b3eb3cSopenharmony_ci return key; 46223b3eb3cSopenharmony_ci} 46323b3eb3cSopenharmony_ci 46423b3eb3cSopenharmony_civoid UObject::ReadKV() 46523b3eb3cSopenharmony_ci{ 46623b3eb3cSopenharmony_ci ItemType type = static_cast<ItemType>(ReadChar()); 46723b3eb3cSopenharmony_ci if (type == ItemType::STRING) { 46823b3eb3cSopenharmony_ci std::string key = ReadKey(); 46923b3eb3cSopenharmony_ci int32_t valueLen = ReadInt32(); 47023b3eb3cSopenharmony_ci std::string value = ReadString(valueLen); 47123b3eb3cSopenharmony_ci AddItemToObject(key, value); 47223b3eb3cSopenharmony_ci } else if (type == ItemType::SIZE_T) { 47323b3eb3cSopenharmony_ci std::string key = ReadKey(); 47423b3eb3cSopenharmony_ci size_t value = ReadSizeT(); 47523b3eb3cSopenharmony_ci AddItemToObject(key, value); 47623b3eb3cSopenharmony_ci } else if (type == ItemType::INT32) { 47723b3eb3cSopenharmony_ci std::string key = ReadKey(); 47823b3eb3cSopenharmony_ci int32_t value = ReadInt32(); 47923b3eb3cSopenharmony_ci AddItemToObject(key, value); 48023b3eb3cSopenharmony_ci } else if (type == ItemType::INT64) { 48123b3eb3cSopenharmony_ci std::string key = ReadKey(); 48223b3eb3cSopenharmony_ci int64_t value = ReadInt64(); 48323b3eb3cSopenharmony_ci AddItemToObject(key, value); 48423b3eb3cSopenharmony_ci } else if (type == ItemType::DOUBLE) { 48523b3eb3cSopenharmony_ci std::string key = ReadKey(); 48623b3eb3cSopenharmony_ci double value = ReadDouble(); 48723b3eb3cSopenharmony_ci AddItemToObject(key, value); 48823b3eb3cSopenharmony_ci } else if (type == ItemType::BOOL) { 48923b3eb3cSopenharmony_ci std::string key = ReadKey(); 49023b3eb3cSopenharmony_ci bool value = ReadChar(); 49123b3eb3cSopenharmony_ci AddItemToObject(key, value); 49223b3eb3cSopenharmony_ci } else if (type == ItemType::UOBJECT) { 49323b3eb3cSopenharmony_ci std::string key = ReadKey(); 49423b3eb3cSopenharmony_ci int32_t objLen = ReadInt32(); 49523b3eb3cSopenharmony_ci std::shared_ptr<UObject> obj = ReadObj(objLen); 49623b3eb3cSopenharmony_ci AddItemToObject(key, obj); 49723b3eb3cSopenharmony_ci } 49823b3eb3cSopenharmony_ci} 49923b3eb3cSopenharmony_ci} // namespace OHOS 500