From b5a62bb178639a5adea4e2d1596512edd6083623 Mon Sep 17 00:00:00 2001 From: daiyunlong Date: Thu, 19 Jun 2025 20:47:35 +0800 Subject: [PATCH] =?UTF-8?q?Json=E5=BC=82=E5=B8=B8=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=BF=BD=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: daiyunlong --- common/src/dm_anonymous.cpp | 2 +- json/include/json_object.h | 2 ++ json/src/json_object_cjson.cpp | 11 +++++++++++ json/src/json_object_nlohmannjson.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/common/src/dm_anonymous.cpp b/common/src/dm_anonymous.cpp index 69b50d5cc..985e06677 100644 --- a/common/src/dm_anonymous.cpp +++ b/common/src/dm_anonymous.cpp @@ -400,7 +400,7 @@ bool IsDmCommonNotifyEventValid(DmCommonNotifyEvent dmCommonNotifyEvent) DM_EXPORT std::string SafetyDump(const JsonItemObject &jsonObj) { - return jsonObj.Dump(); + return jsonObj.DumpIgnoreError(); } std::string GetSubStr(const std::string &rawStr, const std::string &separator, int32_t index) diff --git a/json/include/json_object.h b/json/include/json_object.h index 9b91ddf85..b91a00800 100644 --- a/json/include/json_object.h +++ b/json/include/json_object.h @@ -50,6 +50,7 @@ public: const JsonItemObject operator[](const std::string &key) const; std::string DumpFormated() const; std::string Dump() const; + std::string DumpIgnoreError() const; bool Contains(const std::string &key) const; bool IsDiscarded() const; bool PushBack(const std::string &strValue); @@ -124,6 +125,7 @@ protected: JsonItemObject(); void Delete(); std::string Dump(bool formatFlag) const; + std::string Dump(bool formatFlag, bool isIgnoreError) const; void AddItemToArray(JsonItemObject &item); bool InitItem(JsonItemObject &item); bool InitArray(); diff --git a/json/src/json_object_cjson.cpp b/json/src/json_object_cjson.cpp index 76b3a4aeb..33fc92509 100644 --- a/json/src/json_object_cjson.cpp +++ b/json/src/json_object_cjson.cpp @@ -351,6 +351,17 @@ std::string JsonItemObject::Dump() const return Dump(false); } +std::string JsonItemObject::DumpIgnoreError() const +{ + return Dump(false, true); +} + +std::string JsonItemObject::Dump(bool formatFlag, bool isIgnoreError) const +{ + (void) isIgnoreError; + return Dump(formatFlag); +} + std::string JsonItemObject::Dump(bool formatFlag) const { if (item_ == nullptr) { diff --git a/json/src/json_object_nlohmannjson.cpp b/json/src/json_object_nlohmannjson.cpp index a834dfdbd..8f91d8050 100644 --- a/json/src/json_object_nlohmannjson.cpp +++ b/json/src/json_object_nlohmannjson.cpp @@ -286,6 +286,32 @@ std::string JsonItemObject::Dump() const return Dump(false); } +std::string JsonItemObject::DumpIgnoreError() const +{ + return Dump(false, true); +} + +std::string JsonItemObject::Dump(bool formatFlag, bool isIgnoreError) const +{ + if (item_ == nullptr) { + LOGE("item_ is nullptr"); + return ""; + } + int indent = -1; + char indent_char = ' '; + bool ensure_ascii = false; + nlohmann::detail::error_handler_t error_handler = nlohmann::detail::error_handler_t::strict; + if (formatFlag) { + indent = 1; + indent_char = '\t'; + error_handler = nlohmann::detail::error_handler_t::ignore; + } + if (isIgnoreError) { + error_handler = nlohmann::detail::error_handler_t::ignore; + } + return GetJsonPointer(item_)->dump(indent, indent_char, ensure_ascii, error_handler); +} + std::string JsonItemObject::Dump(bool formatFlag) const { if (item_ == nullptr) { -- Gitee