Files
json/examples/error_handler_t.cpp
T
2026-10-26 21:24:35 +00:00

26 lines
805 B
C++

#include <iostream>
#include <nlohmann/json.hpp>
using nlohmann::json;
int main()
{
// create JSON value with invalid UTF-8 byte sequence
json j_invalid = "ä\xA9ü";
try
{
std::cout << j_invalid.dump() << '\n';
}
catch (const json::type_error& e)
{
std::cout << e.what() << '\n';
}
std::cout << "string with replaced invalid characters: "
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::replace)
<< "\nstring with ignored invalid characters: "
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore)
<< "\nstring with the invalid byte kept as is (" << j_invalid.dump(-1, ' ', false, json::error_handler_t::keep).size()
<< " bytes, not valid UTF-8 itself)\n";
}