mirror of
https://github.com/nlohmann/json.git
synced 2026-10-05 06:00:29 +00:00
* Keep the serializer conversion for objects whose keys cannot be converted #5591 added a test converting nlohmann::json into a basic_json whose string type cannot be constructed from std::string. That instantiates convert_iteratively(), whose members.emplace_back(next.key(), ...) needs exactly that key conversion, and broke the build of unit-alt-string. Dispatch on the key's constructibility and leave such conversions to the serializers, as the levels above the nesting bound already do (#3425). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the remaining CI failures on develop - unit-wstring: with a 16-bit wchar_t (Windows), a lone surrogate is reported as the ill-formed byte 0xFF since #5704; the std::wstring expectations still had the previous <U+0000>. - ci_single_binaries: json_literals.hpp (#5610) and json.hpp include each other on purpose, and IWYU, not following the cycle, asks to replace json.hpp with json_fwd.hpp. Report its findings without failing the build, as already done for json.hpp. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the library warnings and noexcept specifications from the merged PRs - binary_reader: rename the error_handler constructor parameter, which shadowed the member (-Wshadow, -Wshadow-field-in-constructor; #5746) - basic_json(copy_construct_tag, ...): declare it noexcept when copying the base class is (GCC 16 -Wnoexcept; #5690) - the scalar-on-left legacy comparison operators: noexcept only when converting the scalar is, like their member counterparts (#5682, #5751) - compare_leaves: use std::is_eq/is_lt/is_gt instead of comparing a std::partial_ordering with 0 (-Wzero-as-null-pointer-constant; #5686) - serializer: silence MSVC C4127 for the EnsureAscii template parameter (#5741, #5746) - clang-tidy: return the sanitized reference in binary_writer, take the key of ordered_map::find_impl by const reference (#5727), and mark the switches over parse_array_index (#5728) - ordered_map: keep <memory> for std::allocator (IWYU) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Split unit-conversions.cpp so MinGW can link it clang 18 with the MinGW linker failed to link test-conversions_cpp17 ("relocation truncated to fit: IMAGE_REL_AMD64_REL32"). As windows.yml recommends, keep the objects small by splitting the test file. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the tests added by the merged PRs for all CI configurations - discard the results of dump() and from_*() in CHECK_THROWS with utils::ignore_return_value (GCC -Werror=unused-result) - give unit-bson's huge_string_t a default constructor (MSVC C2512, GCC 5, clang 3.5) - unit-disabled_exceptions: use the literals namespace when the global UDLs are off (ci_test_noglobaludls; #5700) - unit-binary_utf8_strict: expect the JSON pointer prefix with JSON_DIAGNOSTICS (#5741) - skip the tests that rely on exceptions under JSON_NOEXCEPTION (#5678, #5732) - clang-tidy and clang -Werror: static test data, CAPTURE(...);, const-correctness, use-after-move alias, unused conversion operator, a missing <iterator> include Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Title the macro examples and add JSON_STRICT_BINARY_UTF8 to the docset The documentation style check requires "Example: ..." titles on pages with several examples (#5741, #5591) and a docset entry for every macro page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Regenerate BUILD.bazel and nlohmann_json.natvis Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5746 added detail/output/error_handler.hpp and #5741 the json_abi_sbu8 ABI tag. * Install libidn11 for the CMake 3.5.0 binary in ci_cmake_flags Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5733 moved ci_cmake_options from ubuntu:focal to ubuntu:24.04, which no longer ships libidn.so.11; the CMake 3.5.0 release binary links against it, so every ci_cmake_flags run has failed since. Install focal's libidn11 package for that matrix entry only. * Suppress Infer's false STACK_VARIABLE_ADDRESS_ESCAPE in get_impl get_impl() returns its local by value. A test added by the merged PRs instantiates it with a type Infer misreads, so ci_infer reported the 2021 code for the first time. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
88 lines
3.1 KiB
C++
88 lines
3.1 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "doctest_compatibility.h"
|
|
|
|
// disable -Wnoexcept as exceptions are switched off for this test suite
|
|
DOCTEST_GCC_SUPPRESS_WARNING_PUSH
|
|
DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")
|
|
|
|
#include <nlohmann/json.hpp>
|
|
using json = nlohmann::json;
|
|
#ifdef JSON_TEST_NO_GLOBAL_UDLS
|
|
using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
|
|
#endif
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
// for #2824
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json, nlohmann::detail::string_input_adapter_type>
|
|
{
|
|
public:
|
|
explicit sax_no_exception(json& j) : nlohmann::detail::json_sax_dom_parser<json, nlohmann::detail::string_input_adapter_type>(j, false) {}
|
|
|
|
static bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& ex)
|
|
{
|
|
error_string = new std::string(ex.what()); // NOLINT(cppcoreguidelines-owning-memory)
|
|
return false;
|
|
}
|
|
|
|
static std::string* error_string;
|
|
};
|
|
|
|
std::string* sax_no_exception::error_string = nullptr;
|
|
|
|
TEST_CASE("Tests with disabled exceptions")
|
|
{
|
|
SECTION("issue #2824 - encoding of json::exception::what()")
|
|
{
|
|
json j;
|
|
sax_no_exception sax(j);
|
|
|
|
CHECK (!json::sax_parse("xyz", &sax));
|
|
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'");
|
|
delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory)
|
|
}
|
|
|
|
SECTION("issue #5672 - value(json_pointer, default) must not abort for array tokens that are not a valid index")
|
|
{
|
|
const json j = {1, 2, 3};
|
|
|
|
// a syntactically valid index that is out of range for this array
|
|
CHECK(j.value("/7"_json_pointer, 42) == 42);
|
|
// a reference token that is not a number at all
|
|
CHECK(j.value("/1a"_json_pointer, 42) == 42);
|
|
// the empty reference token (JSON pointer "/")
|
|
CHECK(j.value("/"_json_pointer, 42) == 42);
|
|
// an index whose magnitude does not fit into size_type
|
|
CHECK(j.value("/99999999999999999999999"_json_pointer, 42) == 42);
|
|
CHECK(j.value("/18446744073709551615"_json_pointer, 42) == 42);
|
|
}
|
|
|
|
SECTION("growing an ordered_json object")
|
|
{
|
|
auto j = nlohmann::ordered_json::object();
|
|
for (int i = 0; i < 100; ++i)
|
|
{
|
|
j[std::to_string(i)] = {{"nested", i}};
|
|
}
|
|
|
|
CHECK(j.size() == 100);
|
|
int i = 0;
|
|
for (const auto& element : j.items())
|
|
{
|
|
CHECK(element.key() == std::to_string(i));
|
|
CHECK(element.value()["nested"] == i);
|
|
++i;
|
|
}
|
|
}
|
|
}
|
|
|
|
DOCTEST_GCC_SUPPRESS_WARNING_POP
|