Commit 28644bad by Niels

removed std::out output from test cases

parent 6e1347e6
...@@ -10309,252 +10309,261 @@ TEST_CASE("parser class") ...@@ -10309,252 +10309,261 @@ TEST_CASE("parser class")
TEST_CASE("README", "[hide]") TEST_CASE("README", "[hide]")
{ {
{ {
// create an empty structure (null) // redirect std::cout for the README file
json j; auto old_cout_buffer = std::cout.rdbuf();
std::ostringstream new_stream;
std::cout.rdbuf(new_stream.rdbuf());
{
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object) // add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141; j["pi"] = 3.141;
// add a Boolean that is stored as bool // add a Boolean that is stored as bool
j["happy"] = true; j["happy"] = true;
// add a string that is stored as std::string // add a string that is stored as std::string
j["name"] = "Niels"; j["name"] = "Niels";
// add another null object by passing nullptr // add another null object by passing nullptr
j["nothing"] = nullptr; j["nothing"] = nullptr;
// add an object inside the object // add an object inside the object
j["answer"]["everything"] = 42; j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list) // add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 }; j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs) // add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} }; j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above) // instead, you could also write (which looks very similar to the JSON above)
json j2 = json j2 =
{
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{ {
"answer", { {"pi", 3.141},
{"everything", 42} {"happy", true},
} {"name", "Niels"},
}, {"nothing", nullptr},
{"list", {1, 0, 2}}, {
{ "answer", {
"object", { {"everything", 42}
{"currency", "USD"}, }
{"value", 42.99} },
{"list", {1, 0, 2}},
{
"object", {
{"currency", "USD"},
{"value", 42.99}
}
} }
} };
}; }
}
{ {
// ways to express the empty array [] // ways to express the empty array []
json empty_array_implicit = {{}}; json empty_array_implicit = {{}};
json empty_array_explicit = json::array(); json empty_array_explicit = json::array();
// a way to express the empty object {} // a way to express the empty object {}
json empty_object_explicit = json::object(); json empty_object_explicit = json::object();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]] // a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) }; json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };
} }
{ {
// create object from string literal // create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// or even nicer with a raw string literal // or even nicer with a raw string literal
auto j2 = R"( auto j2 = R"(
{ {
"happy": true, "happy": true,
"pi": 3.141 "pi": 3.141
} }
)"_json; )"_json;
// or explicitly // or explicitly
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }"); auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
// explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }
std::cout << std::setw(2) << j << std::endl; // explicit conversion to string
} std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
{ // serialization with pretty printing
// create an array using push_back // pass in the amount of spaces to indent
json j; std::cout << j.dump(4) << std::endl;
j.push_back("foo"); // {
j.push_back(1); // "happy": true,
j.push_back(true); // "pi": 3.141
// }
// iterate the array std::cout << std::setw(2) << j << std::endl;
for (json::iterator it = j.begin(); it != j.end(); ++it)
{
std::cout << *it << '\n';
} }
// range-based for
for (auto element : j)
{ {
std::cout << element << '\n'; // create an array using push_back
} json j;
j.push_back("foo");
j.push_back(1);
j.push_back(true);
// getter/setter // iterate the array
const std::string tmp = j[0]; for (json::iterator it = j.begin(); it != j.end(); ++it)
j[1] = 42; {
bool foo = j.at(2); std::cout << *it << '\n';
}
// other stuff // range-based for
j.size(); // 3 entries for (auto element : j)
j.empty(); // false {
j.type(); // json::value_t::array std::cout << element << '\n';
j.clear(); // the array is empty again }
// comparison // getter/setter
j == "[\"foo\", 1, true]"_json; // true const std::string tmp = j[0];
j[1] = 42;
bool foo = j.at(2);
// create an object // other stuff
json o; j.size(); // 3 entries
o["foo"] = 23; j.empty(); // false
o["bar"] = false; j.type(); // json::value_t::array
o["baz"] = 3.141; j.clear(); // the array is empty again
// find an entry // comparison
if (o.find("foo") != o.end()) j == "[\"foo\", 1, true]"_json; // true
{
// there is an entry with key "foo" // create an object
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.141;
// find an entry
if (o.find("foo") != o.end())
{
// there is an entry with key "foo"
}
} }
}
{ {
std::vector<int> c_vector {1, 2, 3, 4}; std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector); json j_vec(c_vector);
// [1, 2, 3, 4] // [1, 2, 3, 4]
std::deque<float> c_deque {1.2f, 2.3f, 3.4f, 5.6f}; std::deque<float> c_deque {1.2f, 2.3f, 3.4f, 5.6f};
json j_deque(c_deque); json j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6] // [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true}; std::list<bool> c_list {true, true, false, true};
json j_list(c_list); json j_list(c_list);
// [true, true, false, true] // [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist); json j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543] // [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array); json j_array(c_array);
// [1, 2, 3, 4] // [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"}; std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used json j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"] // ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used json j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"] // maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"}; std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // only one entry for "one" is used json j_mset(c_mset); // only one entry for "one" is used
// maybe ["one", "two", "four"] // maybe ["one", "two", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used json j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"] // maybe ["one", "two", "one", "four"]
} }
{ {
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} }; std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map); json j_map(c_map);
// {"one": 1, "two": 2, "three": 3} // {"one": 1, "two": 2, "three": 3}
std::unordered_map<const char*, float> c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} }; std::unordered_map<const char*, float> c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} };
json j_umap(c_umap); json j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4} // {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used json j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true} // maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used json j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true} // maybe {"one": true, "two": true, "three": true}
} }
{ {
// strings // strings
std::string s1 = "Hello, world!"; std::string s1 = "Hello, world!";
json js = s1; json js = s1;
std::string s2 = js; std::string s2 = js;
// Booleans // Booleans
bool b1 = true; bool b1 = true;
json jb = b1; json jb = b1;
bool b2 = jb; bool b2 = jb;
// numbers // numbers
int i = 42; int i = 42;
json jn = i; json jn = i;
double f = jn; double f = jn;
// etc. // etc.
std::string vs = js.get<std::string>(); std::string vs = js.get<std::string>();
bool vb = jb.get<bool>(); bool vb = jb.get<bool>();
int vi = jn.get<int>(); int vi = jn.get<int>();
// etc. // etc.
} }
{ {
// a JSON value // a JSON value
json j_original = R"({ json j_original = R"({
"baz": ["one", "two", "three"], "baz": ["one", "two", "three"],
"foo": "bar" "foo": "bar"
})"_json; })"_json;
// access members with a JSON pointer (RFC 6901) // access members with a JSON pointer (RFC 6901)
j_original["/baz/1"_json_pointer]; j_original["/baz/1"_json_pointer];
// "two" // "two"
// a JSON patch (RFC 6902) // a JSON patch (RFC 6902)
json j_patch = R"([ json j_patch = R"([
{ "op": "replace", "path": "/baz", "value": "boo" }, { "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] }, { "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"} { "op": "remove", "path": "/foo"}
])"_json; ])"_json;
// apply the patch // apply the patch
json j_result = j_original.patch(j_patch); json j_result = j_original.patch(j_patch);
// { // {
// "baz": "boo", // "baz": "boo",
// "hello": ["world"] // "hello": ["world"]
// } // }
// calculate a JSON patch from two JSON values // calculate a JSON patch from two JSON values
json::diff(j_result, j_original); json::diff(j_result, j_original);
// [ // [
// { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] }, // { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] },
// { "op":"remove","path":"/hello" }, // { "op":"remove","path":"/hello" },
// { "op":"add","path":"/foo","value":"bar" } // { "op":"add","path":"/foo","value":"bar" }
// ] // ]
}
// restore old std::cout
std::cout.rdbuf(old_cout_buffer);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment