mirror of
https://github.com/nlohmann/json.git
synced 2026-10-14 10:17:12 +00:00
Export client-facing headers from json.hpp for IWYU / include-cleaner, and check clients in CI (#5813)
* Mark json_fwd.hpp as exported by json.hpp for IWYU / include-cleaner json_fwd.hpp is the only header declaring the nlohmann::json alias, so include-what-you-use and clang-tidy's misc-include-cleaner attributed the name to it and told users of <nlohmann/json.hpp> to include json_fwd.hpp as well (or instead, which breaks compilation). Fixes #5812 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Export the remaining client-facing headers from json.hpp and check clients with IWYU Besides json_fwd.hpp, include analyzers also told clients of <nlohmann/json.hpp> to include detail/json_ref.hpp (brace initialization), json_literals.hpp (_json, _json_pointer), adl_serializer.hpp, detail/macro_scope.hpp (NLOHMANN_DEFINE_*) and detail/abi_macros.hpp. Mark those includes as exported, too. ci_single_binaries only checked each header on its own, so it could not notice this. It now also runs IWYU and clang-tidy's misc-include-cleaner on small client programs in tests/iwyu_consumer, which fail on develop. With the exports, json_literals.hpp no longer gets the json_fwd.hpp suggestion that kept it out of the IWYU gate, so it is gated again. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
aa9620fb15
commit
e839bbd1c0
6 files changed
+152
-19
No files matched your search
@@ -0,0 +1,62 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | 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
|
||||
|
||||
// See values.cpp: the conversion macros and nlohmann::adl_serializer must be available through
|
||||
// <nlohmann/json.hpp> alone. NLOHMANN_JSON_SERIALIZE_ENUM (from the same header as
|
||||
// NLOHMANN_DEFINE_TYPE_INTRUSIVE) is left out: IWYU attributes the basic_json comparison in its
|
||||
// expansion to an unrelated standard header, the ambiguity described for json.hpp in cmake/ci.cmake.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace ns
|
||||
{
|
||||
struct person
|
||||
{
|
||||
std::string name;
|
||||
int age;
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person, name, age)
|
||||
};
|
||||
|
||||
struct legacy
|
||||
{
|
||||
int value;
|
||||
};
|
||||
} // namespace ns
|
||||
|
||||
namespace nlohmann
|
||||
{
|
||||
template<>
|
||||
struct adl_serializer<ns::legacy>
|
||||
{
|
||||
static void to_json(json& j, const ns::legacy& l)
|
||||
{
|
||||
j = l.value;
|
||||
}
|
||||
|
||||
static void from_json(const json& j, ns::legacy& l)
|
||||
{
|
||||
l.value = j.get<int>();
|
||||
}
|
||||
};
|
||||
} // namespace nlohmann
|
||||
|
||||
int main()
|
||||
{
|
||||
ns::person ada;
|
||||
ada.name = "Ada";
|
||||
ada.age = 36;
|
||||
ns::legacy one;
|
||||
one.value = 1;
|
||||
|
||||
const nlohmann::json j = ada;
|
||||
const auto p = j.get<ns::person>();
|
||||
const auto l = nlohmann::json(one).get<ns::legacy>();
|
||||
return p.age - 36 + l.value - 1;
|
||||
}
|
||||
Reference in new issue
Block a user