-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- implemented `get_ptr` function to return pointer to value member - overworked `get` function to support pointer types - added test cases - added documentation (see http://nlohmann.github.io/json/classnlohmann_1_1basic__json.html) with examples
- Loading branch information
Showing
11 changed files
with
698 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <json.hpp> | ||
|
||
using namespace nlohmann; | ||
|
||
int main() | ||
{ | ||
// create a JSON boolean | ||
json value = 17; | ||
|
||
// explicitly getting pointers | ||
auto p1 = value.get<const json::number_integer_t*>(); | ||
auto p2 = value.get<json::number_integer_t*>(); | ||
auto p3 = value.get<json::number_integer_t* const>(); | ||
auto p4 = value.get<const json::number_integer_t* const>(); | ||
auto p5 = value.get<json::number_float_t*>(); | ||
|
||
// print the pointees | ||
std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; | ||
std::cout << std::boolalpha << (p5 == nullptr) << '\n'; | ||
} |
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 @@ | ||
17 17 17 17 | ||
true |
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,49 @@ | ||
#include <json.hpp> | ||
#include <unordered_map> | ||
|
||
using namespace nlohmann; | ||
|
||
int main() | ||
{ | ||
// create a JSON value with different types | ||
json json_types = | ||
{ | ||
{"boolean", true}, | ||
{ | ||
"number", { | ||
{"integer", 42}, | ||
{"floating-point", 17.23} | ||
} | ||
}, | ||
{"string", "Hello, world!"}, | ||
{"array", {1, 2, 3, 4, 5}}, | ||
{"null", nullptr} | ||
}; | ||
|
||
// use explicit conversions | ||
auto v1 = json_types["boolean"].get<bool>(); | ||
auto v2 = json_types["number"]["integer"].get<int>(); | ||
auto v3 = json_types["number"]["integer"].get<short>(); | ||
auto v4 = json_types["number"]["floating-point"].get<float>(); | ||
auto v5 = json_types["number"]["floating-point"].get<int>(); | ||
auto v6 = json_types["string"].get<std::string>(); | ||
auto v7 = json_types["array"].get<std::vector<short>>(); | ||
auto v8 = json_types.get<std::unordered_map<std::string, json>>(); | ||
|
||
// print the conversion results | ||
std::cout << v1 << '\n'; | ||
std::cout << v2 << ' ' << v3 << '\n'; | ||
std::cout << v4 << ' ' << v5 << '\n'; | ||
std::cout << v6 << '\n'; | ||
|
||
for (auto i : v7) | ||
{ | ||
std::cout << i << ' '; | ||
} | ||
std::cout << "\n\n"; | ||
|
||
for (auto i : v8) | ||
{ | ||
std::cout << i.first << ": " << i.second << '\n'; | ||
} | ||
} |
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,11 @@ | ||
1 | ||
42 42 | ||
17.23 17 | ||
Hello, world! | ||
1 2 3 4 5 | ||
|
||
string: "Hello, world!" | ||
number: {"floating-point":17.23,"integer":42} | ||
null: null | ||
boolean: true | ||
array: [1,2,3,4,5] |
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,20 @@ | ||
#include <json.hpp> | ||
|
||
using namespace nlohmann; | ||
|
||
int main() | ||
{ | ||
// create a JSON boolean | ||
json value = 17; | ||
|
||
// explicitly getting pointers | ||
auto p1 = value.get_ptr<const json::number_integer_t*>(); | ||
auto p2 = value.get_ptr<json::number_integer_t*>(); | ||
auto p3 = value.get_ptr<json::number_integer_t* const>(); | ||
auto p4 = value.get_ptr<const json::number_integer_t* const>(); | ||
auto p5 = value.get_ptr<json::number_float_t*>(); | ||
|
||
// print the pointees | ||
std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n'; | ||
std::cout << std::boolalpha << (p5 == nullptr) << '\n'; | ||
} |
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 @@ | ||
17 17 17 17 | ||
true |
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,49 @@ | ||
#include <json.hpp> | ||
#include <unordered_map> | ||
|
||
using namespace nlohmann; | ||
|
||
int main() | ||
{ | ||
// create a JSON value with different types | ||
json json_types = | ||
{ | ||
{"boolean", true}, | ||
{ | ||
"number", { | ||
{"integer", 42}, | ||
{"floating-point", 17.23} | ||
} | ||
}, | ||
{"string", "Hello, world!"}, | ||
{"array", {1, 2, 3, 4, 5}}, | ||
{"null", nullptr} | ||
}; | ||
|
||
// use implicit conversions | ||
bool v1 = json_types["boolean"]; | ||
int v2 = json_types["number"]["integer"]; | ||
short v3 = json_types["number"]["integer"]; | ||
float v4 = json_types["number"]["floating-point"]; | ||
int v5 = json_types["number"]["floating-point"]; | ||
std::string v6 = json_types["string"]; | ||
std::vector<short> v7 = json_types["array"]; | ||
std::unordered_map<std::string, json> v8 = json_types; | ||
|
||
// print the conversion results | ||
std::cout << v1 << '\n'; | ||
std::cout << v2 << ' ' << v3 << '\n'; | ||
std::cout << v4 << ' ' << v5 << '\n'; | ||
std::cout << v6 << '\n'; | ||
|
||
for (auto i : v7) | ||
{ | ||
std::cout << i << ' '; | ||
} | ||
std::cout << "\n\n"; | ||
|
||
for (auto i : v8) | ||
{ | ||
std::cout << i.first << ": " << i.second << '\n'; | ||
} | ||
} |
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,11 @@ | ||
1 | ||
42 42 | ||
17.23 17 | ||
Hello, world! | ||
1 2 3 4 5 | ||
|
||
string: "Hello, world!" | ||
number: {"floating-point":17.23,"integer":42} | ||
null: null | ||
boolean: true | ||
array: [1,2,3,4,5] |
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.