xref: /third_party/json/docs/examples/error_handler_t.cpp
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/json/docs/examples/
1c5f01b2fSopenharmony_ci#include <iostream>
2c5f01b2fSopenharmony_ci#include <nlohmann/json.hpp>
3c5f01b2fSopenharmony_ci
4c5f01b2fSopenharmony_ciusing json = nlohmann::json;
5c5f01b2fSopenharmony_ci
6c5f01b2fSopenharmony_ciint main()
7c5f01b2fSopenharmony_ci{
8c5f01b2fSopenharmony_ci    // create JSON value with invalid UTF-8 byte sequence
9c5f01b2fSopenharmony_ci    json j_invalid = "ä\xA9ü";
10c5f01b2fSopenharmony_ci    try
11c5f01b2fSopenharmony_ci    {
12c5f01b2fSopenharmony_ci        std::cout << j_invalid.dump() << std::endl;
13c5f01b2fSopenharmony_ci    }
14c5f01b2fSopenharmony_ci    catch (json::type_error& e)
15c5f01b2fSopenharmony_ci    {
16c5f01b2fSopenharmony_ci        std::cout << e.what() << std::endl;
17c5f01b2fSopenharmony_ci    }
18c5f01b2fSopenharmony_ci
19c5f01b2fSopenharmony_ci    std::cout << "string with replaced invalid characters: "
20c5f01b2fSopenharmony_ci              << j_invalid.dump(-1, ' ', false, json::error_handler_t::replace)
21c5f01b2fSopenharmony_ci              << "\nstring with ignored invalid characters: "
22c5f01b2fSopenharmony_ci              << j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore)
23c5f01b2fSopenharmony_ci              << '\n';
24c5f01b2fSopenharmony_ci}
25

Indexes created Thu Nov 07 10:32:03 CST 2024