This commit is contained in:
Raphael Grimm 2023-11-27 14:54:06 +03:00 committed by GitHub
commit cd74c2215f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 127 additions and 28 deletions

View File

@ -0,0 +1,40 @@
#include <iostream>
#include <nlohmann/json.hpp>
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<std::uint8_t>,
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";
}

View File

@ -0,0 +1,3 @@
null
my_name
43

View File

@ -0,0 +1,32 @@
# <small>nlohmann::basic_json::</small>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 ???.???.???

View File

@ -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

View File

@ -7,6 +7,12 @@ using json_base_class_t = detail::json_base_class<CustomBaseClass>;
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`

View File

@ -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

View File

@ -1,3 +1,11 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.11.2
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#pragma once
#include <type_traits> // conditional, is_same

View File

@ -5146,6 +5146,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}
/// @}
json_base_class_t& as_base_class()
{
return static_cast<json_base_class_t&>(*this);
}
const json_base_class_t& as_base_class() const
{
return static_cast<const json_base_class_t&>(*this);
}
};
/// @brief user-defined to_string function for JSON values

View File

@ -13756,6 +13756,14 @@ NLOHMANN_JSON_NAMESPACE_END
// #include <nlohmann/detail/iterators/primitive_iterator.hpp>
// #include <nlohmann/detail/json_custom_base_class.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.11.2
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#include <type_traits> // 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<json_base_class_t&>(*this);
}
const json_base_class_t& as_base_class() const
{
return static_cast<const json_base_class_t&>(*this);
}
};
/// @brief user-defined to_string function for JSON values

View File

@ -1,31 +1,11 @@
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.10.2
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
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 <http://nlohmann.me>.
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#include <set>
#include <sstream>