mirror of
https://github.com/nlohmann/json.git
synced 2026-07-12 13:35:13 +00:00
Generate template functions with NLOHMANN_DEFINE_TYPE macros (#4597)
* Support any basic_json type in NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Test NLOHMANN_DEFINE_TYPE_* macros also support unordered_json Signed-off-by: kimci86 <kimci86@hotmail.fr> * Simplify test about NLOHMANN_DEFINE_TYPE_ with many arguments Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove extra scope in macros tests Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove unused test class in macros tests Signed-off-by: kimci86 <kimci86@hotmail.fr> * Update documentation about NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix NLOHMANN_JSON_SERIALIZE_ENUM documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> * Mark some variables const in macros tests, fixes clang-tidy Signed-off-by: kimci86 <kimci86@hotmail.fr> * Workaround clang 3.5 issue with const object initialization Signed-off-by: kimci86 <kimci86@hotmail.fr> * Update highlighted lines in NLOHMANN_DEFINE_TYPE_* macros examples Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix swapped macros in documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove extra backslashes at the end of macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Require basic_json type in NLOHMANN_DEFINE_TYPE_* generated functions Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix typos in macros documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> --------- Signed-off-by: kimci86 <kimci86@hotmail.fr>
This commit is contained in:
@@ -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");
|
||||
|
||||
+2
-1
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
|
||||
+2
-1
@@ -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;
|
||||
|
||||
+4
-2
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user