1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-29 23:01:16 +03:00

Allow custom base class as node customization point (#3110)

Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
Co-authored-by: barcode <barcode@example.com>
This commit is contained in:
Raphael Grimm
2022-08-28 13:59:07 +02:00
committed by GitHub
parent f7973f46d6
commit bed648ca55
12 changed files with 595 additions and 16 deletions

View File

@ -13,7 +13,8 @@ template<
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer = adl_serializer,
class BinaryType = std::vector<std::uint8_t>
class BinaryType = std::vector<std::uint8_t,
class CustomBaseClass = void>
>
class basic_json;
```
@ -32,6 +33,7 @@ class basic_json;
| `AllocatorType` | type of the allocator to use | |
| `JSONSerializer` | the serializer to resolve internal calls to `to_json()` and `from_json()` | [`json_serializer`](json_serializer.md) |
| `BinaryType` | type for binary arrays | [`binary_t`](binary_t.md) |
| `CustomBaseClass` | extension point for user code | [`json_base_class_t`](json_base_class_t.md) |
## Specializations

View File

@ -0,0 +1,44 @@
# <small>nlohmann::basic_json::</small>json_base_class_t
```cpp
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.
## Template parameters
`CustomBaseClass`
: the base class to be added to `basic_json`
## Notes
#### Default type
The default value for `CustomBaseClass` is `void`. In this case an [empty base class](https://en.cppreference.com/w/cpp/language/ebo) is used and no additional functionality is injected.
#### Limitations
The type `CustomBaseClass` has to be a default-constructible class.
`basic_json` only supports copy/move construction/assignment if `CustomBaseClass` does so as well.
## Examples
??? example
The following code shows how to inject custom data and methods for each node.
```cpp
--8<-- "examples/json_base_class_t.cpp"
```
Output:
```json
--8<-- "examples/json_base_class_t.output"
```
## Version history
- Added in version 3.12.0.

View File

@ -141,6 +141,7 @@ nav:
- 'is_string': api/basic_json/is_string.md
- 'is_structured': api/basic_json/is_structured.md
- 'items': api/basic_json/items.md
- 'json_base_class_t': api/basic_json/json_base_class_t.md
- 'json_serializer': api/basic_json/json_serializer.md
- 'max_size': api/basic_json/max_size.md
- 'meta': api/basic_json/meta.md