fix documentation hiccups
This commit is contained in:
parent
cd0ed45276
commit
aa3a920b3a
@ -839,7 +839,7 @@ Some important things:
|
|||||||
|
|
||||||
#### Simplify your life with macros
|
#### Simplify your life with macros
|
||||||
|
|
||||||
If you just want to serialize/deserialize some structs, the `to_json`/`from_json` functions can be a lot of boilerplate. There are [**several macros**](https://json.nlohmann.me/api/macros/#serializationdeserialization-macros) to make your life easier as long as you want to use a dedicated DTO for JSON serialization.
|
If you just want to serialize/deserialize some structs, the `to_json`/`from_json` functions can be a lot of boilerplate. There are [**several macros**](https://json.nlohmann.me/api/macros/#serializationdeserialization-macros) to make your life easier as long as you want to use a JSON object as serialization.
|
||||||
|
|
||||||
Which macro to choose depends on whether private member variables need to be accessed, a deserialization is needed, missing values should yield an error or should be replaced by default values, and if derived classes are used. See [this overview to choose the right one for your use case](https://json.nlohmann.me/features/arbitrary_types/#simplify-your-life-with-macros).
|
Which macro to choose depends on whether private member variables need to be accessed, a deserialization is needed, missing values should yield an error or should be replaced by default values, and if derived classes are used. See [this overview to choose the right one for your use case](https://json.nlohmann.me/features/arbitrary_types/#simplify-your-life-with-macros).
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
# The Named Conversion Macros
|
# NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES, NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES(type, "json_member_name", member...)
|
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES(type, "json_member_name", member...)
|
||||||
@ -33,7 +33,7 @@ For further information please refer to the corresponding macros without `WITH_N
|
|||||||
: name of the base type (class, struct) `type` is derived from (used only in `DEFINE_DERIVED_TYPE` macros)
|
: name of the base type (class, struct) `type` is derived from (used only in `DEFINE_DERIVED_TYPE` macros)
|
||||||
|
|
||||||
`json_member_name` (in)
|
`json_member_name` (in)
|
||||||
: json name that will be used for the next member variable
|
: the string that will be used as the name for the next value
|
||||||
|
|
||||||
`member` (in)
|
`member` (in)
|
||||||
: name of the member variable to serialize/deserialize
|
: name of the member variable to serialize/deserialize
|
||||||
|
|||||||
@ -13,14 +13,14 @@ struct person
|
|||||||
int age;
|
int age;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int >= 0>
|
template <typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0>
|
||||||
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t) {
|
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t) {
|
||||||
nlohmann_json_j["json_name"] = nlohmann_json_t.name;
|
nlohmann_json_j["json_name"] = nlohmann_json_t.name;
|
||||||
nlohmann_json_j["json_address"] = nlohmann_json_t.address;
|
nlohmann_json_j["json_address"] = nlohmann_json_t.address;
|
||||||
nlohmann_json_j["json_age"] = nlohmann_json_t.age;
|
nlohmann_json_j["json_age"] = nlohmann_json_t.age;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int >= 0>
|
template <typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0>
|
||||||
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t) {
|
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t) {
|
||||||
nlohmann_json_j.at("json_name").get_to(nlohmann_json_t.name);
|
nlohmann_json_j.at("json_name").get_to(nlohmann_json_t.name);
|
||||||
nlohmann_json_j.at("json_address").get_to(nlohmann_json_t.address);
|
nlohmann_json_j.at("json_address").get_to(nlohmann_json_t.address);
|
||||||
|
|||||||
@ -85,7 +85,7 @@ Some important things:
|
|||||||
|
|
||||||
If you just want to serialize/deserialize some structs, the `to_json`/`from_json` functions can be a lot of boilerplate.
|
If you just want to serialize/deserialize some structs, the `to_json`/`from_json` functions can be a lot of boilerplate.
|
||||||
|
|
||||||
There are several macros to make your life easier as long as you want to use a dedicated DTO for JSON serialization. The macros are following the naming pattern, and you can chose the macro based on the needed features:
|
There are several macros to make your life easier as long as you want to use a JSON object as serialization. The macros are following the naming pattern, and you can chose the macro based on the needed features:
|
||||||
|
|
||||||
- All the macros start with `NLOHMANN_DEFINE`.
|
- All the macros start with `NLOHMANN_DEFINE`.
|
||||||
- If you want a macro for the derived object, use the [`DERIVED_TYPE`](../api/macros/nlohmann_define_derived_type.md) variant, otherwise use `TYPE`.
|
- If you want a macro for the derived object, use the [`DERIVED_TYPE`](../api/macros/nlohmann_define_derived_type.md) variant, otherwise use `TYPE`.
|
||||||
@ -98,13 +98,33 @@ There are several macros to make your life easier as long as you want to use a d
|
|||||||
|
|
||||||
For all the macros, the first parameter is the name of the class/struct. The `DERIVED_TYPE` macros require a second parameter of a base class. All the remaining parameters name the member variables. The `WITH_NAMES` macros require a JSON name before each of the variables.
|
For all the macros, the first parameter is the name of the class/struct. The `DERIVED_TYPE` macros require a second parameter of a base class. All the remaining parameters name the member variables. The `WITH_NAMES` macros require a JSON name before each of the variables.
|
||||||
|
|
||||||
|
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro |
|
||||||
|
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_INTRUSIVE**](../api/macros/nlohmann_define_type_intrusive.md) |
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT**](../api/macros/nlohmann_define_type_intrusive.md) |
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE**](../api/macros/nlohmann_define_type_intrusive.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE**](../api/macros/nlohmann_define_type_non_intrusive.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT**](../api/macros/nlohmann_define_type_non_intrusive.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | [**NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE**](../api/macros/nlohmann_define_type_non_intrusive.md) |
|
||||||
|
|
||||||
|
For _derived_ classes and structs, use the following macros
|
||||||
|
|
||||||
|
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro |
|
||||||
|
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: grey;">:octicons-skip-fill-24:</div> | [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE**](../api/macros/nlohmann_define_derived_type.md) |
|
||||||
|
|
||||||
!!! info "Implementation limits"
|
!!! info "Implementation limits"
|
||||||
|
|
||||||
- The current macro implementations are limited to at most 63 member variables. If you want to serialize/deserialize
|
- The current macro implementations are limited to at most 63 member variables. If you want to serialize/deserialize
|
||||||
types with more than 63 member variables, you need to define the `to_json`/`from_json` functions manually.
|
types with more than 63 member variables, you need to define the `to_json`/`from_json` functions manually.
|
||||||
- For the `WITH_NAMES` variants the limit is halved to 31 member variables.
|
- For the `WITH_NAMES` variants the limit is halved to 31 member variables.
|
||||||
|
|
||||||
??? examples
|
??? example
|
||||||
|
|
||||||
The `to_json`/`from_json` functions for the `person` struct above can be created with:
|
The `to_json`/`from_json` functions for the `person` struct above can be created with:
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user