-
-
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.
added value() function to get object value at given key or a default value if key does not exist
- Loading branch information
Showing
7 changed files
with
325 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <json.hpp> | ||
|
||
using namespace nlohmann; | ||
|
||
int main() | ||
{ | ||
// create a JSON object with different entry types | ||
json j = | ||
{ | ||
{"integer", 1}, | ||
{"floating", 42.23}, | ||
{"string", "hello world"}, | ||
{"boolean", true}, | ||
{"object", {{"key1", 1}, {"key2", 2}}}, | ||
{"array", {1, 2, 3}} | ||
}; | ||
|
||
// access existing values | ||
int v_integer = j.value("integer", 0); | ||
double v_floating = j.value("floating", 47.11); | ||
|
||
// access nonexisting values and rely on default value | ||
std::string v_string = j.value("nonexisting", "oops"); | ||
bool v_boolean = j.value("nonexisting", false); | ||
|
||
// output values | ||
std::cout << std::boolalpha << v_integer << " " << v_floating | ||
<< " " << v_string << " " << v_boolean << "\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 @@ | ||
<a target="_blank" href="http://melpon.org/wandbox/permlink/pehnjdIduvv90qfX"><b>online</b></a> |
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 42.23 oops false |
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.