Add nlohmann::json::as_base_class
This commit is contained in:
parent
317f72ca6d
commit
0f6d47133e
40
docs/examples/as_base_class.cpp
Normal file
40
docs/examples/as_base_class.cpp
Normal 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";
|
||||||
|
}
|
||||||
3
docs/examples/as_base_class.output
Normal file
3
docs/examples/as_base_class.output
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
null
|
||||||
|
my_name
|
||||||
|
43
|
||||||
32
docs/mkdocs/docs/api/basic_json/as_base_class.md
Normal file
32
docs/mkdocs/docs/api/basic_json/as_base_class.md
Normal 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 ???.???.???
|
||||||
@ -151,6 +151,7 @@ The class satisfies the following concept requirements:
|
|||||||
- [**array**](array_t.md) (_static_) - explicitly create an array
|
- [**array**](array_t.md) (_static_) - explicitly create an array
|
||||||
- [**binary**](binary.md) (_static_) - explicitly create a binary array
|
- [**binary**](binary.md) (_static_) - explicitly create a binary array
|
||||||
- [**object**](object_t.md) (_static_) - explicitly create an object
|
- [**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
|
### Object inspection
|
||||||
|
|
||||||
|
|||||||
@ -100,6 +100,7 @@ nav:
|
|||||||
- 'accept': api/basic_json/accept.md
|
- 'accept': api/basic_json/accept.md
|
||||||
- 'array': api/basic_json/array.md
|
- 'array': api/basic_json/array.md
|
||||||
- 'array_t': api/basic_json/array_t.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
|
- 'at': api/basic_json/at.md
|
||||||
- 'back': api/basic_json/back.md
|
- 'back': api/basic_json/back.md
|
||||||
- 'begin': api/basic_json/begin.md
|
- 'begin': api/basic_json/begin.md
|
||||||
|
|||||||
@ -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
|
/// @brief user-defined to_string function for JSON values
|
||||||
|
|||||||
@ -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
|
/// @brief user-defined to_string function for JSON values
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user