-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
- Loading branch information
1 parent
9e1a7c8
commit 7b6cf59
Showing
65 changed files
with
583 additions
and
303 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
// a simple struct to model a person | ||
struct person | ||
{ | ||
std::string name; | ||
std::string address; | ||
int age; | ||
}; | ||
} // namespace ns | ||
|
||
namespace ns | ||
{ | ||
void from_json(const json& j, person& p) | ||
{ | ||
j.at("name").get_to(p.name); | ||
j.at("address").get_to(p.address); | ||
j.at("age").get_to(p.age); | ||
} | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
json j; | ||
j["name"] = "Ned Flanders"; | ||
j["address"] = "744 Evergreen Terrace"; | ||
j["age"] = 60; | ||
|
||
auto p = j.get<ns::person>(); | ||
|
||
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; | ||
} |
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 @@ | ||
Ned Flanders (60) lives in 744 Evergreen Terrace |
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,53 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
// a simple struct to model a person (not default constructible) | ||
struct person | ||
{ | ||
person(std::string n, std::string a, int aa) | ||
: name(std::move(n)), address(std::move(a)), age(aa) | ||
{} | ||
|
||
std::string name; | ||
std::string address; | ||
int age; | ||
}; | ||
} // namespace ns | ||
|
||
namespace nlohmann | ||
{ | ||
template <> | ||
struct adl_serializer<ns::person> | ||
{ | ||
static ns::person from_json(const json& j) | ||
{ | ||
return {j.at("name"), j.at("address"), j.at("age")}; | ||
} | ||
|
||
// Here's the catch! You must provide a to_json method! Otherwise, you | ||
// will not be able to convert person to json, since you fully | ||
// specialized adl_serializer on that type | ||
static void to_json(json& j, ns::person p) | ||
{ | ||
j["name"] = p.name; | ||
j["address"] = p.address; | ||
j["age"] = p.age; | ||
} | ||
}; | ||
} // namespace nlohmann | ||
|
||
int main() | ||
{ | ||
json j; | ||
j["name"] = "Ned Flanders"; | ||
j["address"] = "744 Evergreen Terrace"; | ||
j["age"] = 60; | ||
|
||
auto p = j.get<ns::person>(); | ||
|
||
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl; | ||
} |
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 @@ | ||
Ned Flanders (60) lives in 744 Evergreen Terrace |
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,14 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// possible use case: use NLOHMANN_JSON_NAMESPACE instead of nlohmann | ||
using json = NLOHMANN_JSON_NAMESPACE::json; | ||
|
||
// macro needed to output the NLOHMANN_JSON_NAMESPACE as string literal | ||
#define Q(x) #x | ||
#define QUOTE(x) Q(x) | ||
|
||
int main() | ||
{ | ||
std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl; | ||
} |
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 @@ | ||
nlohmann::json_v3_11_1 |
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 <optional> | ||
#include <nlohmann/json.hpp> | ||
|
||
// partial specialization (see https://json.nlohmann.me/features/arbitrary_types/) | ||
NLOHMANN_JSON_NAMESPACE_BEGIN | ||
template <typename T> | ||
struct adl_serializer<std::optional<T>> | ||
{ | ||
static void to_json(json& j, const std::optional<T>& opt) | ||
{ | ||
if (opt == std::nullopt) | ||
{ | ||
j = nullptr; | ||
} | ||
else | ||
{ | ||
j = *opt; | ||
} | ||
} | ||
}; | ||
NLOHMANN_JSON_NAMESPACE_END | ||
|
||
int main() | ||
{ | ||
std::optional<int> o1 = 1; | ||
std::optional<int> o2 = std::nullopt; | ||
|
||
NLOHMANN_JSON_NAMESPACE::json j; | ||
j.push_back(o1); | ||
j.push_back(o2); | ||
std::cout << j << std::endl; | ||
} |
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 @@ | ||
[1,null] |
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,32 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
// a simple struct to model a person | ||
struct person | ||
{ | ||
std::string name; | ||
std::string address; | ||
int age; | ||
}; | ||
} // namespace ns | ||
|
||
namespace ns | ||
{ | ||
void to_json(json& j, const person& p) | ||
{ | ||
j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} }; | ||
} | ||
} // namespace ns | ||
|
||
int main() | ||
{ | ||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60}; | ||
|
||
json j = p; | ||
|
||
std::cout << j << std::endl; | ||
} |
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 @@ | ||
{"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
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.