diff --git a/lib/base/json.cpp b/lib/base/json.cpp index e2749bbde..00d114731 100644 --- a/lib/base/json.cpp +++ b/lib/base/json.cpp @@ -5,6 +5,7 @@ #include "base/dictionary.hpp" #include "base/namespace.hpp" #include "base/objectlock.hpp" +#include "base/utility.hpp" #include #include #include @@ -56,7 +57,7 @@ void JsonEncoder::Encode(const Value& value, boost::asio::yield_context* yc) Write(value.ToBool() ? "true" : "false"); break; case ValueString: - EncodeNlohmannJson(Utility::ValidateUTF8(value.Get())); + EncodeNlohmannJson(value.Get()); break; case ValueNumber: EncodeNumber(value.Get()); @@ -76,7 +77,7 @@ void JsonEncoder::Encode(const Value& value, boost::asio::yield_context* yc) EncodeValueGenerator(gen, yc); } else { // Some other non-serializable object type! - EncodeNlohmannJson(Utility::ValidateUTF8(obj->ToString())); + EncodeNlohmannJson(obj->ToString()); } break; } @@ -166,7 +167,7 @@ void JsonEncoder::EncodeObject(const Iterable& container, const ValExtractor& ex WriteSeparatorAndIndentStrIfNeeded(!isEmpty); isEmpty = false; - EncodeNlohmannJson(Utility::ValidateUTF8(key)); + EncodeNlohmannJson(key); Write(m_Pretty ? ": " : ":"); Encode(extractor(val), yc); @@ -179,13 +180,15 @@ void JsonEncoder::EncodeObject(const Iterable& container, const ValExtractor& ex * Dumps a nlohmann::json object to the output stream using the serializer. * * This function uses the @c nlohmann::detail::serializer to dump the provided @c nlohmann::json - * object to the output stream managed by the @c JsonEncoder. + * object to the output stream managed by the @c JsonEncoder. Strings will be properly escaped, and + * if any invalid UTF-8 sequences are encountered, it will replace them with the Unicode replacement + * character (U+FFFD). * * @param json The nlohmann::json object to encode. */ void JsonEncoder::EncodeNlohmannJson(const nlohmann::json& json) const { - nlohmann::detail::serializer s(m_Writer, ' ', nlohmann::json::error_handler_t::strict); + nlohmann::detail::serializer s(m_Writer, ' ', nlohmann::json::error_handler_t::replace); s.dump(json, m_Pretty, true, 0, 0); } diff --git a/lib/base/json.hpp b/lib/base/json.hpp index 464a926f5..238c9bc53 100644 --- a/lib/base/json.hpp +++ b/lib/base/json.hpp @@ -6,7 +6,6 @@ #include "base/i2-base.hpp" #include "base/array.hpp" #include "base/generator.hpp" -#include "base/utility.hpp" #include #include @@ -58,8 +57,7 @@ class Value; * The JSON encoder generates most of the low level JSON tokens, but it still relies on the already existing * @c nlohmann::detail::serializer<> class to dump numbers and ASCII validated JSON strings. This means that the * encoder doesn't perform any kind of JSON validation or escaping on its own, but simply delegates all this kind - * of work to serializer<>. However, Strings are UTF-8 validated beforehand using the @c Utility::ValidateUTF8() - * function and only the validated (copy of the original) String is passed to the serializer. + * of work to serializer<>. * * The generated JSON can be either prettified or compact, depending on your needs. The prettified JSON object * is indented with 4 spaces and grows linearly with the depth of the object tree.