11<h1 >NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT,
2- NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT</h1>
2+ NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE,
3+ NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE</h1>
34
45``` cpp
5- #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE (type, base_type, member... ) // (1)
6- #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT (type, base_type, member... ) // (2)
6+ #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE (type, base_type, member... ) // (1)
7+ #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT (type, base_type, member... ) // (2)
8+ #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE (type, base_type, member... ) // (3)
79
8- #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE (type, base_type, member... ) // (3)
9- #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT (type, base_type, member... ) // (4)
10+ #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE (type, base_type, member... ) // (4)
11+ #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT (type, base_type, member... ) // (5)
12+ #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...) // (6)
1013```
1114
1215These macros can be used to simplify the serialization/deserialization of derived types if you want to use a JSON
1316object as serialization and want to use the member variable names as object keys in that object.
1417
15- - Macros 1 and 2 are to be defined **inside** the class/struct to create code for.
18+ - Macros 1, 2 and 3 are to be defined **inside** the class/struct to create code for.
1619Like [`NLOHMANN_DEFINE_TYPE_INTRUSIVE`](nlohmann_define_type_intrusive.md), they can access private members.
17- - Macros 3 and 4 are to be defined **outside** the class/struct to create code for, but **inside** its namespace.
20+ - Macros 4, 5 and 6 are to be defined **outside** the class/struct to create code for, but **inside** its namespace.
1821Like [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE`](nlohmann_define_type_non_intrusive.md),
1922they **cannot** access private members.
2023
@@ -48,14 +51,20 @@ friend void to_json(nlohmann::json&, const type&);
4851friend void from_json(const nlohmann::json&, type&);
4952```
5053
51- Macros 3 and 4 add two functions to the namespace which take care of the serialization and deserialization:
54+ Macros 4 and 5 add two functions to the namespace which take care of the serialization and deserialization:
5255
5356``` cpp
5457void to_json (nlohmann::json&, const type&);
5558void from_json(const nlohmann::json&, type&);
5659```
5760
58- In both cases they call the `to_json`/`from_json` functions of the base type
61+ Macros 3 and 6 add one function to the namespace which take care of the serialization only:
62+
63+ ```cpp
64+ void to_json(nlohmann::json&, const type&);
65+ ```
66+
67+ In first two cases cases they call the ` to_json ` /` from_json ` functions of the base type
5968before serializing/deserializing the members of the derived type:
6069
6170``` cpp
@@ -73,12 +82,24 @@ void from_json(const nlohmann::json& j, B& b) {
7382}
7483```
7584
85+ In the third case only `to_json` will be called:
86+
87+ ```cpp
88+ class A { /* ... */ };
89+ class B : public A { /* ... */ };
90+
91+ void to_json(nlohmann::json& j, const B& b) {
92+ nlohmann::to_json(j, static_cast<const A&>(b));
93+ // ...
94+ }
95+ ```
96+
7697## Notes
7798
7899!!! info "Prerequisites"
79100
80- - Macros 1 and 2 have the same prerequisites of NLOHMANN_DEFINE_TYPE_INTRUSIVE.
81- - Macros 3 and 3 have the same prerequisites of NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
101+ - Macros 1, 2 and 3 have the same prerequisites of NLOHMANN_DEFINE_TYPE_INTRUSIVE.
102+ - Macros 4, 5 and 6 have the same prerequisites of NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
82103 - Serialization/deserialization of base types must be defined.
83104
84105!!! warning "Implementation limits"
0 commit comments