-
-
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.
* 📝 overwork macro documentation * 📝 address review comments * 🔧 add style check to Makefile * 🙈 overwork .gitignore * 📌 Pygments 2.12.0 is broken * ✏️ fix links * 🚸 adjust output to cppcheck * 📝 add titles to more admonitions * ✏️ fix typos * 📝 document future behavior change
- Loading branch information
Showing
63 changed files
with
1,693 additions
and
370 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
json_unit | ||
json_benchmarks | ||
json_benchmarks_simple | ||
fuzz-testing | ||
|
||
*.dSYM | ||
*.o | ||
*.gcno | ||
*.gcda | ||
|
||
build | ||
build_coverage | ||
clang_analyze_build | ||
|
||
benchmarks/files/numbers/*.json | ||
.DS_Store | ||
|
||
.wsjcpp-logs/* | ||
.wsjcpp/* | ||
|
||
.idea | ||
/.idea | ||
/cmake-build-* | ||
|
||
test/test-* | ||
/.vs | ||
|
||
doc/html | ||
doc/mkdocs/venv/ | ||
doc/mkdocs/docs/examples | ||
doc/mkdocs/site | ||
doc/mkdocs/docs/__pycache__/ | ||
/doc/mkdocs/docs/examples/ | ||
/doc/mkdocs/docs/__pycache__/ | ||
/doc/mkdocs/site/ | ||
/doc/mkdocs/venv/ | ||
/doc/docset/JSON_for_Modern_C++.docset/ | ||
/doc/docset/JSON_for_Modern_C++.tgz | ||
/doc/mkdocs/docs/images/json.gif |
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,59 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
person() = default; | ||
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; | ||
} | ||
|
||
friend void from_json(const nlohmann::json& 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"); | ||
nlohmann_json_t.age = nlohmann_json_j.at("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; | ||
|
||
// deserialization: json -> person | ||
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; | ||
auto p2 = j2.get<ns::person>(); | ||
|
||
// incomplete deserialization: | ||
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; | ||
try | ||
{ | ||
auto p3 = j3.get<ns::person>(); | ||
} | ||
catch (json::exception& e) | ||
{ | ||
std::cout << "deserialization failed: " << e.what() << 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,2 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} | ||
deserialization failed: [json.exception.out_of_range.403] key 'age' not found |
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,47 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
person() = default; | ||
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; | ||
|
||
// deserialization: json -> person | ||
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; | ||
auto p2 = j2.get<ns::person>(); | ||
|
||
// incomplete deserialization: | ||
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; | ||
try | ||
{ | ||
auto p3 = j3.get<ns::person>(); | ||
} | ||
catch (json::exception& e) | ||
{ | ||
std::cout << "deserialization failed: " << e.what() << 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,2 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} | ||
deserialization failed: [json.exception.out_of_range.403] key 'age' not found |
54 changes: 54 additions & 0 deletions
54
doc/examples/nlohmann_define_type_intrusive_with_default_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,54 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
person() = default; | ||
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; | ||
} | ||
|
||
friend void from_json(const nlohmann::json& 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); | ||
nlohmann_json_t.address = nlohmann_json_j.value("address", nlohmann_json_default_obj.address); | ||
nlohmann_json_t.age = nlohmann_json_j.value("age", nlohmann_json_default_obj.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; | ||
|
||
// deserialization: json -> person | ||
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; | ||
auto p2 = j2.get<ns::person>(); | ||
|
||
// incomplete deserialization: | ||
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; | ||
auto p3 = j3.get<ns::person>(); | ||
std::cout << "roundtrip: " << json(p3) << std::endl; | ||
} |
2 changes: 2 additions & 0 deletions
2
doc/examples/nlohmann_define_type_intrusive_with_default_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,2 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} | ||
roundtrip: {"address":"742 Evergreen Terrace","age":-1,"name":"Maggie Simpson"} |
41 changes: 41 additions & 0 deletions
41
doc/examples/nlohmann_define_type_intrusive_with_default_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,41 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ns | ||
{ | ||
class person | ||
{ | ||
private: | ||
std::string name = "John Doe"; | ||
std::string address = "123 Fake St"; | ||
int age = -1; | ||
|
||
public: | ||
person() = default; | ||
person(std::string name_, std::string address_, int age_) | ||
: name(std::move(name_)), address(std::move(address_)), age(age_) | ||
{} | ||
|
||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(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; | ||
|
||
// deserialization: json -> person | ||
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; | ||
auto p2 = j2.get<ns::person>(); | ||
|
||
// incomplete deserialization: | ||
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; | ||
auto p3 = j3.get<ns::person>(); | ||
std::cout << "roundtrip: " << json(p3) << std::endl; | ||
} |
2 changes: 2 additions & 0 deletions
2
doc/examples/nlohmann_define_type_intrusive_with_default_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,2 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} | ||
roundtrip: {"address":"742 Evergreen Terrace","age":-1,"name":"Maggie Simpson"} |
52 changes: 52 additions & 0 deletions
52
doc/examples/nlohmann_define_type_non_intrusive_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,52 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
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; | ||
} | ||
|
||
void from_json(const nlohmann::json& 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"); | ||
nlohmann_json_t.age = nlohmann_json_j.at("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; | ||
|
||
// deserialization: json -> person | ||
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json; | ||
auto p2 = j2.get<ns::person>(); | ||
|
||
// incomplete deserialization: | ||
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json; | ||
try | ||
{ | ||
auto p3 = j3.get<ns::person>(); | ||
} | ||
catch (json::exception& e) | ||
{ | ||
std::cout << "deserialization failed: " << e.what() << std::endl; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
doc/examples/nlohmann_define_type_non_intrusive_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,2 @@ | ||
serialization: {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"} | ||
deserialization failed: [json.exception.out_of_range.403] key 'age' not found |
Oops, something went wrong.