-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serialization-only user defined type macros (#3816)
- Loading branch information
Showing
15 changed files
with
293 additions
and
13 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
using namespace nlohmann::literals; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
// No default constructor | ||
person(std::string name_, std::string address_, int age_) | ||
: name(std::move(name_)), address(std::move(address_)), age(age_) | ||
{} | ||
|
||
friend void to_json(nlohmann::json& 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; | ||
} | ||
}; | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; | ||
|
||
// serialization: person -> json | ||
json j = p; | ||
std::cout << "serialization: " << j << std::endl; | ||
} |
1 change: 1 addition & 0 deletions
1
docs/examples/nlohmann_define_type_intrusive_only_serialize_explicit.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} |
33 changes: 33 additions & 0 deletions
33
docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
using namespace nlohmann::literals; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
// No default constructor | ||
person(std::string name_, std::string address_, int age_) | ||
: name(std::move(name_)), address(std::move(address_)), age(age_) | ||
{} | ||
|
||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person, name, address, age) | ||
}; | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; | ||
|
||
// serialization: person -> json | ||
json j = p; | ||
std::cout << "serialization: " << j << std::endl; | ||
} |
1 change: 1 addition & 0 deletions
1
docs/examples/nlohmann_define_type_intrusive_only_serialize_macro.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} |
31 changes: 31 additions & 0 deletions
31
docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
using namespace nlohmann::literals; | ||
|
||
namespace ns | ||
{ | ||
struct person | ||
{ | ||
std::string name; | ||
std::string address; | ||
int age; | ||
}; | ||
|
||
void to_json(nlohmann::json& 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; | ||
} | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; | ||
|
||
// serialization: person -> json | ||
json j = p; | ||
std::cout << "serialization: " << j << std::endl; | ||
} |
1 change: 1 addition & 0 deletions
1
docs/examples/nlohmann_define_type_non_intrusive_only_serialize_explicit.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} |
26 changes: 26 additions & 0 deletions
26
docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
using namespace nlohmann::literals; | ||
|
||
namespace ns | ||
{ | ||
struct person | ||
{ | ||
std::string name; | ||
std::string address; | ||
int age; | ||
}; | ||
|
||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(person, name, address, age) | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; | ||
|
||
// serialization: person -> json | ||
json j = p; | ||
std::cout << "serialization: " << j << std::endl; | ||
} |
1 change: 1 addition & 0 deletions
1
docs/examples/nlohmann_define_type_non_intrusive_only_serialize_macro.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.