diff --git a/docs/examples/as_base_class.cpp b/docs/examples/as_base_class.cpp new file mode 100644 index 000000000..6eb2c53a0 --- /dev/null +++ b/docs/examples/as_base_class.cpp @@ -0,0 +1,40 @@ +#include +#include + +class base_class_with_shadowed_members +{ + public: + + int m_value = 43; + + const char* type_name() const noexcept + { + return "my_name"; + } +}; + +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, + base_class_with_shadowed_members + >; + +int main() +{ + json j; + + //access the shadowing method + std::cout << j.type_name() << "\n"; + + //access the shadowed method and member variable + std::cout << j.as_base_class().type_name() << "\n"; + std::cout << j.as_base_class().m_value << "\n"; +} diff --git a/docs/examples/as_base_class.output b/docs/examples/as_base_class.output new file mode 100644 index 000000000..f9954efa3 --- /dev/null +++ b/docs/examples/as_base_class.output @@ -0,0 +1,3 @@ +null +my_name +43 diff --git a/docs/mkdocs/docs/api/basic_json/as_base_class.md b/docs/mkdocs/docs/api/basic_json/as_base_class.md new file mode 100644 index 000000000..84f8d32da --- /dev/null +++ b/docs/mkdocs/docs/api/basic_json/as_base_class.md @@ -0,0 +1,32 @@ +# nlohmann::basic_json::as_base_class + +```cpp +json_base_class_t& as_base_class(); +const json_base_class_t& as_base_class() const; +``` + +Returns this object casted to [json_base_class_t](json_base_class_t.md). + +## Return value + +This object casted to [json_base_class_t](json_base_class_t.md). + +## Examples + +??? example + + Use `as_base_class` to access shadowed methods and member variables defined in [json_base_class_t](json_base_class_t.md). + + ```cpp + --8<-- "examples/as_base_class.cpp" + ``` + + Output: + + ```json + --8<-- "examples/as_base_class.output" + ``` + +## Version history + +Added in version ???.???.??? diff --git a/docs/mkdocs/docs/api/basic_json/index.md b/docs/mkdocs/docs/api/basic_json/index.md index 648670144..13b75bf17 100644 --- a/docs/mkdocs/docs/api/basic_json/index.md +++ b/docs/mkdocs/docs/api/basic_json/index.md @@ -151,6 +151,7 @@ The class satisfies the following concept requirements: - [**array**](array_t.md) (_static_) - explicitly create an array - [**binary**](binary.md) (_static_) - explicitly create a binary array - [**object**](object_t.md) (_static_) - explicitly create an object +- [**as_base_class**](as_base_class.md) - cast this object to [json_base_class_t](json_base_class_t.md) ### Object inspection diff --git a/docs/mkdocs/docs/api/basic_json/json_base_class_t.md b/docs/mkdocs/docs/api/basic_json/json_base_class_t.md index 75752049f..ac3bbbee5 100644 --- a/docs/mkdocs/docs/api/basic_json/json_base_class_t.md +++ b/docs/mkdocs/docs/api/basic_json/json_base_class_t.md @@ -7,6 +7,12 @@ using json_base_class_t = detail::json_base_class; The base class used to inject custom functionality into each instance of `basic_json`. Examples of such functionality might be metadata, additional member functions (e.g., visitors), or other application-specific code. +!!! warning "Name conflicts when using generic names" + + Tt is possible for name shadowing to occur since `nlohmann::basic_json` is derived from this class. + If this happens, the correct method or member variable can be accessed by either casting the json object to the base class or calling [as_base_class](as_base_class.md). + When updating to a new version of this library a previously available member might be shadowed by a new member of `nlohmann::basic_json`. + ## Template parameters `CustomBaseClass` diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 5e66db596..de5dd57f9 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -100,6 +100,7 @@ nav: - 'accept': api/basic_json/accept.md - 'array': api/basic_json/array.md - 'array_t': api/basic_json/array_t.md + - 'as_base_class' : api/basic_json/as_base_class.md - 'at': api/basic_json/at.md - 'back': api/basic_json/back.md - 'begin': api/basic_json/begin.md diff --git a/include/nlohmann/detail/json_custom_base_class.hpp b/include/nlohmann/detail/json_custom_base_class.hpp index ff06653ab..f39c28806 100644 --- a/include/nlohmann/detail/json_custom_base_class.hpp +++ b/include/nlohmann/detail/json_custom_base_class.hpp @@ -1,3 +1,11 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // conditional, is_same diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ad6cd65ee..f3b61e86b 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5146,6 +5146,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } /// @} + + json_base_class_t& as_base_class() + { + return static_cast(*this); + } + + const json_base_class_t& as_base_class() const + { + return static_cast(*this); + } }; /// @brief user-defined to_string function for JSON values diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 09db5f1dc..f2604e9bc 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -13756,6 +13756,14 @@ NLOHMANN_JSON_NAMESPACE_END // #include // #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #include // conditional, is_same @@ -24440,6 +24448,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } /// @} + + json_base_class_t& as_base_class() + { + return static_cast(*this); + } + + const json_base_class_t& as_base_class() const + { + return static_cast(*this); + } }; /// @brief user-defined to_string function for JSON values diff --git a/tests/src/unit-custom-base-class.cpp b/tests/src/unit-custom-base-class.cpp index 2526453bf..4b92a58c5 100644 --- a/tests/src/unit-custom-base-class.cpp +++ b/tests/src/unit-custom-base-class.cpp @@ -1,31 +1,11 @@ -/* - __ _____ _____ _____ - __| | __| | | | JSON for Modern C++ (test suite) -| | |__ | | | | | | version 3.10.2 -|_____|_____|_____|_|___| https://github.com/nlohmann/json - -Licensed under the MIT License . -SPDX-License-Identifier: MIT -Copyright (c) 2013-2019 Niels Lohmann . - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ (supporting code) +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// Copyright (c) 2013-2019 Niels Lohmann . +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT #include #include