Skip to content

Commit 8a882f3

Browse files
authored
Generate template functions with NLOHMANN_DEFINE_TYPE macros (#4597)
* Support any basic_json type in NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <[email protected]> * Test NLOHMANN_DEFINE_TYPE_* macros also support unordered_json Signed-off-by: kimci86 <[email protected]> * Simplify test about NLOHMANN_DEFINE_TYPE_ with many arguments Signed-off-by: kimci86 <[email protected]> * Remove extra scope in macros tests Signed-off-by: kimci86 <[email protected]> * Remove unused test class in macros tests Signed-off-by: kimci86 <[email protected]> * Update documentation about NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <[email protected]> * Fix NLOHMANN_JSON_SERIALIZE_ENUM documentation Signed-off-by: kimci86 <[email protected]> * Mark some variables const in macros tests, fixes clang-tidy Signed-off-by: kimci86 <[email protected]> * Workaround clang 3.5 issue with const object initialization Signed-off-by: kimci86 <[email protected]> * Update highlighted lines in NLOHMANN_DEFINE_TYPE_* macros examples Signed-off-by: kimci86 <[email protected]> * Fix swapped macros in documentation Signed-off-by: kimci86 <[email protected]> * Remove extra backslashes at the end of macros Signed-off-by: kimci86 <[email protected]> * Require basic_json type in NLOHMANN_DEFINE_TYPE_* generated functions Signed-off-by: kimci86 <[email protected]> * Fix typos in macros documentation Signed-off-by: kimci86 <[email protected]> --------- Signed-off-by: kimci86 <[email protected]>
1 parent bdb8d2b commit 8a882f3

14 files changed

+275
-231
lines changed

docs/mkdocs/docs/api/macros/nlohmann_define_derived_type.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,26 @@ Summary:
6464
Macros 1 and 2 add two friend functions to the class which take care of the serialization and deserialization:
6565

6666
```cpp
67-
friend void to_json(nlohmann::json&, const type&);
68-
friend void from_json(const nlohmann::json&, type&);
67+
template<typename BasicJsonType>
68+
friend void to_json(BasicJsonType&, const type&);
69+
template<typename BasicJsonType>
70+
friend void from_json(const BasicJsonType&, type&);
6971
```
7072
7173
Macros 4 and 5 add two functions to the namespace which take care of the serialization and deserialization:
7274
7375
```cpp
74-
void to_json(nlohmann::json&, const type&);
75-
void from_json(const nlohmann::json&, type&);
76+
template<typename BasicJsonType>
77+
void to_json(BasicJsonType&, const type&);
78+
template<typename BasicJsonType>
79+
void from_json(const BasicJsonType&, type&);
7680
```
7781

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

8084
```cpp
81-
void to_json(nlohmann::json&, const type&);
85+
template<typename BasicJsonType>
86+
void to_json(BasicJsonType&, const type&);
8287
```
8388
8489
In first two cases, they call the `to_json`/`from_json` functions of the base type
@@ -88,12 +93,14 @@ before serializing/deserializing the members of the derived type:
8893
class A { /* ... */ };
8994
class B : public A { /* ... */ };
9095
91-
void to_json(nlohmann::json& j, const B& b) {
96+
template<typename BasicJsonType>
97+
void to_json(BasicJsonType& j, const B& b) {
9298
nlohmann::to_json(j, static_cast<const A&>(b));
9399
// ...
94100
}
95101
96-
void from_json(const nlohmann::json& j, B& b) {
102+
template<typename BasicJsonType>
103+
void from_json(const BasicJsonType& j, B& b) {
97104
nlohmann::from_json(j, static_cast<A&>(b));
98105
// ...
99106
}
@@ -105,7 +112,8 @@ In the third case, only `to_json` will be called:
105112
class A { /* ... */ };
106113
class B : public A { /* ... */ };
107114

108-
void to_json(nlohmann::json& j, const B& b) {
115+
template<typename BasicJsonType>
116+
void to_json(BasicJsonType& j, const B& b) {
109117
nlohmann::to_json(j, static_cast<const A&>(b));
110118
// ...
111119
}

docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ Summary:
4040
The macros add two friend functions to the class which take care of the serialization and deserialization:
4141
4242
```cpp
43-
friend void to_json(nlohmann::json&, const type&);
44-
friend void from_json(const nlohmann::json&, type&); // except (3)
43+
template<typename BasicJsonType>
44+
friend void to_json(BasicJsonType&, const type&);
45+
template<typename BasicJsonType>
46+
friend void from_json(const BasicJsonType&, type&); // except (3)
4547
```
4648

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

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

6666
## Examples
6767

@@ -90,7 +90,7 @@ See examples below for the concrete generated code.
9090

9191
The macro is equivalent to:
9292

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

@@ -118,7 +118,7 @@ See examples below for the concrete generated code.
118118

119119
The macro is equivalent to:
120120

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

@@ -147,7 +147,7 @@ See examples below for the concrete generated code.
147147

148148
The macro is equivalent to:
149149

150-
```cpp hl_lines="22 22 23 24 25 26 27"
150+
```cpp hl_lines="22 22 23 24 25 26 27 28"
151151
--8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
152152
```
153153

docs/mkdocs/docs/api/macros/nlohmann_define_type_non_intrusive.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ Summary:
4040
The macros add two functions to the namespace which take care of the serialization and deserialization:
4141
4242
```cpp
43-
void to_json(nlohmann::json&, const type&);
44-
void from_json(const nlohmann::json&, type&); // except (3)
43+
template<typename BasicJsonType>
44+
void to_json(BasicJsonType&, const type&);
45+
template<typename BasicJsonType>
46+
void from_json(const BasicJsonType&, type&); // except (3)
4547
```
4648

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

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

6767
## Examples
6868

@@ -90,15 +90,15 @@ See examples below for the concrete generated code.
9090

9191
The macro is equivalent to:
9292

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

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

9999
Consider the following complete example:
100100

101-
```cpp hl_lines="22"
101+
```cpp hl_lines="21"
102102
--8<-- "examples/nlohmann_define_type_non_intrusive_with_default_macro.cpp"
103103
```
104104

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

120120
The macro is equivalent to:
121121

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

@@ -148,7 +148,7 @@ See examples below for the concrete generated code.
148148

149149
The macro is equivalent to:
150150

151-
```cpp hl_lines="16 17 18 19 20 21"
151+
```cpp hl_lines="16 17 18 19 20 21 22"
152152
--8<-- "examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp"
153153
```
154154

docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The `NLOHMANN_JSON_SERIALIZE_ENUM` allows to define a user-defined serialization
2020

2121
## Default definition
2222

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

2525
```cpp
2626
template<typename BasicJsonType>

docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ class person
1919
: name(std::move(name_)), address(std::move(address_)), age(age_)
2020
{}
2121

22-
friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
22+
template<typename BasicJsonType>
23+
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
2324
{
2425
nlohmann_json_j["name"] = nlohmann_json_t.name;
2526
nlohmann_json_j["address"] = nlohmann_json_t.address;
2627
nlohmann_json_j["age"] = nlohmann_json_t.age;
2728
}
2829

29-
friend void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
30+
template<typename BasicJsonType>
31+
friend void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
3032
{
3133
nlohmann_json_t.name = nlohmann_json_j.at("name");
3234
nlohmann_json_t.address = nlohmann_json_j.at("address");

docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class person
1919
: name(std::move(name_)), address(std::move(address_)), age(age_)
2020
{}
2121

22-
friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
22+
template<typename BasicJsonType>
23+
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
2324
{
2425
nlohmann_json_j["name"] = nlohmann_json_t.name;
2526
nlohmann_json_j["address"] = nlohmann_json_t.address;

docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_with_default_explicit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ class person
1919
: name(std::move(name_)), address(std::move(address_)), age(age_)
2020
{}
2121

22-
friend void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
22+
template<typename BasicJsonType>
23+
friend void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
2324
{
2425
nlohmann_json_j["name"] = nlohmann_json_t.name;
2526
nlohmann_json_j["address"] = nlohmann_json_t.address;
2627
nlohmann_json_j["age"] = nlohmann_json_t.age;
2728
}
2829

29-
friend void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
30+
template<typename BasicJsonType>
31+
friend void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
3032
{
3133
person nlohmann_json_default_obj;
3234
nlohmann_json_t.name = nlohmann_json_j.value("name", nlohmann_json_default_obj.name);

docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_explicit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ struct person
1313
int age;
1414
};
1515

16-
void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
16+
template<typename BasicJsonType>
17+
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
1718
{
1819
nlohmann_json_j["name"] = nlohmann_json_t.name;
1920
nlohmann_json_j["address"] = nlohmann_json_t.address;
2021
nlohmann_json_j["age"] = nlohmann_json_t.age;
2122
}
2223

23-
void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
24+
template<typename BasicJsonType>
25+
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
2426
{
2527
nlohmann_json_t.name = nlohmann_json_j.at("name");
2628
nlohmann_json_t.address = nlohmann_json_j.at("address");

docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ struct person
1313
int age;
1414
};
1515

16-
void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
16+
template<typename BasicJsonType>
17+
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
1718
{
1819
nlohmann_json_j["name"] = nlohmann_json_t.name;
1920
nlohmann_json_j["address"] = nlohmann_json_t.address;

docs/mkdocs/docs/examples/nlohmann_define_type_non_intrusive_with_default_explicit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ struct person
1818
{}
1919
};
2020

21-
void to_json(nlohmann::json& nlohmann_json_j, const person& nlohmann_json_t)
21+
template<typename BasicJsonType>
22+
void to_json(BasicJsonType& nlohmann_json_j, const person& nlohmann_json_t)
2223
{
2324
nlohmann_json_j["name"] = nlohmann_json_t.name;
2425
nlohmann_json_j["address"] = nlohmann_json_t.address;
2526
nlohmann_json_j["age"] = nlohmann_json_t.age;
2627
}
2728

28-
void from_json(const nlohmann::json& nlohmann_json_j, person& nlohmann_json_t)
29+
template<typename BasicJsonType>
30+
void from_json(const BasicJsonType& nlohmann_json_j, person& nlohmann_json_t)
2931
{
3032
person nlohmann_json_default_obj;
3133
nlohmann_json_t.name = nlohmann_json_j.value("name", nlohmann_json_default_obj.name);

0 commit comments

Comments
 (0)