From a3ae5e6b422a73f1dcad4ad5fd0a6ea452691399 Mon Sep 17 00:00:00 2001 From: bjd Date: Thu, 22 May 2025 20:00:20 +0800 Subject: [PATCH 1/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- bundle.json | 2 +- .../adapter/communicator/BUILD.gn | 2 +- .../adapter/communicator/test/BUILD.gn | 2 +- services/distributeddataservice/app/BUILD.gn | 2 +- .../distributeddataservice/app/test/BUILD.gn | 6 +- .../distributeddataservice/framework/BUILD.gn | 2 +- .../framework/include/cloud/subscription.h | 6 + .../include/serializable/serializable.h | 116 ++- .../framework/serializable/serializable.cpp | 857 +++++++++++++++++- .../framework/test/BUILD.gn | 12 +- .../framework/test/cloud_test.cpp | 7 +- .../framework/test/meta_data_test.cpp | 6 +- .../framework/test/serializable_test.cpp | 6 +- .../framework/test/subscription_test.cpp | 4 +- .../rust/extension/BUILD.gn | 2 +- .../service/backup/BUILD.gn | 2 +- .../service/bootstrap/BUILD.gn | 2 +- .../service/cloud/BUILD.gn | 2 +- .../service/config/BUILD.gn | 2 +- .../config/src/model/component_config.cpp | 2 +- .../service/data_share/BUILD.gn | 2 +- .../service/dumper/BUILD.gn | 2 +- .../service/kvdb/BUILD.gn | 2 +- .../service/matrix/BUILD.gn | 2 +- .../service/permission/BUILD.gn | 2 +- .../service/test/BUILD.gn | 30 +- .../test/data_share_profile_config_test.cpp | 4 +- .../objectservicestub_fuzzer/BUILD.gn | 2 +- 28 files changed, 1014 insertions(+), 74 deletions(-) diff --git a/bundle.json b/bundle.json index e80707c82..eb78c8cf7 100644 --- a/bundle.json +++ b/bundle.json @@ -92,7 +92,7 @@ "app_file_service", "file_api", "openssl", - "json", + "cJSON", "dmsfwk", "data_object", "init" diff --git a/services/distributeddataservice/adapter/communicator/BUILD.gn b/services/distributeddataservice/adapter/communicator/BUILD.gn index 9cb388498..c46ff0ade 100755 --- a/services/distributeddataservice/adapter/communicator/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/BUILD.gn @@ -64,7 +64,7 @@ ohos_source_set("distributeddata_communicator") { "dsoftbus:softbus_client", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/adapter/communicator/test/BUILD.gn b/services/distributeddataservice/adapter/communicator/test/BUILD.gn index a85976697..04663104d 100755 --- a/services/distributeddataservice/adapter/communicator/test/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/test/BUILD.gn @@ -215,7 +215,7 @@ ohos_unittest("SoftbusAdapterStandardTest") { "dsoftbus:softbus_client", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddata_inner", "kv_store:distributeddb", diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 45849945d..85cd9d5be 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -148,7 +148,7 @@ ohos_shared_library("distributeddataservice") { "hitrace:hitrace_meter", "hitrace:libhitracechain", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddata_mgr", "kv_store:distributeddb", diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 002e0dfa9..bfb70673a 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -168,7 +168,7 @@ ohos_unittest("SessionManagerTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", @@ -242,7 +242,7 @@ ohos_unittest("KvStoreDataServiceClearTest") { "hitrace:hitrace_meter", "hitrace:libhitracechain", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", @@ -361,7 +361,7 @@ ohos_unittest("UpgradeManagerTest") { "c_utils:utils", "googletest:gtest_main", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", ] diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 368fe2443..83b8bc210 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -139,7 +139,7 @@ ohos_shared_library("distributeddatasvcfwk") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "openssl:libcrypto_shared", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/framework/include/cloud/subscription.h b/services/distributeddataservice/framework/include/cloud/subscription.h index 4054a91f3..29a8601c1 100644 --- a/services/distributeddataservice/framework/include/cloud/subscription.h +++ b/services/distributeddataservice/framework/include/cloud/subscription.h @@ -28,6 +28,9 @@ struct API_EXPORT Subscription final : public Serializable { std::map relations; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; + void SetRelations(Serializable::json &node, const std::map &values) { + Serializable::SetValue(node, values); + } }; bool Marshal(json &node) const; @@ -38,6 +41,9 @@ struct API_EXPORT Subscription final : public Serializable { static std::string GetKey(int32_t userId); static std::string GetRelationKey(int32_t userId, const std::string &bundleName); static std::string GetPrefix(const std::initializer_list &fields); + void SetSubscriptions(Serializable::json &node, const std::map &values) { + Serializable::SetValue(node, values); + } private: static constexpr const char *PREFIX = "CLOUD_SUBSCRIPTION"; diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index a0b6ad5a7..df04dfa11 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -15,14 +15,16 @@ #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_COMMON_SERIALIZABLE_H +#include #include #include +#include #include "visibility.h" #ifndef JSON_NOEXCEPTION #define JSON_NOEXCEPTION #endif #include -#include +struct cJSON; namespace OHOS { namespace DistributedData { #ifndef GET_NAME @@ -30,9 +32,101 @@ namespace DistributedData { #endif struct Serializable { public: - using json = nlohmann::json; - using size_type = nlohmann::json::size_type; - using error_handler_t = nlohmann::detail::error_handler_t; + class iterator; + class JSONWrapper final{ + public: + friend iterator; + enum class Type : uint8_t { + ARRAY, + OBJECT, + }; + API_EXPORT JSONWrapper(); + API_EXPORT JSONWrapper(cJSON *json, cJSON *root, const std::string &key = ""); + API_EXPORT JSONWrapper(const std::string &jsonStr); + API_EXPORT JSONWrapper(JSONWrapper &&jsonWrapper); + + API_EXPORT operator std::string() const; + API_EXPORT bool operator==(int32_t value) const; + API_EXPORT bool operator==(const std::string &value) const; + + API_EXPORT JSONWrapper &operator=(JSONWrapper &&jsonWrapper); + API_EXPORT JSONWrapper &operator=(JSONWrapper::Type type); + API_EXPORT JSONWrapper &operator=(bool value); + API_EXPORT JSONWrapper &operator=(int32_t value); + API_EXPORT JSONWrapper &operator=(uint32_t value); + API_EXPORT JSONWrapper &operator=(int64_t value); + API_EXPORT JSONWrapper &operator=(uint64_t value); + API_EXPORT JSONWrapper &operator=(double value); + API_EXPORT JSONWrapper &operator=(const char *value); + API_EXPORT JSONWrapper &operator=(const std::string &value); + API_EXPORT JSONWrapper &operator=(const std::vector &value); + API_EXPORT JSONWrapper &operator[](const std::string &key); + API_EXPORT JSONWrapper &operator[](size_t index); + API_EXPORT JSONWrapper &operator[](size_t index) const; + + API_EXPORT bool is_null() const; + API_EXPORT bool is_boolean() const; + API_EXPORT bool is_number_integer() const; + API_EXPORT bool is_number_unsigned() const; + API_EXPORT bool is_number_float() const; + API_EXPORT bool is_string() const; + API_EXPORT bool is_array() const; + API_EXPORT bool is_object() const; + API_EXPORT bool is_discarded() const; + API_EXPORT bool get_to(bool &values) const; + API_EXPORT bool get_to(int16_t &values) const; + API_EXPORT bool get_to(uint16_t &values) const; + API_EXPORT bool get_to(int32_t &values) const; + API_EXPORT bool get_to(uint32_t &values) const; + API_EXPORT bool get_to(int64_t &values) const; + API_EXPORT bool get_to(uint64_t &values) const; + API_EXPORT bool get_to(double &values) const; + API_EXPORT bool get_to(std::string &values) const; + API_EXPORT bool get_to(std::vector &values) const; + API_EXPORT size_t size() const; + API_EXPORT std::string dump() const; + API_EXPORT iterator find(const std::string &key) const; + API_EXPORT iterator begin() const; + API_EXPORT iterator end() const; + API_EXPORT ~JSONWrapper(); + API_EXPORT static JSONWrapper parse(const std::string &str); + API_EXPORT static bool accept(const std::string &str); + + API_EXPORT static JSONWrapper array(); + API_EXPORT void push_back(const JSONWrapper &value); + API_EXPORT static std::string to_string(const JSONWrapper &jsonWrapper); + API_EXPORT bool operator==(const std::map& value) const; + API_EXPORT bool operator==(const std::map& value) const; + API_EXPORT bool operator==(const std::vector& value) const; + API_EXPORT bool empty() const; + API_EXPORT JSONWrapper& operator=(const std::map& value); + API_EXPORT JSONWrapper &operator=(const std::map& value); + API_EXPORT bool erase(const std::string& key); + API_EXPORT bool erase(int index); + private: + void AddToRoot(); + JSONWrapper(const JSONWrapper& jsonWrapper) = delete; + JSONWrapper &operator=(const JSONWrapper &jsonWrapper) = delete; + cJSON *json_ = nullptr; + cJSON *root_ = nullptr; + std::string key_; + bool needDel_ = false; + mutable std::vector> children_; + }; + class iterator { + public: + API_EXPORT iterator(std::vector>::iterator it); + API_EXPORT iterator &operator++(); + API_EXPORT bool operator==(const iterator &iter) const; + API_EXPORT bool operator!=(const iterator &iter) const; + API_EXPORT const JSONWrapper &operator*() const; + API_EXPORT std::string key() const; + API_EXPORT const JSONWrapper &value() const; + + private: + std::vector>::iterator node_; + }; + using json = JSONWrapper; API_EXPORT json Marshall() const; template @@ -40,7 +134,7 @@ public: { json root; SetValue(root, values); - return root.dump(-1, ' ', false, error_handler_t::replace); + return root.dump(); } API_EXPORT bool Unmarshall(const std::string &jsonStr); @@ -62,6 +156,7 @@ public: API_EXPORT static bool GetValue(const json &node, const std::string &name, bool &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, std::vector &value); API_EXPORT static bool GetValue(const json &node, const std::string &name, Serializable &value); + API_EXPORT static bool GetValue(const json &node, const std::string &name, double &value); API_EXPORT static bool SetValue(json &node, const std::string &value); API_EXPORT static bool SetValue(json &node, const uint32_t &value); API_EXPORT static bool SetValue(json &node, const int32_t &value); @@ -128,8 +223,9 @@ bool Serializable::GetValue(const json &node, const std::string &name, std::vect return false; } bool result = true; - values.resize(subNode.size()); - for (size_type i = 0; i < subNode.size(); ++i) { + auto size = subNode.size(); + values.resize(size); + for (size_t i = 0; i < size; ++i) { result = GetValue(subNode[i], "", values[i]) && result; } return result; @@ -139,8 +235,8 @@ template bool Serializable::SetValue(json &node, const std::vector &values) { bool result = true; - size_type i = 0; - node = json::value_t::array; + size_t i = 0; + node = JSONWrapper::Type::ARRAY; for (const auto &value : values) { result = SetValue(node[i], value) && result; i++; @@ -166,7 +262,7 @@ template bool Serializable::SetValue(json &node, const std::map &values) { bool result = true; - node = json::value_t::object; + node = JSONWrapper::Type::OBJECT; for (const auto &[key, value] : values) { result = SetValue(node[key], value) && result; } diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index 253a93234..a09c8de82 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -13,7 +13,11 @@ * limitations under the License. */ +#define LOG_TAG "serializable" +#include "log_print.h" #include "serializable/serializable.h" + +#include namespace OHOS { namespace DistributedData { Serializable::json Serializable::Marshall() const @@ -25,13 +29,13 @@ Serializable::json Serializable::Marshall() const bool Serializable::Unmarshall(const std::string &jsonStr) { - json jsonObj = json::parse(jsonStr, nullptr, false); + json jsonObj = json::parse(jsonStr); if (jsonObj.is_discarded()) { // if the string size is less than 1, means the string is invalid. if (jsonStr.empty()) { return false; } - jsonObj = json::parse(jsonStr.substr(1), nullptr, false); // drop first char to adapt A's value; + jsonObj = json::parse(jsonStr.substr(1)); // drop first char to adapt A's value; if (jsonObj.is_discarded()) { return false; } @@ -41,13 +45,13 @@ bool Serializable::Unmarshall(const std::string &jsonStr) Serializable::json Serializable::ToJson(const std::string &jsonStr) { - json jsonObj = json::parse(jsonStr, nullptr, false); + json jsonObj = json::parse(jsonStr); if (jsonObj.is_discarded()) { // if the string size is less than 1, means the string is invalid. if (jsonStr.empty()) { return {}; } - jsonObj = json::parse(jsonStr.substr(1), nullptr, false); // drop first char to adapt A's value; + jsonObj = json::parse(jsonStr.substr(1)); // drop first char to adapt A's value; if (jsonObj.is_discarded()) { return {}; } @@ -58,7 +62,7 @@ Serializable::json Serializable::ToJson(const std::string &jsonStr) bool Serializable::IsJson(const std::string &jsonStr) { if (!json::accept(jsonStr)) { - return json::accept(jsonStr.begin() + 1, jsonStr.end()); + return json::accept(jsonStr.substr(1)); } return true; } @@ -69,8 +73,7 @@ bool Serializable::GetValue(const json &node, const std::string &name, std::stri if (subNode.is_null() || !subNode.is_string()) { return false; } - value = subNode; - return true; + return subNode.get_to(value); } bool Serializable::GetValue(const json &node, const std::string &name, uint32_t &value) @@ -147,8 +150,8 @@ bool Serializable::GetValue(const json &node, const std::string &name, std::vect if (subNode.is_null() || !subNode.is_array()) { return false; } - value = std::vector(subNode); - return true; + + return subNode.get_to(value); } bool Serializable::GetValue(const json &node, const std::string &name, Serializable &value) @@ -160,6 +163,16 @@ bool Serializable::GetValue(const json &node, const std::string &name, Serializa return value.Unmarshal(subNode); } +bool Serializable::GetValue(const json &node, const std::string &name, double &value) +{ + auto &subNode = GetSubNode(node, name); + if (subNode.is_null() || !subNode.is_number_integer()) { + return false; + } + subNode.get_to(value); + return true; +} + bool Serializable::SetValue(json &node, const std::string &value) { node = value; @@ -215,7 +228,7 @@ bool Serializable::SetValue(json &node, const Serializable &value) const Serializable::json &Serializable::GetSubNode(const json &node, const std::string &name) { - static const json jsonNull = json::value_t::null; + static const json jsonNull; if (node.is_discarded() || node.is_null()) { return jsonNull; } @@ -230,5 +243,827 @@ const Serializable::json &Serializable::GetSubNode(const json &node, const std:: } return *it; } + +Serializable::JSONWrapper::JSONWrapper() : root_(nullptr) +{ + json_ = cJSON_CreateNull(); +} + +Serializable::JSONWrapper::JSONWrapper(cJSON *json, cJSON *root, const std::string &key) + : json_(json), root_(root), key_(key), needDel_(root == nullptr) +{ +} + +Serializable::JSONWrapper::JSONWrapper(const std::string &jsonStr) : root_(nullptr), needDel_(true) +{ + if (json_ != nullptr) { + cJSON_Delete(json_); + children_.clear(); + } + json_ = cJSON_Parse(jsonStr.c_str()); +} + +Serializable::JSONWrapper::JSONWrapper(JSONWrapper &&jsonWrapper) +{ + if (this == &jsonWrapper) { + return; + } + if (json_ != nullptr) { + if (root_ == nullptr) { + cJSON_Delete(json_); + } + json_ = nullptr; + root_ = nullptr; + } + json_ = std::move(jsonWrapper.json_); + jsonWrapper.json_ = nullptr; + root_ = jsonWrapper.root_; + jsonWrapper.root_ = nullptr; + key_ = std::move(jsonWrapper.key_); + children_ = std::move(jsonWrapper.children_); + needDel_ = jsonWrapper.needDel_; +} + +Serializable::JSONWrapper::operator std::string() const +{ + return dump(); +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(JSONWrapper &&jsonWrapper) +{ + if (root_ == nullptr) { + cJSON_Delete(json_); + json_ = nullptr; + } + json_ = std::move(jsonWrapper.json_); + jsonWrapper.json_ = nullptr; + root_ = jsonWrapper.root_; + jsonWrapper.root_ = nullptr; + key_ = std::move(jsonWrapper.key_); + children_ = std::move(jsonWrapper.children_); + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(Serializable::JSONWrapper::Type type) +{ + if (json_ != nullptr) { + ZLOGE("cannot use operator[]"); + return *this; + } + switch (type) { + case Type::ARRAY: + json_ = cJSON_CreateArray(); + break; + case Type::OBJECT: + json_ = cJSON_CreateObject(); + } + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(bool value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ == nullptr) { + json_ = cJSON_CreateBool(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + } + if (json_ == nullptr) { + return *this; + } + if (cJSON_IsBool(json_)) { + cJSON_SetBoolValue(json_, value); + return *this; + } + cJSON *node = cJSON_CreateBool(value); + if (node == nullptr) { + return *this; + } + if (!ReplaceNode(node)) { + cJSON_Delete(node); + } + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(int32_t value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ != nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); + } + return *this; + } + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(uint32_t value) +{ + int32_t number = static_cast(value); + return operator=(number); +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(int64_t value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ != nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); + } + return *this; + } + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(uint64_t value) +{ + int64_t number = static_cast(value); + return operator=(number); +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(double value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ != nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); + } + return *this; + } + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(const char *value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + + if (json_ == nullptr) { + json_ = cJSON_CreateString(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + } + if (cJSON_IsString(json_)) { + cJSON_SetValuestring(json_, value); + return *this; + } + + cJSON *node = cJSON_CreateString(value); + if (node == nullptr) { + return *this; + } + if (!ReplaceNode(node)) { + cJSON_Delete(node); + } + return *this; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(const std::string &value) +{ + return operator=(value.c_str()); +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(const std::vector &value) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ != nullptr) { + return *this; + } + json_ = cJSON_CreateArray(); + children_.clear(); + for (size_t i = 0; json_ && (i < value.size()); i++) { + auto node = cJSON_CreateNumber(value[i]); + if (!node || !cJSON_AddItemToArray(json_, node)) { + cJSON_Delete(json_); + json_ = nullptr; + children_.clear(); + return *this; + } + children_.push_back(std::make_shared(node, json_)); + } + if (json_ == nullptr || root_ == nullptr) { + return *this; + } + AddToRoot(); + return *this; +} + +bool Serializable::JSONWrapper::ReplaceNode(cJSON *node) +{ + if (root_ == nullptr) { + cJSON_Delete(json_); + json_ = node; + return true; + } + + bool success = false; + if (key_.empty()) { + success = cJSON_ReplaceItemViaPointer(root_, json_, node); + } else { + success = cJSON_ReplaceItemInObject(root_, key_.c_str(), node); + } + + if (success) { + json_ = node; + } + return success; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](const std::string &key) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ == nullptr) { + json_ = cJSON_CreateObject(); + if (json_ != nullptr && root_ != nullptr) { + AddToRoot(); + } + } + if (!is_object()) { + ZLOGE("cannot use operator[]."); + } + auto it = children_.begin(); + while (it != children_.end()) { + if ((*it)->key_ == key) { + return **it; + } + ++it; + } + auto item = cJSON_GetObjectItem(json_, key.c_str()); + auto res = std::make_shared(item, json_, key); + children_.push_back(res); + return *res; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](size_t index) +{ + if (root_ == nullptr && cJSON_IsNull(json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } + if (json_ == nullptr) { + json_ = cJSON_CreateArray(); + if (json_ != nullptr && root_ != nullptr) { + AddToRoot(); + } + } + if (!is_array()) { + ZLOGE("cannot use operator[]."); + //throw std::invalid_argument("cannot use operator[]"); + } + int size = cJSON_GetArraySize(json_); + auto len = children_.size(); + while (len < size) { + auto item = cJSON_GetArrayItem(json_, len); + children_.push_back(std::make_shared(item, json_)); + len++; + } + if (index > len) { + ZLOGE("cannot use operator[]."); + //throw std::invalid_argument("cannot use operator[]"); + } + if (index == len) { + auto item = cJSON_GetArrayItem(json_, len); + children_.push_back(std::make_shared(item, json_)); + } + return *children_[index]; +} + +Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](size_t index) const +{ + if (!is_array()) { + ZLOGE("invalid args."); + //throw std::invalid_argument("invalid args"); + } + int size = cJSON_GetArraySize(json_); + if (index >= size) { + ZLOGE("invalid args."); + //throw std::out_of_range("invalid args"); + } + auto len = children_.size(); + while (len < size) { + auto item = cJSON_GetArrayItem(json_, len); + children_.push_back(std::make_shared(item, json_)); + len++; + } + return *children_[index]; +} + +bool Serializable::JSONWrapper::is_null() const +{ + return cJSON_IsNull(json_); +} + +bool Serializable::JSONWrapper::is_boolean() const +{ + return cJSON_IsBool(json_); +} + +bool Serializable::JSONWrapper::is_number_integer() const +{ + return cJSON_IsNumber(json_); +} + +bool Serializable::JSONWrapper::is_number_unsigned() const +{ + return cJSON_IsNumber(json_) && cJSON_GetNumberValue(json_) >= 0; +} + +bool Serializable::JSONWrapper::is_number_float() const +{ + return cJSON_IsNumber(json_); +} + +bool Serializable::JSONWrapper::is_string() const +{ + return cJSON_IsString(json_); +} + +bool Serializable::JSONWrapper::is_array() const +{ + return cJSON_IsArray(json_); +} + +bool Serializable::JSONWrapper::is_object() const +{ + return cJSON_IsObject(json_); +} + +bool Serializable::JSONWrapper::is_discarded() const +{ + return json_ == nullptr; +} + +bool Serializable::JSONWrapper::accept(const std::string &str) +{ + cJSON *json = cJSON_Parse(str.c_str()); + if (json == nullptr) { + return false; + } + + cJSON_Delete(json); + return true; +} + +bool Serializable::JSONWrapper::get_to(bool &values) const +{ + if (json_ == nullptr || !is_boolean()) { + return false; + } + + values = cJSON_IsTrue(json_) ? true : false; + return true; +} + +bool Serializable::JSONWrapper::get_to(int16_t &values) const +{ + if (json_ == nullptr || !is_number_integer()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(uint16_t &values) const +{ + if (json_ == nullptr || !is_number_unsigned()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(int32_t &values) const +{ + if (json_ == nullptr || !is_number_integer()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(uint32_t &values) const +{ + if (json_ == nullptr || !is_number_unsigned()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(int64_t &values) const +{ + if (json_ == nullptr || !is_number_integer()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(uint64_t &values) const +{ + if (json_ == nullptr || !is_number_unsigned()) { + return false; + } + values = json_->valueint; + return true; +} + +bool Serializable::JSONWrapper::get_to(double &values) const +{ + if (json_ == nullptr || !is_number_integer()) { + return false; + } + values = cJSON_GetNumberValue(json_); + return true; +} + +bool Serializable::JSONWrapper::get_to(std::string &values) const +{ + if (json_ == nullptr || !is_string()) { + return false; + } + values = cJSON_GetStringValue(json_); + return true; +} + +bool Serializable::JSONWrapper::get_to(std::vector &values) const +{ + if (json_ == nullptr || !is_array()) { + return false; + } + auto size = cJSON_GetArraySize(json_); + values.clear(); + values.reserve(size); + for (auto i = 0; i < size; i++) { + auto item = cJSON_GetArrayItem(json_, i); + if (item) { + values.push_back(cJSON_GetNumberValue(item)); + } + } + return true; +} + +bool Serializable::JSONWrapper::empty() const +{ + if (json_ == nullptr) { + return true; + } + if (cJSON_IsNull(json_)) { + return true; + } + if (cJSON_IsArray(json_)) { + return cJSON_GetArraySize(json_) == 0; + } + if (cJSON_IsObject(json_)) { + int size = 0; + cJSON *child = json_->child; + while (child) { + size++; + child = child->next; + } + return size == 0; + } + return false; +} + +size_t Serializable::JSONWrapper::size() const +{ + if (!cJSON_IsArray(json_) && !cJSON_IsObject(json_)) { + return 0; + } + return cJSON_GetArraySize(json_); +} + +std::string Serializable::JSONWrapper::dump() const +{ + if (json_ == nullptr) { + return ""; + } + char *str = cJSON_PrintUnformatted(json_); + std::string res(str); + cJSON_free(str); + return res; +} + +Serializable::iterator Serializable::JSONWrapper::find(const std::string &key) const +{ + auto size = cJSON_GetArraySize(json_); + auto len = children_.size(); + if (len != size) { + children_.clear(); + for (int i = 0; i < size; i++) { + auto item = cJSON_GetArrayItem(json_, i); + children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); + } + } + auto it = children_.begin(); + while (it != children_.end()) { + if ((*it)->key_ == key) { + return it; + } + ++it; + } + return it; +} + +Serializable::iterator Serializable::JSONWrapper::begin() const +{ + if (json_ == nullptr || (!is_array() && !is_object())) { + ZLOGE("not support."); + //throw std::logic_error("not support"); + } + auto size = cJSON_GetArraySize(json_); + auto len = children_.size(); + if (len != size) { + children_.clear(); + for (int i = 0; i < size; i++) { + auto item = cJSON_GetArrayItem(json_, i); + children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); + } + } + return children_.begin(); +} + +Serializable::iterator Serializable::JSONWrapper::end() const +{ + if (json_ == nullptr || json_->child == nullptr || (!is_array() && !is_object())) { + ZLOGE("not support."); + //throw std::logic_error("not support"); + } + auto size = cJSON_GetArraySize(json_); + auto len = children_.size(); + if (len != size) { + children_.clear(); + for (int i = 0; i < size; i++) { + auto item = cJSON_GetArrayItem(json_, i); + children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); + } + } + return children_.end(); +} + +Serializable::JSONWrapper::~JSONWrapper() +{ + if (needDel_ && root_ == nullptr && json_ != nullptr) { + cJSON_Delete(json_); + } +} + +Serializable::JSONWrapper Serializable::JSONWrapper::parse(const std::string &str) +{ + return Serializable::JSONWrapper(cJSON_Parse(str.c_str()), nullptr); +} + +bool Serializable::JSONWrapper::operator==(int32_t value) const +{ + return value == cJSON_GetNumberValue(json_); +} + +bool Serializable::JSONWrapper::operator==(const std::string &value) const +{ + return value == cJSON_GetStringValue(json_); +} + +void Serializable::JSONWrapper::AddToRoot() +{ + if (!key_.empty()) { + if (!cJSON_AddItemToObject(root_, key_.c_str(), json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } else { + needDel_ = false; + } + } else { + if (!cJSON_AddItemToArray(root_, json_)) { + cJSON_Delete(json_); + json_ = nullptr; + } else { + needDel_ = false; + } + } +} + +Serializable::iterator::iterator(std::vector>::iterator it) : node_(it) {} + +Serializable::iterator &Serializable::iterator::operator++() +{ + node_++; + return *this; +} + +bool Serializable::iterator::operator==(const Serializable::iterator &iter) const +{ + return node_ == iter.node_; +} + +bool Serializable::iterator::operator!=(const Serializable::iterator &iter) const +{ + return !operator==(iter); +} + +const Serializable::JSONWrapper &Serializable::iterator::operator*() const +{ + return **node_; +} + +std::string Serializable::iterator::key() const +{ + return (*node_)->key_; +} + +const Serializable::JSONWrapper &Serializable::iterator::value() const +{ + return operator*(); +} + +Serializable::JSONWrapper Serializable::JSONWrapper::array() +{ + cJSON* json = cJSON_CreateArray(); + return Serializable::JSONWrapper(json, nullptr, ""); +} + +void Serializable::JSONWrapper::push_back(const JSONWrapper &value) +{ + if (is_array()) { + cJSON *newJson = cJSON_Duplicate(value.json_, 1); + cJSON_AddItemToArray(json_, newJson); + children_.push_back(std::make_shared(newJson, json_)); + } +} + +std::string Serializable::JSONWrapper::to_string(const JSONWrapper &jsonWrapper) +{ + return jsonWrapper.dump(); +} + +bool Serializable::JSONWrapper::operator==(const std::map& value) const +{ + if (!is_object()) { + return false; + } + if (cJSON_GetArraySize(json_) != value.size()) { + return false; + } + for (const auto& pair : value) { + auto item = cJSON_GetObjectItem(json_, pair.first.c_str()); + if (item == nullptr || std::string(cJSON_GetStringValue(item)) != pair.second) { + return false; + } + } + return true; +} + +bool Serializable::JSONWrapper::operator==(const std::map& value) const +{ + if (!is_object()) { + return false; + } + if (cJSON_GetArraySize(json_) != value.size()) { + return false; + } + for (const auto& pair : value) { + auto item = cJSON_GetObjectItem(json_, pair.first.c_str()); + if (item == nullptr || cJSON_GetNumberValue(item) != pair.second) { + return false; + } + } + return true; +} + +bool Serializable::JSONWrapper::operator==(const std::vector& value) const +{ + if (!is_array()) { + return false; + } + if (cJSON_GetArraySize(json_) != value.size()) { + return false; + } + for (size_t i = 0; i < value.size(); ++i) { + auto item = cJSON_GetArrayItem(json_, i); + if (item == nullptr || std::string(cJSON_GetStringValue(item)) != value[i]) { + return false; + } + } + return true; +} + +Serializable::JSONWrapper& Serializable::JSONWrapper::operator=(const std::map& value) +{ + if (json_) { + cJSON_Delete(json_); + json_ = nullptr; + } + json_ = cJSON_CreateObject(); + for (const auto& pair : value) { + cJSON* num = cJSON_CreateNumber(pair.second); + cJSON_AddItemToObject(json_, pair.first.c_str(), num); + } + children_.clear(); + return *this; +} + +Serializable::JSONWrapper& Serializable::JSONWrapper::operator=(const std::map& value) +{ + if (json_) { + cJSON_Delete(json_); + json_ = nullptr; + } + json_ = cJSON_CreateObject(); + for (const auto& pair : value) { + cJSON* str = cJSON_CreateString(pair.second.c_str()); + cJSON_AddItemToObject(json_, pair.first.c_str(), str); + } + children_.clear(); + return *this; +} + +bool Serializable::JSONWrapper::erase(const std::string& key) +{ + if (!json_ || !is_object()) { + return false; + } + + auto it = children_.begin(); + while (it != children_.end()) { + if ((*it)->key_ == key) { + it = children_.erase(it); + } else { + ++it; + } + } + + cJSON_DeleteItemFromObject(json_, key.c_str()); + return true; +} + +bool Serializable::JSONWrapper::erase(int index) +{ + if (!json_ || !is_array()) { + return false; + } + + int size = cJSON_GetArraySize(json_); + auto len = children_.size(); + while (len < size) { + auto item = cJSON_GetArrayItem(json_, len); + children_.push_back(std::make_shared(item, json_)); + len++; + } + + if (index < 0 || index >= size) { + return false; + } + + if (index < children_.size()) { + children_.erase(children_.begin() + index); + } + + cJSON_DeleteItemFromArray(json_, index); + return true; +} + } // namespace DistributedData -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/framework/test/BUILD.gn b/services/distributeddataservice/framework/test/BUILD.gn index 4417c7c7a..bbac87d0e 100644 --- a/services/distributeddataservice/framework/test/BUILD.gn +++ b/services/distributeddataservice/framework/test/BUILD.gn @@ -121,7 +121,7 @@ ohos_unittest("ServiceUtilsTest") { "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", ] deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] @@ -199,7 +199,7 @@ ohos_unittest("CloudInfoTest") { deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] external_deps = [ - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] } @@ -225,7 +225,7 @@ ohos_unittest("SubscriptionTest") { configs = [ ":module_private_config" ] deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] external_deps = [ - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] } @@ -250,7 +250,7 @@ ohos_unittest("MetaDataManagerTest") { "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", ] deps = [ @@ -264,7 +264,7 @@ ohos_unittest("StoreMetaDataLocalTest") { sources = [ "store_meta_data_local_test.cpp" ] configs = [ ":module_private_config" ] deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] - external_deps = [ "json:nlohmann_json_static" ] + external_deps = [ "cJSON:cjson" ] } ohos_unittest("ConstantTest") { @@ -313,7 +313,7 @@ ohos_unittest("ServiceMetaDataTest") { "hitrace:hitrace_meter", "hitrace:libhitracechain", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", diff --git a/services/distributeddataservice/framework/test/cloud_test.cpp b/services/distributeddataservice/framework/test/cloud_test.cpp index 5179ef017..f5a22b8e2 100644 --- a/services/distributeddataservice/framework/test/cloud_test.cpp +++ b/services/distributeddataservice/framework/test/cloud_test.cpp @@ -22,7 +22,6 @@ #include "cloud/cloud_info.h" #include "cloud/cloud_server.h" #include "cloud/schema_meta.h" -#include "nlohmann/json.hpp" #include "utils/crypto.h" #include "screen/screen_manager.h" #include "store/general_store.h" @@ -249,7 +248,7 @@ HWTEST_F(CloudInfoTest, CloudInfoTest001, TestSize.Level0) Serializable::json node1; cloudInfo1.Marshal(node1); - EXPECT_EQ(Serializable::Marshall(cloudInfo1), to_string(node1)); + EXPECT_EQ(Serializable::Marshall(cloudInfo1), Serializable::JSONWrapper::to_string(node1)); CloudInfo cloudInfo2; cloudInfo2.Unmarshal(node1); @@ -274,7 +273,7 @@ HWTEST_F(CloudInfoTest, AppInfoTest, TestSize.Level0) Serializable::json node1; cloudInfoAppInfo1.Marshal(node1); - EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), to_string(node1)); + EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), Serializable::JSONWrapper::to_string(node1)); CloudInfo::AppInfo cloudInfoAppInfo2; cloudInfoAppInfo2.Unmarshal(node1); @@ -304,7 +303,7 @@ HWTEST_F(CloudInfoTest, TableTest, TestSize.Level0) table1.fields.push_back(field1); Serializable::json node1; table1.Marshal(node1); - EXPECT_EQ(Serializable::Marshall(table1), to_string(node1)); + EXPECT_EQ(Serializable::Marshall(table1), Serializable::JSONWrapper::to_string(node1)); Table table2; table2.Unmarshal(node1); diff --git a/services/distributeddataservice/framework/test/meta_data_test.cpp b/services/distributeddataservice/framework/test/meta_data_test.cpp index 58359051e..4ae6ed71e 100644 --- a/services/distributeddataservice/framework/test/meta_data_test.cpp +++ b/services/distributeddataservice/framework/test/meta_data_test.cpp @@ -35,7 +35,7 @@ #include "metadata/device_meta_data.h" #include "utils/constant.h" #include "gtest/gtest.h" -#include +#include "serializable/serializable.h" using namespace testing::ext; using namespace OHOS; using namespace OHOS::DistributedKv; @@ -736,7 +736,9 @@ HWTEST_F(ServiceMetaDataTest, UserMetaData, TestSize.Level1) Serializable::json node2; userStatus.Marshal(node2); - EXPECT_EQ(node2["isActive"], true); + bool ret; + node2["isActive"].get_to(ret); + EXPECT_EQ(ret, true); EXPECT_EQ(node2["id"], USER_ID2); UserStatus userUnmarshal; diff --git a/services/distributeddataservice/framework/test/serializable_test.cpp b/services/distributeddataservice/framework/test/serializable_test.cpp index 14a00bf91..4c8ece234 100644 --- a/services/distributeddataservice/framework/test/serializable_test.cpp +++ b/services/distributeddataservice/framework/test/serializable_test.cpp @@ -128,7 +128,7 @@ HWTEST_F(SerializableTest, GetNormalVal, TestSize.Level2) normal.value = -56; normal.isClear = true; normal.cols = {"adfasdfas"}; - auto jstr = to_string(normal.Marshall()); + auto jstr = Serializable::JSONWrapper::to_string(normal.Marshall()); Normal normal1; normal1.Unmarshall(jstr); ASSERT_TRUE(normal == normal1) << normal1.name; @@ -161,7 +161,7 @@ HWTEST_F(SerializableTest, GetMutilVal, TestSize.Level2) NormalEx normalEx; normalEx.normals = {Normal()}; normalEx.name = "normalEx"; - auto jstr = to_string(normalEx.Marshall()); + auto jstr = Serializable::JSONWrapper::to_string(normalEx.Marshall()); NormalEx normal1; normal1.Unmarshall(jstr); ASSERT_TRUE(normalEx == normal1) << normal1.name; @@ -296,7 +296,7 @@ HWTEST_F(SerializableTest, SetPointerValue, TestSize.Level2) in.value = new int64_t(-100); in.status = new uint32_t(110); in.isClear = new bool(true); - auto json = to_string(in.Marshall()); + auto json = Serializable::JSONWrapper::to_string(in.Marshall()); Test out; out.Unmarshall(json); ASSERT_TRUE(in == out) << in.count; diff --git a/services/distributeddataservice/framework/test/subscription_test.cpp b/services/distributeddataservice/framework/test/subscription_test.cpp index 00cffdd43..2b70c8a07 100644 --- a/services/distributeddataservice/framework/test/subscription_test.cpp +++ b/services/distributeddataservice/framework/test/subscription_test.cpp @@ -58,8 +58,8 @@ HWTEST_F(SubscriptionTest, RelationUnmarshal, TestSize.Level1) Subscription::json node; node["id"] = "testId"; node["bundleName"] = "testBundleName"; - node["relations"] = testRelation; Subscription::Relation relation; + relation.SetRelations(node["relations"], testRelation); relation.Unmarshal(node); ASSERT_EQ(relation.id, "testId"); ASSERT_EQ(relation.bundleName, "testBundleName"); @@ -94,8 +94,8 @@ HWTEST_F(SubscriptionTest, Unmarshal, TestSize.Level1) Subscription::json node; node["userId"] = 100; node["id"] = "testId"; - node["expiresTime"] = testExpiresTime; Subscription subscription; + subscription.SetSubscriptions(node["expiresTime"], testExpiresTime); subscription.Unmarshal(node); ASSERT_EQ(subscription.userId, 100); ASSERT_EQ(subscription.id, "testId"); diff --git a/services/distributeddataservice/rust/extension/BUILD.gn b/services/distributeddataservice/rust/extension/BUILD.gn index 6cbd29243..5a620259a 100644 --- a/services/distributeddataservice/rust/extension/BUILD.gn +++ b/services/distributeddataservice/rust/extension/BUILD.gn @@ -60,7 +60,7 @@ ohos_shared_library("opencloudextension") { external_deps = [ "access_token:libaccesstoken_sdk", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/service/backup/BUILD.gn b/services/distributeddataservice/service/backup/BUILD.gn index 6b90fa32f..d0e21358c 100755 --- a/services/distributeddataservice/service/backup/BUILD.gn +++ b/services/distributeddataservice/service/backup/BUILD.gn @@ -56,7 +56,7 @@ ohos_source_set("distributeddata_backup") { external_deps = [ "device_manager:devicemanagersdk", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/service/bootstrap/BUILD.gn b/services/distributeddataservice/service/bootstrap/BUILD.gn index ff48eaece..0f6d8f04d 100644 --- a/services/distributeddataservice/service/bootstrap/BUILD.gn +++ b/services/distributeddataservice/service/bootstrap/BUILD.gn @@ -54,7 +54,7 @@ ohos_source_set("distributeddata_bootstrap") { external_deps = [ "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/service/cloud/BUILD.gn b/services/distributeddataservice/service/cloud/BUILD.gn index b36909c07..92a49cb1e 100755 --- a/services/distributeddataservice/service/cloud/BUILD.gn +++ b/services/distributeddataservice/service/cloud/BUILD.gn @@ -70,7 +70,7 @@ ohos_source_set("distributeddata_cloud") { "access_token:libtokenid_sdk", "device_manager:devicemanagersdk", "hicollie:libhicollie", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", "relational_store:cloud_data_inner", diff --git a/services/distributeddataservice/service/config/BUILD.gn b/services/distributeddataservice/service/config/BUILD.gn index cccd96958..aaf607e58 100644 --- a/services/distributeddataservice/service/config/BUILD.gn +++ b/services/distributeddataservice/service/config/BUILD.gn @@ -60,7 +60,7 @@ ohos_source_set("distributeddata_config") { deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] external_deps = [ "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", ] subsystem_name = "distributeddatamgr" part_name = "datamgr_service" diff --git a/services/distributeddataservice/service/config/src/model/component_config.cpp b/services/distributeddataservice/service/config/src/model/component_config.cpp index 3810ece2b..9195e7dc8 100644 --- a/services/distributeddataservice/service/config/src/model/component_config.cpp +++ b/services/distributeddataservice/service/config/src/model/component_config.cpp @@ -36,7 +36,7 @@ bool ComponentConfig::Unmarshal(const json &node) GetValue(node, GET_NAME(destructor), destructor); const auto &subNode = GetSubNode(node, GET_NAME(params)); if (!subNode.is_null()) { - params = to_string(subNode); + params = Serializable::JSONWrapper::to_string(subNode); } return true; } diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index ef7b7be1f..dce11c874 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -129,7 +129,7 @@ ohos_source_set("data_share_service") { "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", "qos_manager:concurrent_task_client", diff --git a/services/distributeddataservice/service/dumper/BUILD.gn b/services/distributeddataservice/service/dumper/BUILD.gn index 72b194d79..813707ad5 100644 --- a/services/distributeddataservice/service/dumper/BUILD.gn +++ b/services/distributeddataservice/service/dumper/BUILD.gn @@ -42,7 +42,7 @@ ohos_source_set("distributeddata_dumper") { deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] external_deps = [ "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/service/kvdb/BUILD.gn b/services/distributeddataservice/service/kvdb/BUILD.gn index 874dc361c..011ace89b 100644 --- a/services/distributeddataservice/service/kvdb/BUILD.gn +++ b/services/distributeddataservice/service/kvdb/BUILD.gn @@ -78,7 +78,7 @@ ohos_source_set("distributeddata_kvdb") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", "kv_store:kvdb_inner_lite", diff --git a/services/distributeddataservice/service/matrix/BUILD.gn b/services/distributeddataservice/service/matrix/BUILD.gn index e7f2d0a63..fc0110efb 100755 --- a/services/distributeddataservice/service/matrix/BUILD.gn +++ b/services/distributeddataservice/service/matrix/BUILD.gn @@ -58,7 +58,7 @@ ohos_source_set("distributeddata_matrix") { external_deps = [ "device_manager:devicemanagersdk", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", ] subsystem_name = "distributeddatamgr" diff --git a/services/distributeddataservice/service/permission/BUILD.gn b/services/distributeddataservice/service/permission/BUILD.gn index 2795bda98..3eba0a70f 100644 --- a/services/distributeddataservice/service/permission/BUILD.gn +++ b/services/distributeddataservice/service/permission/BUILD.gn @@ -62,7 +62,7 @@ ohos_source_set("distributeddata_permit") { "device_auth:deviceauth_sdk", "device_manager:devicemanagersdk", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index a5f539cc5..d08cf688d 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -80,7 +80,7 @@ ohos_unittest("CloudDataTest") { "c_utils:utils", "device_manager:devicemanagersdk", "hicollie:libhicollie", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", @@ -132,7 +132,7 @@ ohos_unittest("CloudServiceImplTest") { "c_utils:utils", "device_manager:devicemanagersdk", "hicollie:libhicollie", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", @@ -431,7 +431,7 @@ ohos_unittest("KVDBGeneralStoreAbnormalTest") { "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", @@ -629,7 +629,7 @@ ohos_unittest("ObjectAssetLoaderTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", ] @@ -668,7 +668,7 @@ ohos_unittest("ObjectAssetMachineTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", ] @@ -766,7 +766,7 @@ ohos_unittest("ObjectManagerTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", @@ -809,7 +809,7 @@ ohos_unittest("ObjectSnapshotTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "relational_store:native_rdb", ] @@ -854,7 +854,7 @@ ohos_unittest("MetaDataTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", @@ -1030,7 +1030,7 @@ ohos_unittest("DataShareServiceImplTest") { "huks:libhukssdk", "init:libbegetutil", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", "kv_store:distributeddb", @@ -1180,7 +1180,7 @@ ohos_unittest("UdmfServiceImplTest") { "device_manager:devicemanagersdk", "googletest:gtest_main", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", "relational_store:native_rdb", @@ -1549,7 +1549,7 @@ ohos_unittest("PermitDelegateMockTest") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddb", ] @@ -1651,7 +1651,7 @@ ohos_unittest("QueryHelperUnitTest") { "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", "kv_store:kvdb_inner_lite", @@ -1681,7 +1681,7 @@ ohos_unittest("AuthDelegateMockTest") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", ] @@ -1732,7 +1732,7 @@ ohos_unittest("UpgradeMockTest") { "googletest:gtest_main", "hilog:libhilog", "huks:libhukssdk", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", ] @@ -1780,7 +1780,7 @@ ohos_unittest("UserDelegateMockTest") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:datamgr_common", "kv_store:distributeddb", ] diff --git a/services/distributeddataservice/service/test/data_share_profile_config_test.cpp b/services/distributeddataservice/service/test/data_share_profile_config_test.cpp index 55f5a31b9..c7d227241 100644 --- a/services/distributeddataservice/service/test/data_share_profile_config_test.cpp +++ b/services/distributeddataservice/service/test/data_share_profile_config_test.cpp @@ -113,7 +113,9 @@ HWTEST_F(DataShareProfileConfigTest, ProfileInfo001, TestSize.Level1) Serializable::json node; info.Marshal(node); - EXPECT_EQ(node["isSilentProxyEnable"], true); + bool ret; + node["isSilentProxyEnable"].get_to(ret); + EXPECT_EQ(ret, true); EXPECT_EQ(node["path"], "storeName/tableName"); EXPECT_EQ(node["scope"], "module"); EXPECT_EQ(node["type"], "rdb"); diff --git a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn index f328df573..1fa88f886 100755 --- a/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/objectservicestub_fuzzer/BUILD.gn @@ -103,7 +103,7 @@ ohos_fuzztest("ObjectServiceStubFuzzTest") { "hisysevent:libhisysevent", "huks:libhukssdk", "ipc:ipc_core", - "json:nlohmann_json_static", + "cJSON:cjson", "kv_store:distributeddata_inner", "kv_store:distributeddata_mgr", "kv_store:distributeddb", -- Gitee From 2727a7220b0a977513c62631e91563e1d424e3bc Mon Sep 17 00:00:00 2001 From: bjd Date: Thu, 22 May 2025 20:02:52 +0800 Subject: [PATCH 2/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/serializable/serializable.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index a09c8de82..5261f0cc0 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -551,7 +551,6 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](size_t index) } if (!is_array()) { ZLOGE("cannot use operator[]."); - //throw std::invalid_argument("cannot use operator[]"); } int size = cJSON_GetArraySize(json_); auto len = children_.size(); @@ -562,7 +561,6 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](size_t index) } if (index > len) { ZLOGE("cannot use operator[]."); - //throw std::invalid_argument("cannot use operator[]"); } if (index == len) { auto item = cJSON_GetArrayItem(json_, len); @@ -575,12 +573,10 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator[](size_t index) c { if (!is_array()) { ZLOGE("invalid args."); - //throw std::invalid_argument("invalid args"); } int size = cJSON_GetArraySize(json_); if (index >= size) { ZLOGE("invalid args."); - //throw std::out_of_range("invalid args"); } auto len = children_.size(); while (len < size) { @@ -813,7 +809,6 @@ Serializable::iterator Serializable::JSONWrapper::begin() const { if (json_ == nullptr || (!is_array() && !is_object())) { ZLOGE("not support."); - //throw std::logic_error("not support"); } auto size = cJSON_GetArraySize(json_); auto len = children_.size(); @@ -831,7 +826,6 @@ Serializable::iterator Serializable::JSONWrapper::end() const { if (json_ == nullptr || json_->child == nullptr || (!is_array() && !is_object())) { ZLOGE("not support."); - //throw std::logic_error("not support"); } auto size = cJSON_GetArraySize(json_); auto len = children_.size(); -- Gitee From 58edad83023727670d63a2e8a9ac0f72d89b11a5 Mon Sep 17 00:00:00 2001 From: bjd Date: Thu, 22 May 2025 20:18:55 +0800 Subject: [PATCH 3/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/include/serializable/serializable.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index df04dfa11..050b160ab 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -107,6 +107,7 @@ public: void AddToRoot(); JSONWrapper(const JSONWrapper& jsonWrapper) = delete; JSONWrapper &operator=(const JSONWrapper &jsonWrapper) = delete; + bool ReplaceNode(cJSON *node); cJSON *json_ = nullptr; cJSON *root_ = nullptr; std::string key_; -- Gitee From 70cac17775118b2bf21ef14e10147619138c8b91 Mon Sep 17 00:00:00 2001 From: bjd Date: Thu, 22 May 2025 21:27:05 +0800 Subject: [PATCH 4/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../distributeddataservice/service/test/config_factory_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/service/test/config_factory_test.cpp b/services/distributeddataservice/service/test/config_factory_test.cpp index fde209259..4077c2acd 100644 --- a/services/distributeddataservice/service/test/config_factory_test.cpp +++ b/services/distributeddataservice/service/test/config_factory_test.cpp @@ -60,7 +60,7 @@ HWTEST_F(ConfigFactoryTest, ComponentConfig, TestSize.Level0) ASSERT_EQ(config.lib, "libconfigdemo.z.so"); ASSERT_EQ(config.constructor, ""); ASSERT_EQ(config.destructor, ""); - ASSERT_EQ(config.params, "{\"count\":1,\"key\":\"value\"}"); + ASSERT_EQ(config.params, "{\"key\":\"value\",\"count\":1}"); } /** -- Gitee From 67706b2263f129eec070278c2cfe93038758df28 Mon Sep 17 00:00:00 2001 From: bjd Date: Sat, 24 May 2025 18:33:53 +0800 Subject: [PATCH 5/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/serializable/serializable.cpp | 106 ++++++++++-------- 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index 5261f0cc0..e177ad518 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -247,6 +247,7 @@ const Serializable::json &Serializable::GetSubNode(const json &node, const std:: Serializable::JSONWrapper::JSONWrapper() : root_(nullptr) { json_ = cJSON_CreateNull(); + needDel_ = true; } Serializable::JSONWrapper::JSONWrapper(cJSON *json, cJSON *root, const std::string &key) @@ -256,25 +257,11 @@ Serializable::JSONWrapper::JSONWrapper(cJSON *json, cJSON *root, const std::stri Serializable::JSONWrapper::JSONWrapper(const std::string &jsonStr) : root_(nullptr), needDel_(true) { - if (json_ != nullptr) { - cJSON_Delete(json_); - children_.clear(); - } json_ = cJSON_Parse(jsonStr.c_str()); } Serializable::JSONWrapper::JSONWrapper(JSONWrapper &&jsonWrapper) { - if (this == &jsonWrapper) { - return; - } - if (json_ != nullptr) { - if (root_ == nullptr) { - cJSON_Delete(json_); - } - json_ = nullptr; - root_ = nullptr; - } json_ = std::move(jsonWrapper.json_); jsonWrapper.json_ = nullptr; root_ = jsonWrapper.root_; @@ -291,6 +278,9 @@ Serializable::JSONWrapper::operator std::string() const Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(JSONWrapper &&jsonWrapper) { + if (this == &jsonWrapper) { + return *this; + } if (root_ == nullptr) { cJSON_Delete(json_); json_ = nullptr; @@ -301,6 +291,7 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(JSONWrapper &&js jsonWrapper.root_ = nullptr; key_ = std::move(jsonWrapper.key_); children_ = std::move(jsonWrapper.children_); + needDel_ = jsonWrapper.needDel_; return *this; } @@ -360,17 +351,27 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(int32_t value) cJSON_Delete(json_); json_ = nullptr; } - if (json_ != nullptr) { - if (cJSON_IsNumber(json_)) { - cJSON_SetNumberValue(json_, value); + if (json_ == nullptr) { + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; } + AddToRoot(); + } + if (json_ == nullptr) { return *this; } - json_ = cJSON_CreateNumber(value); - if (json_ == nullptr || root_ == nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); return *this; } - AddToRoot(); + cJSON *node = cJSON_CreateNumber(value); + if (node == nullptr) { + return *this; + } + if (!ReplaceNode(node)) { + cJSON_Delete(node); + } return *this; } @@ -386,17 +387,27 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(int64_t value) cJSON_Delete(json_); json_ = nullptr; } - if (json_ != nullptr) { - if (cJSON_IsNumber(json_)) { - cJSON_SetNumberValue(json_, value); + if (json_ == nullptr) { + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; } + AddToRoot(); + } + if (json_ == nullptr) { return *this; } - json_ = cJSON_CreateNumber(value); - if (json_ == nullptr || root_ == nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); return *this; } - AddToRoot(); + cJSON *node = cJSON_CreateNumber(value); + if (node == nullptr) { + return *this; + } + if (!ReplaceNode(node)) { + cJSON_Delete(node); + } return *this; } @@ -412,17 +423,27 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(double value) cJSON_Delete(json_); json_ = nullptr; } - if (json_ != nullptr) { - if (cJSON_IsNumber(json_)) { - cJSON_SetNumberValue(json_, value); + if (json_ == nullptr) { + json_ = cJSON_CreateNumber(value); + if (json_ == nullptr || root_ == nullptr) { + return *this; } + AddToRoot(); + } + if (json_ == nullptr) { return *this; } - json_ = cJSON_CreateNumber(value); - if (json_ == nullptr || root_ == nullptr) { + if (cJSON_IsNumber(json_)) { + cJSON_SetNumberValue(json_, value); return *this; } - AddToRoot(); + cJSON *node = cJSON_CreateNumber(value); + if (node == nullptr) { + return *this; + } + if (!ReplaceNode(node)) { + cJSON_Delete(node); + } return *this; } @@ -475,6 +496,7 @@ Serializable::JSONWrapper &Serializable::JSONWrapper::operator=(const std::vecto auto node = cJSON_CreateNumber(value[i]); if (!node || !cJSON_AddItemToArray(json_, node)) { cJSON_Delete(json_); + cJSON_Delete(node); json_ = nullptr; children_.clear(); return *this; @@ -505,6 +527,7 @@ bool Serializable::JSONWrapper::ReplaceNode(cJSON *node) if (success) { json_ = node; + children_.clear(); } return success; } @@ -750,18 +773,9 @@ bool Serializable::JSONWrapper::empty() const if (cJSON_IsNull(json_)) { return true; } - if (cJSON_IsArray(json_)) { + if (cJSON_IsArray(json_) || cJSON_IsObject(json_)) { return cJSON_GetArraySize(json_) == 0; } - if (cJSON_IsObject(json_)) { - int size = 0; - cJSON *child = json_->child; - while (child) { - size++; - child = child->next; - } - return size == 0; - } return false; } @@ -993,7 +1007,9 @@ Serializable::JSONWrapper& Serializable::JSONWrapper::operator=(const std::map Date: Sat, 24 May 2025 18:43:12 +0800 Subject: [PATCH 6/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/include/cloud/subscription.h | 6 ------ .../framework/test/subscription_test.cpp | 16 ++++++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/services/distributeddataservice/framework/include/cloud/subscription.h b/services/distributeddataservice/framework/include/cloud/subscription.h index 29a8601c1..4054a91f3 100644 --- a/services/distributeddataservice/framework/include/cloud/subscription.h +++ b/services/distributeddataservice/framework/include/cloud/subscription.h @@ -28,9 +28,6 @@ struct API_EXPORT Subscription final : public Serializable { std::map relations; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; - void SetRelations(Serializable::json &node, const std::map &values) { - Serializable::SetValue(node, values); - } }; bool Marshal(json &node) const; @@ -41,9 +38,6 @@ struct API_EXPORT Subscription final : public Serializable { static std::string GetKey(int32_t userId); static std::string GetRelationKey(int32_t userId, const std::string &bundleName); static std::string GetPrefix(const std::initializer_list &fields); - void SetSubscriptions(Serializable::json &node, const std::map &values) { - Serializable::SetValue(node, values); - } private: static constexpr const char *PREFIX = "CLOUD_SUBSCRIPTION"; diff --git a/services/distributeddataservice/framework/test/subscription_test.cpp b/services/distributeddataservice/framework/test/subscription_test.cpp index 2b70c8a07..f729d878e 100644 --- a/services/distributeddataservice/framework/test/subscription_test.cpp +++ b/services/distributeddataservice/framework/test/subscription_test.cpp @@ -55,11 +55,13 @@ HWTEST_F(SubscriptionTest, RelationMarshal, TestSize.Level1) */ HWTEST_F(SubscriptionTest, RelationUnmarshal, TestSize.Level1) { + Subscription::Relation relationOri; + relationOri.id = "testId"; + relationOri.bundleName = "testBundleName"; + relationOri.relations = testRelation; Subscription::json node; - node["id"] = "testId"; - node["bundleName"] = "testBundleName"; + relationOri.Marshal(node); Subscription::Relation relation; - relation.SetRelations(node["relations"], testRelation); relation.Unmarshal(node); ASSERT_EQ(relation.id, "testId"); ASSERT_EQ(relation.bundleName, "testBundleName"); @@ -91,11 +93,13 @@ HWTEST_F(SubscriptionTest, Marshal, TestSize.Level1) */ HWTEST_F(SubscriptionTest, Unmarshal, TestSize.Level1) { + Subscription subscriptionOri; + subscriptionOri.userId = 100; + subscriptionOri.id = "testId"; + subscriptionOri.expiresTime = testExpiresTime; Subscription::json node; - node["userId"] = 100; - node["id"] = "testId"; + subscriptionOri.Marshal(node); Subscription subscription; - subscription.SetSubscriptions(node["expiresTime"], testExpiresTime); subscription.Unmarshal(node); ASSERT_EQ(subscription.userId, 100); ASSERT_EQ(subscription.id, "testId"); -- Gitee From 7bfd2078371170d58a5eafa5083c040a52a89861 Mon Sep 17 00:00:00 2001 From: bjd Date: Sat, 24 May 2025 18:53:14 +0800 Subject: [PATCH 7/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/serializable/serializable.cpp | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/framework/serializable/serializable.cpp b/services/distributeddataservice/framework/serializable/serializable.cpp index e177ad518..5cb4c9265 100644 --- a/services/distributeddataservice/framework/serializable/serializable.cpp +++ b/services/distributeddataservice/framework/serializable/serializable.cpp @@ -800,15 +800,7 @@ std::string Serializable::JSONWrapper::dump() const Serializable::iterator Serializable::JSONWrapper::find(const std::string &key) const { - auto size = cJSON_GetArraySize(json_); - auto len = children_.size(); - if (len != size) { - children_.clear(); - for (int i = 0; i < size; i++) { - auto item = cJSON_GetArrayItem(json_, i); - children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); - } - } + SyncChildren(); auto it = children_.begin(); while (it != children_.end()) { if ((*it)->key_ == key) { @@ -824,15 +816,7 @@ Serializable::iterator Serializable::JSONWrapper::begin() const if (json_ == nullptr || (!is_array() && !is_object())) { ZLOGE("not support."); } - auto size = cJSON_GetArraySize(json_); - auto len = children_.size(); - if (len != size) { - children_.clear(); - for (int i = 0; i < size; i++) { - auto item = cJSON_GetArrayItem(json_, i); - children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); - } - } + SyncChildren(); return children_.begin(); } @@ -841,6 +825,12 @@ Serializable::iterator Serializable::JSONWrapper::end() const if (json_ == nullptr || json_->child == nullptr || (!is_array() && !is_object())) { ZLOGE("not support."); } + SyncChildren(); + return children_.end(); +} + +void Serializable::JSONWrapper::SyncChildren() const +{ auto size = cJSON_GetArraySize(json_); auto len = children_.size(); if (len != size) { @@ -850,7 +840,6 @@ Serializable::iterator Serializable::JSONWrapper::end() const children_.push_back(std::make_shared(item, json_, is_object() ? item->string:"")); } } - return children_.end(); } Serializable::JSONWrapper::~JSONWrapper() -- Gitee From d2d6d8e9c82aa97bf8ef48533ca68e8cf68fc1ea Mon Sep 17 00:00:00 2001 From: bjd Date: Sat, 24 May 2025 19:11:52 +0800 Subject: [PATCH 8/8] =?UTF-8?q?cjson=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- .../framework/include/serializable/serializable.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributeddataservice/framework/include/serializable/serializable.h b/services/distributeddataservice/framework/include/serializable/serializable.h index 050b160ab..2fb72495a 100644 --- a/services/distributeddataservice/framework/include/serializable/serializable.h +++ b/services/distributeddataservice/framework/include/serializable/serializable.h @@ -108,6 +108,7 @@ public: JSONWrapper(const JSONWrapper& jsonWrapper) = delete; JSONWrapper &operator=(const JSONWrapper &jsonWrapper) = delete; bool ReplaceNode(cJSON *node); + void SyncChildren() const; cJSON *json_ = nullptr; cJSON *root_ = nullptr; std::string key_; -- Gitee