unit-custom-base-class.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++ (test suite)
  4. | | |__ | | | | | | version 3.10.2
  5. |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  7. SPDX-License-Identifier: MIT
  8. Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all
  16. copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. #include <set>
  26. #include <sstream>
  27. #include <string>
  28. #include "doctest_compatibility.h"
  29. #include <nlohmann/json.hpp>
  30. // Test extending nlohmann::json by using a custom base class.
  31. // Add some metadata to each node and test the behaviour of copy / move
  32. template<class MetaDataType>
  33. class json_metadata
  34. {
  35. public:
  36. using metadata_t = MetaDataType;
  37. metadata_t& metadata()
  38. {
  39. return m_metadata;
  40. }
  41. const metadata_t& metadata() const
  42. {
  43. return m_metadata;
  44. }
  45. private:
  46. metadata_t m_metadata = {};
  47. };
  48. template<class T>
  49. using json_with_metadata =
  50. nlohmann::basic_json <
  51. std::map,
  52. std::vector,
  53. std::string,
  54. bool,
  55. std::int64_t,
  56. std::uint64_t,
  57. double,
  58. std::allocator,
  59. nlohmann::adl_serializer,
  60. std::vector<std::uint8_t>,
  61. json_metadata<T>
  62. >;
  63. TEST_CASE("JSON Node Metadata")
  64. {
  65. SECTION("type int")
  66. {
  67. using json = json_with_metadata<int>;
  68. json null;
  69. auto obj = json::object();
  70. auto array = json::array();
  71. null.metadata() = 1;
  72. obj.metadata() = 2;
  73. array.metadata() = 3;
  74. auto copy = array;
  75. CHECK(null.metadata() == 1);
  76. CHECK(obj.metadata() == 2);
  77. CHECK(array.metadata() == 3);
  78. CHECK(copy.metadata() == 3);
  79. }
  80. SECTION("type vector<int>")
  81. {
  82. using json = json_with_metadata<std::vector<int>>;
  83. json value;
  84. value.metadata().emplace_back(1);
  85. auto copy = value;
  86. value.metadata().emplace_back(2);
  87. CHECK(copy.metadata().size() == 1);
  88. CHECK(copy.metadata().at(0) == 1);
  89. CHECK(value.metadata().size() == 2);
  90. CHECK(value.metadata().at(0) == 1);
  91. CHECK(value.metadata().at(1) == 2);
  92. }
  93. SECTION("copy ctor")
  94. {
  95. using json = json_with_metadata<std::vector<int>>;
  96. json value;
  97. value.metadata().emplace_back(1);
  98. value.metadata().emplace_back(2);
  99. json copy = value;
  100. CHECK(copy.metadata().size() == 2);
  101. CHECK(copy.metadata().at(0) == 1);
  102. CHECK(copy.metadata().at(1) == 2);
  103. CHECK(value.metadata().size() == 2);
  104. CHECK(value.metadata().at(0) == 1);
  105. CHECK(value.metadata().at(1) == 2);
  106. value.metadata().clear();
  107. CHECK(copy.metadata().size() == 2);
  108. CHECK(value.metadata().size() == 0);
  109. }
  110. SECTION("move ctor")
  111. {
  112. using json = json_with_metadata<std::vector<int>>;
  113. json value;
  114. value.metadata().emplace_back(1);
  115. value.metadata().emplace_back(2);
  116. const json moved = std::move(value);
  117. CHECK(moved.metadata().size() == 2);
  118. CHECK(moved.metadata().at(0) == 1);
  119. CHECK(moved.metadata().at(1) == 2);
  120. }
  121. SECTION("move assign")
  122. {
  123. using json = json_with_metadata<std::vector<int>>;
  124. json value;
  125. value.metadata().emplace_back(1);
  126. value.metadata().emplace_back(2);
  127. json moved;
  128. moved = std::move(value);
  129. CHECK(moved.metadata().size() == 2);
  130. CHECK(moved.metadata().at(0) == 1);
  131. CHECK(moved.metadata().at(1) == 2);
  132. }
  133. SECTION("copy assign")
  134. {
  135. using json = json_with_metadata<std::vector<int>>;
  136. json value;
  137. value.metadata().emplace_back(1);
  138. value.metadata().emplace_back(2);
  139. json copy;
  140. copy = value;
  141. CHECK(copy.metadata().size() == 2);
  142. CHECK(copy.metadata().at(0) == 1);
  143. CHECK(copy.metadata().at(1) == 2);
  144. CHECK(value.metadata().size() == 2);
  145. CHECK(value.metadata().at(0) == 1);
  146. CHECK(value.metadata().at(1) == 2);
  147. value.metadata().clear();
  148. CHECK(copy.metadata().size() == 2);
  149. CHECK(value.metadata().size() == 0);
  150. }
  151. SECTION("type unique_ptr<int>")
  152. {
  153. using json = json_with_metadata<std::unique_ptr<int>>;
  154. json value;
  155. value.metadata().reset(new int(42)); // NOLINT(cppcoreguidelines-owning-memory)
  156. auto moved = std::move(value);
  157. CHECK(moved.metadata() != nullptr);
  158. CHECK(*moved.metadata() == 42);
  159. }
  160. SECTION("type vector<int> in json array")
  161. {
  162. using json = json_with_metadata<std::vector<int>>;
  163. json value;
  164. value.metadata().emplace_back(1);
  165. value.metadata().emplace_back(2);
  166. json const array(10, value);
  167. CHECK(value.metadata().size() == 2);
  168. CHECK(value.metadata().at(0) == 1);
  169. CHECK(value.metadata().at(1) == 2);
  170. for (const auto& val : array)
  171. {
  172. CHECK(val.metadata().size() == 2);
  173. CHECK(val.metadata().at(0) == 1);
  174. CHECK(val.metadata().at(1) == 2);
  175. }
  176. }
  177. }
  178. // Test extending nlohmann::json by using a custom base class.
  179. // Add a custom member function template iterating over the whole json tree.
  180. class visitor_adaptor
  181. {
  182. public:
  183. template <class Fnc>
  184. void visit(const Fnc& fnc) const;
  185. private:
  186. template <class Ptr, class Fnc>
  187. void do_visit(const Ptr& ptr, const Fnc& fnc) const;
  188. };
  189. using json_with_visitor_t = nlohmann::basic_json <
  190. std::map,
  191. std::vector,
  192. std::string,
  193. bool,
  194. std::int64_t,
  195. std::uint64_t,
  196. double,
  197. std::allocator,
  198. nlohmann::adl_serializer,
  199. std::vector<std::uint8_t>,
  200. visitor_adaptor
  201. >;
  202. template <class Fnc>
  203. void visitor_adaptor::visit(const Fnc& fnc) const
  204. {
  205. do_visit(json_with_visitor_t::json_pointer{}, fnc);
  206. }
  207. template <class Ptr, class Fnc>
  208. void visitor_adaptor::do_visit(const Ptr& ptr, const Fnc& fnc) const
  209. {
  210. using value_t = nlohmann::detail::value_t;
  211. const json_with_visitor_t& json = *static_cast<const json_with_visitor_t*>(this);
  212. switch (json.type())
  213. {
  214. case value_t::object:
  215. for (const auto& entry : json.items())
  216. {
  217. entry.value().do_visit(ptr / entry.key(), fnc);
  218. }
  219. break;
  220. case value_t::array:
  221. for (std::size_t i = 0; i < json.size(); ++i)
  222. {
  223. json.at(i).do_visit(ptr / std::to_string(i), fnc);
  224. }
  225. break;
  226. case value_t::discarded:
  227. break;
  228. case value_t::null:
  229. case value_t::string:
  230. case value_t::boolean:
  231. case value_t::number_integer:
  232. case value_t::number_unsigned:
  233. case value_t::number_float:
  234. case value_t::binary:
  235. default:
  236. fnc(ptr, json);
  237. }
  238. }
  239. TEST_CASE("JSON Visit Node")
  240. {
  241. json_with_visitor_t json;
  242. json["null"];
  243. json["int"] = -1;
  244. json["uint"] = 1U;
  245. json["float"] = 1.0;
  246. json["boolean"] = true;
  247. json["string"] = "string";
  248. json["array"].push_back(0);
  249. json["array"].push_back(1);
  250. json["array"].push_back(json);
  251. std::set<std::string> expected
  252. {
  253. "/null - null - null",
  254. "/int - number_integer - -1",
  255. "/uint - number_unsigned - 1",
  256. "/float - number_float - 1.0",
  257. "/boolean - boolean - true",
  258. "/string - string - \"string\"",
  259. "/array/0 - number_integer - 0",
  260. "/array/1 - number_integer - 1",
  261. "/array/2/null - null - null",
  262. "/array/2/int - number_integer - -1",
  263. "/array/2/uint - number_unsigned - 1",
  264. "/array/2/float - number_float - 1.0",
  265. "/array/2/boolean - boolean - true",
  266. "/array/2/string - string - \"string\"",
  267. "/array/2/array/0 - number_integer - 0",
  268. "/array/2/array/1 - number_integer - 1"
  269. };
  270. json.visit(
  271. [&](const json_with_visitor_t::json_pointer & p,
  272. const json_with_visitor_t& j)
  273. {
  274. std::stringstream str;
  275. str << p.to_string() << " - " ;
  276. using value_t = nlohmann::detail::value_t;
  277. switch (j.type())
  278. {
  279. case value_t::object:
  280. str << "object";
  281. break;
  282. case value_t::array:
  283. str << "array";
  284. break;
  285. case value_t::discarded:
  286. str << "discarded";
  287. break;
  288. case value_t::null:
  289. str << "null";
  290. break;
  291. case value_t::string:
  292. str << "string";
  293. break;
  294. case value_t::boolean:
  295. str << "boolean";
  296. break;
  297. case value_t::number_integer:
  298. str << "number_integer";
  299. break;
  300. case value_t::number_unsigned:
  301. str << "number_unsigned";
  302. break;
  303. case value_t::number_float:
  304. str << "number_float";
  305. break;
  306. case value_t::binary:
  307. str << "binary";
  308. break;
  309. default:
  310. str << "error";
  311. break;
  312. }
  313. str << " - " << j.dump();
  314. CHECK(json.at(p) == j);
  315. INFO(str.str());
  316. CHECK(expected.count(str.str()) == 1);
  317. expected.erase(str.str());
  318. }
  319. );
  320. CHECK(expected.empty());
  321. }