NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(type, base_type, member...) // (1)
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(type, base_type, member...) // (2)
#define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...) // (3)
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(type, base_type, member...) // (4)
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(type, base_type, member...) // (5)
#define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(type, base_type, member...) // (6)
These macros can be used to simplify the serialization/deserialization of derived types if you want to use a JSON object as serialization and want to use the member variable names as object keys in that object.
- Macros 1, 2 and 3 are to be defined inside the class/struct to create code for. Like
NLOHMANN_DEFINE_TYPE_INTRUSIVE
, they can access private members. - Macros 4, 5 and 6 are to be defined outside the class/struct to create code for, but inside its namespace. Like
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
, they cannot access private members.
The first parameter is the name of the derived class/struct, the second parameter is the name of the base class/struct and all remaining parameters name the members. The base type must be already serializable/deserializable.
- Macros 1 and 3 will use
at
during deserialization and will throwout_of_range.403
if a key is missing in the JSON object. - Macros 2 and 4 will use
value
during deserialization and fall back to the default value for the respective type of the member variable if a key in the JSON object is missing. The generatedfrom_json()
function default constructs an object and uses its values as the defaults when calling thevalue
function.
Parameters¶
type
(in)- name of the type (class, struct) to serialize/deserialize
base_type
(in)- name of the base type (class, struct)
type
is derived from member
(in)- name of the member variable to serialize/deserialize; up to 64 members can be given as comma-separated list
Default definition¶
Macros 1 and 2 add two friend functions to the class which take care of the serialization and deserialization:
friend void to_json(nlohmann::json&, const type&);
friend void from_json(const nlohmann::json&, type&);
Macros 4 and 5 add two functions to the namespace which take care of the serialization and deserialization:
void to_json(nlohmann::json&, const type&);
void from_json(const nlohmann::json&, type&);
Macros 3 and 6 add one function to the namespace which take care of the serialization only:
void to_json(nlohmann::json&, const type&);
In first two cases cases they call the to_json
/from_json
functions of the base type before serializing/deserializing the members of the derived type:
class A { /* ... */ };
class B : public A { /* ... */ };
void to_json(nlohmann::json& j, const B& b) {
nlohmann::to_json(j, static_cast<const A&>(b));
// ...
}
void from_json(const nlohmann::json& j, B& b) {
nlohmann::from_json(j, static_cast<A&>(b));
// ...
}
In the third case only to_json
will be called:
class A { /* ... */ };
class B : public A { /* ... */ };
void to_json(nlohmann::json& j, const B& b) {
nlohmann::to_json(j, static_cast<const A&>(b));
// ...
}
Notes¶
Prerequisites
- Macros 1, 2 and 3 have the same prerequisites of NLOHMANN_DEFINE_TYPE_INTRUSIVE.
- Macros 4, 5 and 6 have the same prerequisites of NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
- Serialization/deserialization of base types must be defined.
Implementation limits
- See Implementation limits for NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE.
Examples¶
Example of NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
usage:
class A {
double Aa;
double Ab;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(A, Aa, Ab)
};
class B : public A {
int Ba;
int Bb;
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(B, A, Ba, Bb)
};
See also¶
- NLOHMANN_DEFINE_TYPE_INTRUSIVE / NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT for similar macros that can be defined inside a non-derived type.
- NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT for similar macros that can be defined outside a non-derived type.
- Arbitrary Type Conversions for an overview.
Version history¶
- Added in version 3.11.x.