Add nlohmann::json::as_base_class

This commit is contained in:
barcode 2022-12-26 17:46:00 +01:00 committed by Raphael Grimm
parent 317f72ca6d
commit 0f6d47133e
7 changed files with 97 additions and 0 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

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

@ -5155,6 +5155,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

@ -24379,6 +24379,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