1// __ _____ _____ _____ 2// __| | __| | | | JSON for Modern C++ (supporting code) 3// | | |__ | | | | | | version 3.11.2 4// |_____|_____|_____|_|___| https://github.com/nlohmann/json 5// 6// Copyright (c) 2013-2022 Niels Lohmann <http://nlohmann.me>. 7// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me> 8// SPDX-License-Identifier: MIT 9 10#include "doctest_compatibility.h" 11 12// disable -Wnoexcept as exceptions are switched off for this test suite 13DOCTEST_GCC_SUPPRESS_WARNING_PUSH 14DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept") 15 16#include <nlohmann/json.hpp> 17using json = nlohmann::json; 18 19///////////////////////////////////////////////////////////////////// 20// for #2824 21///////////////////////////////////////////////////////////////////// 22 23class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json> 24{ 25 public: 26 explicit sax_no_exception(json& j) : nlohmann::detail::json_sax_dom_parser<json>(j, false) {} 27 28 static bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& ex) 29 { 30 error_string = new std::string(ex.what()); // NOLINT(cppcoreguidelines-owning-memory) 31 return false; 32 } 33 34 static std::string* error_string; 35}; 36 37std::string* sax_no_exception::error_string = nullptr; 38 39TEST_CASE("Tests with disabled exceptions") 40{ 41 SECTION("issue #2824 - encoding of json::exception::what()") 42 { 43 json j; 44 sax_no_exception sax(j); 45 46 CHECK (!json::sax_parse("xyz", &sax)); 47 CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); 48 delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) 49 } 50} 51 52DOCTEST_GCC_SUPPRESS_WARNING_POP 53