mirror of
https://github.com/nlohmann/json.git
synced 2026-10-04 21:50:33 +00:00
* Add basic_json::as_base_class and document name conflicts with custom base classes Members of basic_json hide members of a custom base class with the same name, and future releases may add members that hide ones accessible today. Document this in json_base_class_t and add as_base_class() to reach hidden members without spelling out the cast. Also make json_base_class_t a public member type. It was documented since 3.12.0, but declared private, so users could not name it. Supersedes #3899. Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com> Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Add as_base_class to the docset search index New public members get an entry in docs/docset/docSet.sql (as done for to_bon8/from_bon8 in #2998). Without it, the Dash/Zeal docset built from the documentation cannot find basic_json::as_base_class. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Silence clang-tidy for the hidden type_name() in the base class test ci_clang_tidy failed with readability-convert-member-functions-to-static on base_class_with_hidden_members::type_name(). It must stay a non-static member: the test shows that it is hidden by the non-static basic_json::type_name() and reachable through as_base_class(). Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Co-authored-by: Raphael Grimm <1005058+barcode@users.noreply.github.com>
42 lines
944 B
C++
42 lines
944 B
C++
#include <iostream>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
class base_class_with_hidden_members
|
|
{
|
|
public:
|
|
const char* type_name() const noexcept
|
|
{
|
|
return "my_type_name";
|
|
}
|
|
|
|
std::size_t size() const noexcept
|
|
{
|
|
return 42;
|
|
}
|
|
};
|
|
|
|
using json = nlohmann::basic_json <
|
|
std::map,
|
|
std::vector,
|
|
std::string,
|
|
bool,
|
|
std::int64_t,
|
|
std::uint64_t,
|
|
double,
|
|
std::allocator,
|
|
nlohmann::adl_serializer,
|
|
std::vector<std::uint8_t>,
|
|
base_class_with_hidden_members
|
|
>;
|
|
|
|
int main()
|
|
{
|
|
json j = {1, 2, 3};
|
|
|
|
// the members of basic_json hide the members of the base class
|
|
std::cout << j.type_name() << ' ' << j.size() << '\n';
|
|
|
|
// access the hidden members of the base class
|
|
std::cout << j.as_base_class().type_name() << ' ' << j.as_base_class().size() << '\n';
|
|
}
|