1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-28 12:02:00 +03:00

💥 throwing an exception in case dump encounters a non-UTF-8 string #838

We had a lot of issues with failing roundtrips (i.e., parse errors from serializations) in case string were stored in the library that were not UTF-8 encoded. This PR adds an exception in this case.
This commit is contained in:
Niels Lohmann
2017-12-11 22:38:05 +01:00
parent 383743c6c0
commit 569d275f65
6 changed files with 116 additions and 15 deletions

View File

@ -32,7 +32,7 @@ SOFTWARE.
#include "json.hpp"
using nlohmann::json;
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii = false);
void check_escaped(const char* original, const char* escaped = "", const bool ensure_ascii = false);
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
{
std::stringstream ss;
@ -99,7 +99,12 @@ TEST_CASE("convenience functions")
check_escaped("\x1f", "\\u001f");
// invalid UTF-8 characters
check_escaped("ä\xA9ü", "ä\xA9ü");
check_escaped("ä\xA9ü", "\\u00e4\xA9\\u00fc", true);
CHECK_THROWS_AS(check_escaped("ä\xA9ü"), json::type_error);
CHECK_THROWS_WITH(check_escaped("ä\xA9ü"),
"[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9");
CHECK_THROWS_AS(check_escaped("\xC2"), json::type_error);
CHECK_THROWS_WITH(check_escaped("\xC2"),
"[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2");
}
}