Skip to content

Generate template functions with NLOHMANN_DEFINE_TYPE macros #4597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions docs/mkdocs/docs/api/macros/nlohmann_define_derived_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,26 @@ Summary:
Macros 1 and 2 add two friend functions to the class which take care of the serialization and deserialization:

```cpp
friend void to_json(nlohmann::json&, const type&);
friend void from_json(const nlohmann::json&, type&);
template<typename BasicJsonType>
friend void to_json(BasicJsonType&, const type&);
template<typename BasicJsonType>
friend void from_json(const BasicJsonType&, type&);
```

Macros 4 and 5 add two functions to the namespace which take care of the serialization and deserialization:

```cpp
void to_json(nlohmann::json&, const type&);
void from_json(const nlohmann::json&, type&);
template<typename BasicJsonType>
void to_json(BasicJsonType&, const type&);
template<typename BasicJsonType>
void from_json(const BasicJsonType&, type&);
```

Macros 3 and 6 add one function to the namespace which take care of the serialization only:

```cpp
void to_json(nlohmann::json&, const type&);
template<typename BasicJsonType>
void to_json(BasicJsonType&, const type&);
```

In first two cases, they call the `to_json`/`from_json` functions of the base type
Expand All @@ -88,12 +93,14 @@ before serializing/deserializing the members of the derived type:
class A { /* ... */ };
class B : public A { /* ... */ };

void to_json(nlohmann::json& j, const B& b) {
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const B& b) {
nlohmann::to_json(j, static_cast<const A&>(b));
// ...
}

void from_json(const nlohmann::json& j, B& b) {
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, B& b) {
nlohmann::from_json(j, static_cast<A&>(b));
// ...
}
Expand All @@ -105,7 +112,8 @@ 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) {
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const B& b) {
nlohmann::to_json(j, static_cast<const A&>(b));
// ...
}
Expand Down
14 changes: 7 additions & 7 deletions docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ Summary:
The macros add two friend functions to the class which take care of the serialization and deserialization:

```cpp
friend void to_json(nlohmann::json&, const type&);
friend void from_json(const nlohmann::json&, type&); // except (3)
template<typename BasicJsonType>
friend void to_json(BasicJsonType&, const type&);
template<typename BasicJsonType>
friend void from_json(const BasicJsonType&, type&); // except (3)
```

See examples below for the concrete generated code.
Expand All @@ -60,8 +62,6 @@ See examples below for the concrete generated code.

- The current implementation is limited to at most 64 member variables. If you want to serialize/deserialize types
with more than 64 member variables, you need to define the `to_json`/`from_json` functions manually.
- The macros only work for the [`nlohmann::json`](../json.md) type; other specializations such as
[`nlohmann::ordered_json`](../ordered_json.md) are currently unsupported.

## Examples

Expand Down Expand Up @@ -90,7 +90,7 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34"
```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34 35 36"
--8<-- "examples/nlohmann_define_type_intrusive_explicit.cpp"
```

Expand Down Expand Up @@ -118,7 +118,7 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34 35"
```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37"
--8<-- "examples/nlohmann_define_type_intrusive_with_default_explicit.cpp"
```

Expand Down Expand Up @@ -147,7 +147,7 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="22 22 23 24 25 26 27"
```cpp hl_lines="22 22 23 24 25 26 27 28"
--8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ Summary:
The macros add two functions to the namespace which take care of the serialization and deserialization:

```cpp
void to_json(nlohmann::json&, const type&);
void from_json(const nlohmann::json&, type&); // except (3)
template<typename BasicJsonType>
void to_json(BasicJsonType&, const type&);
template<typename BasicJsonType>
void from_json(const BasicJsonType&, type&); // except (3)
```

See examples below for the concrete generated code.
Expand All @@ -61,8 +63,6 @@ See examples below for the concrete generated code.

- The current implementation is limited to at most 64 member variables. If you want to serialize/deserialize types
with more than 64 member variables, you need to define the `to_json`/`from_json` functions manually.
- The macros only work for the [`nlohmann::json`](../json.md) type; other specializations such as
[`nlohmann::ordered_json`](../ordered_json.md) are currently unsupported.

## Examples

Expand Down Expand Up @@ -90,15 +90,15 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28"
```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
--8<-- "examples/nlohmann_define_type_non_intrusive_explicit.cpp"
```

??? example "Example (2): NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT"

Consider the following complete example:

```cpp hl_lines="22"
```cpp hl_lines="21"
--8<-- "examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp"
```

Expand All @@ -119,7 +119,7 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="22 23 24 25 26 27 28 29 30 31 32 33 34 35"
```cpp hl_lines="21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36"
--8<-- "examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp"
```

Expand Down Expand Up @@ -148,7 +148,7 @@ See examples below for the concrete generated code.

The macro is equivalent to:

```cpp hl_lines="16 17 18 19 20 21"
```cpp hl_lines="16 17 18 19 20 21 22"
--8<-- "examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The `NLOHMANN_JSON_SERIALIZE_ENUM` allows to define a user-defined serialization

## Default definition

The macros add two friend functions to the class which take care of the serialization and deserialization:
The macro adds two functions to the namespace which take care of the serialization and deserialization:

```cpp
template<typename BasicJsonType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class person
: name(std::move(name_)), address(std::move(address_)), age(age_)
{}

friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
nlohmann_json_j["age"] = nlohmann_json_t.age;
}

friend void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
template<typename BasicJsonType>
friend void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
{
nlohmann_json_t.name = nlohmann_json_j.at("name");
nlohmann_json_t.address = nlohmann_json_j.at("address");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class person
: name(std::move(name_)), address(std::move(address_)), age(age_)
{}

friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class person
: name(std::move(name_)), address(std::move(address_)), age(age_)
{}

friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
nlohmann_json_j["age"] = nlohmann_json_t.age;
}

friend void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
template<typename BasicJsonType>
friend void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
{
person nlohmann_json_default_obj;
nlohmann_json_t.name = nlohmann_json_j.value("name", nlohmann_json_default_obj.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ struct person
int age;
};

void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
nlohmann_json_j["age"] = nlohmann_json_t.age;
}

void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
template<typename BasicJsonType>
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
{
nlohmann_json_t.name = nlohmann_json_j.at("name");
nlohmann_json_t.address = nlohmann_json_j.at("address");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ struct person
int age;
};

void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ struct person
{}
};

void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
template<typename BasicJsonType>
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
{
nlohmann_json_j["name"] = nlohmann_json_t.name;
nlohmann_json_j["address"] = nlohmann_json_t.address;
nlohmann_json_j["age"] = nlohmann_json_t.age;
}

void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
template<typename BasicJsonType>
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
{
person nlohmann_json_default_obj;
nlohmann_json_t.name = nlohmann_json_j.value("name", nlohmann_json_default_obj.name);
Expand Down
6 changes: 2 additions & 4 deletions docs/mkdocs/docs/features/arbitrary_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ There are six macros to make your life easier as long as you (1) want to use a J

- [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_non_intrusive.md) is to be defined inside the namespace of the class/struct to create code for. It will throw an exception in `from_json()` due to a missing value in the JSON object.
- [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_non_intrusive.md) is to be defined inside the namespace of the class/struct to create code for. It will not throw an exception in `from_json()` due to a missing value in the JSON object, but fills in values from object which is default-constructed by the type.
- [`NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_non_intrusive.md) is to be defined inside the namespace of the class/struct to create code for. It does not define a `fron_json()` function which is needed in case the type does not have a default constructor.
- [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_non_intrusive.md) is to be defined inside the namespace of the class/struct to create code for. It does not define a `from_json()` function which is needed in case the type does not have a default constructor.
- [`NLOHMANN_DEFINE_TYPE_INTRUSIVE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_intrusive.md) is to be defined inside the class/struct to create code for. This macro can also access private members. It will throw an exception in `from_json()` due to a missing value in the JSON object.
- [`NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_intrusive.md) is to be defined inside the class/struct to create code for. This macro can also access private members. It will not throw an exception in `from_json()` due to a missing value in the JSON object, but fills in values from object which is default-constructed by the type.
- [`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_intrusive.md) is to be defined inside the class/struct to create code for. This macro can also access private members. It does not define a `fron_json()` function which is needed in case the type does not have a default constructor.
- [`NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(name, member1, member2, ...)`](../api/macros/nlohmann_define_type_intrusive.md) is to be defined inside the class/struct to create code for. This macro can also access private members. It does not define a `from_json()` function which is needed in case the type does not have a default constructor.

Furthermore, there exist versions to use in case of derived classes:

Expand Down Expand Up @@ -120,8 +120,6 @@ For _derived_ classes and structs, use the following macros

- The current macro implementations are limited to at most 64 member variables. If you want to serialize/deserialize
types with more than 64 member variables, you need to define the `to_json`/`from_json` functions manually.
- The macros only work for the [`nlohmann::json`](../api/json.md) type; other specializations such as
[`nlohmann::ordered_json`](../api/ordered_json.md) are currently unsupported.

??? example

Expand Down
Loading
Loading