diff --git a/common/src/dm_anonymous.cpp b/common/src/dm_anonymous.cpp index 69b50d5cc8a0cd625b1935df2a6dfb1db7c1dd49..985e06677d132e6c673ad9a8ed6f5c9d7a5cb050 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 9b91ddf85255e8e2754c1575a742e9e7ca2fce56..b91a008000741a955f4160963bb97b01e0e1e13d 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 76b3a4aeb6804272e6e73e00e2a77bdcda461e01..33fc9250956a7ebb050b716de03039989aec4570 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 a834dfdbda6dfa08196af701006b0c91386748a3..f6915a0d452b0343b9b415331ccfc4c03d4e6968 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) {