unit-readme.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. #include <nlohmann/json.hpp>
  10. using nlohmann::json;
  11. #ifdef JSON_TEST_NO_GLOBAL_UDLS
  12. using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
  13. #endif
  14. #include <deque>
  15. #include <forward_list>
  16. #include <list>
  17. #include <set>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <iostream>
  21. #include <sstream>
  22. #include <iomanip>
  23. // local variable is initialized but not referenced
  24. DOCTEST_MSVC_SUPPRESS_WARNING_PUSH
  25. DOCTEST_MSVC_SUPPRESS_WARNING(4189)
  26. TEST_CASE("README" * doctest::skip())
  27. {
  28. {
  29. // redirect std::cout for the README file
  30. auto* old_cout_buffer = std::cout.rdbuf();
  31. std::ostringstream const new_stream;
  32. std::cout.rdbuf(new_stream.rdbuf());
  33. {
  34. // create an empty structure (null)
  35. json j;
  36. // add a number that is stored as double (note the implicit conversion of j to an object)
  37. j["pi"] = 3.141;
  38. // add a Boolean that is stored as bool
  39. j["happy"] = true;
  40. // add a string that is stored as std::string
  41. j["name"] = "Niels";
  42. // add another null object by passing nullptr
  43. j["nothing"] = nullptr;
  44. // add an object inside the object
  45. j["answer"]["everything"] = 42;
  46. // add an array that is stored as std::vector (using an initializer list)
  47. j["list"] = { 1, 0, 2 };
  48. // add another object (using an initializer list of pairs)
  49. j["object"] = { {"currency", "USD"}, {"value", 42.99} };
  50. // instead, you could also write (which looks very similar to the JSON above)
  51. json const j2 =
  52. {
  53. {"pi", 3.141},
  54. {"happy", true},
  55. {"name", "Niels"},
  56. {"nothing", nullptr},
  57. {
  58. "answer", {
  59. {"everything", 42}
  60. }
  61. },
  62. {"list", {1, 0, 2}},
  63. {
  64. "object", {
  65. {"currency", "USD"},
  66. {"value", 42.99}
  67. }
  68. }
  69. };
  70. }
  71. {
  72. // ways to express the empty array []
  73. json const empty_array_implicit = {{}};
  74. CHECK(empty_array_implicit.is_array());
  75. json const empty_array_explicit = json::array();
  76. CHECK(empty_array_explicit.is_array());
  77. // a way to express the empty object {}
  78. json const empty_object_explicit = json::object();
  79. CHECK(empty_object_explicit.is_object());
  80. // a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
  81. json array_not_object = json::array({ {"currency", "USD"}, {"value", 42.99} });
  82. CHECK(array_not_object.is_array());
  83. CHECK(array_not_object.size() == 2);
  84. CHECK(array_not_object[0].is_array());
  85. CHECK(array_not_object[1].is_array());
  86. }
  87. {
  88. // create object from string literal
  89. json const j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal)
  90. // or even nicer with a raw string literal
  91. auto j2 = R"({
  92. "happy": true,
  93. "pi": 3.141
  94. })"_json;
  95. // or explicitly
  96. auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");
  97. // explicit conversion to string
  98. std::string const s = j.dump(); // {\"happy\":true,\"pi\":3.141}
  99. // serialization with pretty printing
  100. // pass in the amount of spaces to indent
  101. std::cout << j.dump(4) << std::endl;
  102. // {
  103. // "happy": true,
  104. // "pi": 3.141
  105. // }
  106. std::cout << std::setw(2) << j << std::endl;
  107. }
  108. {
  109. // create an array using push_back
  110. json j;
  111. j.push_back("foo");
  112. j.push_back(1);
  113. j.push_back(true);
  114. // comparison
  115. bool x = (j == R"(["foo", 1, true])"_json); // true
  116. CHECK(x == true);
  117. // iterate the array
  118. for (json::iterator it = j.begin(); it != j.end(); ++it) // NOLINT(modernize-loop-convert)
  119. {
  120. std::cout << *it << '\n';
  121. }
  122. // range-based for
  123. for (auto& element : j)
  124. {
  125. std::cout << element << '\n';
  126. }
  127. // getter/setter
  128. const auto tmp = j[0].get<std::string>();
  129. j[1] = 42;
  130. bool foo{j.at(2)};
  131. CHECK(foo == true);
  132. // other stuff
  133. CHECK(j.size() == 3); // 3 entries
  134. CHECK_FALSE(j.empty()); // false
  135. CHECK(j.type() == json::value_t::array); // json::value_t::array
  136. j.clear(); // the array is empty again
  137. // create an object
  138. json o;
  139. o["foo"] = 23;
  140. o["bar"] = false;
  141. o["baz"] = 3.141;
  142. // find an entry
  143. CHECK(o.find("foo") != o.end());
  144. if (o.find("foo") != o.end())
  145. {
  146. // there is an entry with key "foo"
  147. }
  148. }
  149. {
  150. std::vector<int> const c_vector {1, 2, 3, 4};
  151. json const j_vec(c_vector);
  152. // [1, 2, 3, 4]
  153. std::deque<float> const c_deque {1.2f, 2.3f, 3.4f, 5.6f};
  154. json const j_deque(c_deque);
  155. // [1.2, 2.3, 3.4, 5.6]
  156. std::list<bool> const c_list {true, true, false, true};
  157. json const j_list(c_list);
  158. // [true, true, false, true]
  159. std::forward_list<int64_t> const c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
  160. json const j_flist(c_flist);
  161. // [12345678909876, 23456789098765, 34567890987654, 45678909876543]
  162. std::array<unsigned long, 4> const c_array {{1, 2, 3, 4}};
  163. json const j_array(c_array);
  164. // [1, 2, 3, 4]
  165. std::set<std::string> const c_set {"one", "two", "three", "four", "one"};
  166. json const j_set(c_set); // only one entry for "one" is used
  167. // ["four", "one", "three", "two"]
  168. std::unordered_set<std::string> const c_uset {"one", "two", "three", "four", "one"};
  169. json const j_uset(c_uset); // only one entry for "one" is used
  170. // maybe ["two", "three", "four", "one"]
  171. std::multiset<std::string> const c_mset {"one", "two", "one", "four"};
  172. json const j_mset(c_mset); // both entries for "one" are used
  173. // maybe ["one", "two", "one", "four"]
  174. std::unordered_multiset<std::string> const c_umset {"one", "two", "one", "four"};
  175. json const j_umset(c_umset); // both entries for "one" are used
  176. // maybe ["one", "two", "one", "four"]
  177. }
  178. {
  179. std::map<std::string, int> const c_map { {"one", 1}, {"two", 2}, {"three", 3} };
  180. json const j_map(c_map);
  181. // {"one": 1, "two": 2, "three": 3}
  182. std::unordered_map<const char*, float> const c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} };
  183. json const j_umap(c_umap);
  184. // {"one": 1.2, "two": 2.3, "three": 3.4}
  185. std::multimap<std::string, bool> const c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
  186. json const j_mmap(c_mmap); // only one entry for key "three" is used
  187. // maybe {"one": true, "two": true, "three": true}
  188. std::unordered_multimap<std::string, bool> const c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
  189. json const j_ummap(c_ummap); // only one entry for key "three" is used
  190. // maybe {"one": true, "two": true, "three": true}
  191. }
  192. {
  193. // strings
  194. std::string const s1 = "Hello, world!";
  195. json const js = s1;
  196. auto s2 = js.get<std::string>();
  197. // Booleans
  198. bool const b1 = true;
  199. json const jb = b1;
  200. bool b2{jb};
  201. CHECK(b2 == true);
  202. // numbers
  203. int const i = 42;
  204. json const jn = i;
  205. double f{jn};
  206. CHECK(f == 42);
  207. // etc.
  208. std::string const vs = js.get<std::string>();
  209. bool vb = jb.get<bool>();
  210. CHECK(vb == true);
  211. int vi = jn.get<int>();
  212. CHECK(vi == 42);
  213. // etc.
  214. }
  215. {
  216. // a JSON value
  217. json j_original = R"({
  218. "baz": ["one", "two", "three"],
  219. "foo": "bar"
  220. })"_json;
  221. // access members with a JSON pointer (RFC 6901)
  222. j_original["/baz/1"_json_pointer];
  223. // "two"
  224. // a JSON patch (RFC 6902)
  225. json const j_patch = R"([
  226. { "op": "replace", "path": "/baz", "value": "boo" },
  227. { "op": "add", "path": "/hello", "value": ["world"] },
  228. { "op": "remove", "path": "/foo"}
  229. ])"_json;
  230. // apply the patch
  231. json const j_result = j_original.patch(j_patch);
  232. // {
  233. // "baz": "boo",
  234. // "hello": ["world"]
  235. // }
  236. // calculate a JSON patch from two JSON values
  237. auto res = json::diff(j_result, j_original);
  238. // [
  239. // { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] },
  240. // { "op":"remove","path":"/hello" },
  241. // { "op":"add","path":"/foo","value":"bar" }
  242. // ]
  243. }
  244. // restore old std::cout
  245. std::cout.rdbuf(old_cout_buffer);
  246. }
  247. }
  248. DOCTEST_MSVC_SUPPRESS_WARNING_POP