🚧 first implementation for keep

This commit is contained in:
Niels Lohmann
2024-12-20 15:40:20 +01:00
parent a27a5b5442
commit a2d828c204
10 changed files with 54 additions and 25 deletions

View File

@@ -44,7 +44,8 @@ enum class error_handler_t
{
strict, ///< throw a type_error exception in case of invalid UTF-8
replace, ///< replace invalid UTF-8 sequences with U+FFFD
ignore ///< ignore invalid UTF-8 sequences
ignore, ///< ignore invalid UTF-8 sequences
keep ///< keep invalid UTF-8 sequences
};
template<typename BasicJsonType>
@@ -398,6 +399,13 @@ class serializer
std::size_t bytes_after_last_accept = 0;
std::size_t undumped_chars = 0;
// copy string as-is if error handler is set to keep
if (error_handler == error_handler_t::keep)
{
o->write_characters(s.data(), s.size());
return;
}
for (std::size_t i = 0; i < s.size(); ++i)
{
const auto byte = static_cast<std::uint8_t>(s[i]);
@@ -529,12 +537,6 @@ class serializer
// thus removing/ignoring the invalid characters
bytes = bytes_after_last_accept;
// fix for #4552
if (error_handler == error_handler_t::ignore)
{
bytes += undumped_chars;
}
if (error_handler == error_handler_t::replace)
{
// add a replacement character