diff --git a/README.md b/README.md index c73cf1f7..eebf86df 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,7 @@ std::cout << j_string << " == " << serialized_string << std::endl; [`.dump()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5adea76fedba9898d404fef8598aa663.html#a5adea76fedba9898d404fef8598aa663) always returns the serialized value, and [`.get()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a16f9445f7629f634221a42b967cdcd43.html#a16f9445f7629f634221a42b967cdcd43) returns the originally stored string value. -Note the library only supports UTF-8. When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5adea76fedba9898d404fef8598aa663.html#a5adea76fedba9898d404fef8598aa663) may throw an exception. +Note the library only supports UTF-8. When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5adea76fedba9898d404fef8598aa663.html#a5adea76fedba9898d404fef8598aa663) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. #### To/from streams (e.g. files, string streams) diff --git a/doc/examples/dump.cpp b/doc/examples/dump.cpp index 4deb64a1..eb2d71f0 100644 --- a/doc/examples/dump.cpp +++ b/doc/examples/dump.cpp @@ -30,7 +30,7 @@ int main() << j_string.dump(-1, ' ', true) << '\n'; // create JSON value with invalid UTF-8 byte sequence - json j_invalid = "\xF0\xA4\xAD\xC0"; + json j_invalid = "ä\xA9ü"; try { std::cout << j_invalid.dump() << std::endl; @@ -39,4 +39,10 @@ int main() { std::cout << e.what() << std::endl; } + + 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) + << '\n'; } diff --git a/doc/examples/dump.link b/doc/examples/dump.link index c072c0d9..bfdb31bb 100644 --- a/doc/examples/dump.link +++ b/doc/examples/dump.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/dump.output b/doc/examples/dump.output index c94eeb02..43009fe6 100644 --- a/doc/examples/dump.output +++ b/doc/examples/dump.output @@ -50,4 +50,6 @@ arrays: strings: "Hellö 😀!" "Hell\u00f6 \ud83d\ude00!" -[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0xC0 +[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9 +string with replaced invalid characters: "ä�ü" +string with ignored invalid characters: "äü"