unit-serialization.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++ (test suite)
  4. | | |__ | | | | | | version 3.7.3
  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 "doctest_compatibility.h"
  26. #include <nlohmann/json.hpp>
  27. using nlohmann::json;
  28. #include <sstream>
  29. #include <iomanip>
  30. TEST_CASE("serialization")
  31. {
  32. SECTION("operator<<")
  33. {
  34. SECTION("no given width")
  35. {
  36. std::stringstream ss;
  37. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  38. ss << j;
  39. CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
  40. }
  41. SECTION("given width")
  42. {
  43. std::stringstream ss;
  44. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  45. ss << std::setw(4) << j;
  46. CHECK(ss.str() ==
  47. "[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
  48. }
  49. SECTION("given fill")
  50. {
  51. std::stringstream ss;
  52. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  53. ss << std::setw(1) << std::setfill('\t') << j;
  54. CHECK(ss.str() ==
  55. "[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]");
  56. }
  57. }
  58. SECTION("operator>>")
  59. {
  60. SECTION("no given width")
  61. {
  62. std::stringstream ss;
  63. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  64. j >> ss;
  65. CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
  66. }
  67. SECTION("given width")
  68. {
  69. std::stringstream ss;
  70. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  71. ss.width(4);
  72. j >> ss;
  73. CHECK(ss.str() ==
  74. "[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
  75. }
  76. SECTION("given fill")
  77. {
  78. std::stringstream ss;
  79. json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
  80. ss.width(1);
  81. ss.fill('\t');
  82. j >> ss;
  83. CHECK(ss.str() ==
  84. "[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]");
  85. }
  86. }
  87. SECTION("dump")
  88. {
  89. SECTION("invalid character")
  90. {
  91. json j = "ä\xA9ü";
  92. CHECK_THROWS_AS(j.dump(), json::type_error&);
  93. CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9");
  94. CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
  95. CHECK_THROWS_WITH(j.dump(1, ' ', false, json::error_handler_t::strict), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9");
  96. CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"äü\"");
  97. CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"ä\xEF\xBF\xBDü\"");
  98. CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"\\u00e4\\ufffd\\u00fc\"");
  99. }
  100. SECTION("ending with incomplete character")
  101. {
  102. json j = "123\xC2";
  103. CHECK_THROWS_AS(j.dump(), json::type_error&);
  104. CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2");
  105. CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
  106. CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123\"");
  107. CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\"");
  108. CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd\"");
  109. }
  110. SECTION("unexpected character")
  111. {
  112. json j = "123\xF1\xB0\x34\x35\x36";
  113. CHECK_THROWS_AS(j.dump(), json::type_error&);
  114. CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34");
  115. CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
  116. CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123456\"");
  117. CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\x34\x35\x36\"");
  118. CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd456\"");
  119. }
  120. SECTION("U+FFFD Substitution of Maximal Subparts")
  121. {
  122. // Some tests (mostly) from
  123. // https://www.unicode.org/versions/Unicode11.0.0/ch03.pdf
  124. // Section 3.9 -- U+FFFD Substitution of Maximal Subparts
  125. auto test = [&](std::string const & input, std::string const & expected)
  126. {
  127. json j = input;
  128. CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"" + expected + "\"");
  129. };
  130. test("\xC2", "\\ufffd");
  131. test("\xC2\x41\x42", "\\ufffd" "\x41" "\x42");
  132. test("\xC2\xF4", "\\ufffd" "\\ufffd");
  133. test("\xF0\x80\x80\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  134. test("\xF1\x80\x80\x41", "\\ufffd" "\x41");
  135. test("\xF2\x80\x80\x41", "\\ufffd" "\x41");
  136. test("\xF3\x80\x80\x41", "\\ufffd" "\x41");
  137. test("\xF4\x80\x80\x41", "\\ufffd" "\x41");
  138. test("\xF5\x80\x80\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  139. test("\xF0\x90\x80\x41", "\\ufffd" "\x41");
  140. test("\xF1\x90\x80\x41", "\\ufffd" "\x41");
  141. test("\xF2\x90\x80\x41", "\\ufffd" "\x41");
  142. test("\xF3\x90\x80\x41", "\\ufffd" "\x41");
  143. test("\xF4\x90\x80\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  144. test("\xF5\x90\x80\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  145. test("\xC0\xAF\xE0\x80\xBF\xF0\x81\x82\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  146. test("\xED\xA0\x80\xED\xBF\xBF\xED\xAF\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  147. test("\xF4\x91\x92\x93\xFF\x41\x80\xBF\x42", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41" "\\ufffd""\\ufffd" "\x42");
  148. test("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41");
  149. }
  150. }
  151. SECTION("to_string")
  152. {
  153. auto test = [&](std::string const & input, std::string const & expected)
  154. {
  155. using std::to_string;
  156. json j = input;
  157. CHECK(to_string(j) == "\"" + expected + "\"");
  158. };
  159. test("{\"x\":5,\"y\":6}", "{\\\"x\\\":5,\\\"y\\\":6}");
  160. test("{\"x\":[10,null,null,null]}", "{\\\"x\\\":[10,null,null,null]}");
  161. test("test", "test");
  162. test("[3,\"false\",false]", "[3,\\\"false\\\",false]");
  163. }
  164. }
  165. TEST_CASE_TEMPLATE("serialization for extreme integer values", T, int32_t, uint32_t, int64_t, uint64_t)
  166. {
  167. SECTION("minimum")
  168. {
  169. constexpr auto minimum = (std::numeric_limits<T>::min)();
  170. json j = minimum;
  171. CHECK(j.dump() == std::to_string(minimum));
  172. }
  173. SECTION("maximum")
  174. {
  175. constexpr auto maximum = (std::numeric_limits<T>::max)();
  176. json j = maximum;
  177. CHECK(j.dump() == std::to_string(maximum));
  178. }
  179. }
  180. TEST_CASE("dump with binary values")
  181. {
  182. auto binary = json::binary({1, 2, 3, 4});
  183. auto binary_empty = json::binary({});
  184. auto binary_with_subtype = json::binary({1, 2, 3, 4}, 128);
  185. auto binary_empty_with_subtype = json::binary({}, 128);
  186. json object = {{"key", binary}};
  187. json object_empty = {{"key", binary_empty}};
  188. json object_with_subtype = {{"key", binary_with_subtype}};
  189. json object_empty_with_subtype = {{"key", binary_empty_with_subtype}};
  190. json array = {"value", 1, binary};
  191. json array_empty = {"value", 1, binary_empty};
  192. json array_with_subtype = {"value", 1, binary_with_subtype};
  193. json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype};
  194. SECTION("normal")
  195. {
  196. CHECK(binary.dump() == "{\"bytes\":[1,2,3,4],\"subtype\":null}");
  197. CHECK(binary_empty.dump() == "{\"bytes\":[],\"subtype\":null}");
  198. CHECK(binary_with_subtype.dump() == "{\"bytes\":[1,2,3,4],\"subtype\":128}");
  199. CHECK(binary_empty_with_subtype.dump() == "{\"bytes\":[],\"subtype\":128}");
  200. CHECK(object.dump() == "{\"key\":{\"bytes\":[1,2,3,4],\"subtype\":null}}");
  201. CHECK(object_empty.dump() == "{\"key\":{\"bytes\":[],\"subtype\":null}}");
  202. CHECK(object_with_subtype.dump() == "{\"key\":{\"bytes\":[1,2,3,4],\"subtype\":128}}");
  203. CHECK(object_empty_with_subtype.dump() == "{\"key\":{\"bytes\":[],\"subtype\":128}}");
  204. CHECK(array.dump() == "[\"value\",1,{\"bytes\":[1,2,3,4],\"subtype\":null}]");
  205. CHECK(array_empty.dump() == "[\"value\",1,{\"bytes\":[],\"subtype\":null}]");
  206. CHECK(array_with_subtype.dump() == "[\"value\",1,{\"bytes\":[1,2,3,4],\"subtype\":128}]");
  207. CHECK(array_empty_with_subtype.dump() == "[\"value\",1,{\"bytes\":[],\"subtype\":128}]");
  208. }
  209. SECTION("pretty-printed")
  210. {
  211. CHECK(binary.dump(4) == "{\n"
  212. " \"bytes\": [1, 2, 3, 4],\n"
  213. " \"subtype\": null\n"
  214. "}");
  215. CHECK(binary_empty.dump(4) == "{\n"
  216. " \"bytes\": [],\n"
  217. " \"subtype\": null\n"
  218. "}");
  219. CHECK(binary_with_subtype.dump(4) == "{\n"
  220. " \"bytes\": [1, 2, 3, 4],\n"
  221. " \"subtype\": 128\n"
  222. "}");
  223. CHECK(binary_empty_with_subtype.dump(4) == "{\n"
  224. " \"bytes\": [],\n"
  225. " \"subtype\": 128\n"
  226. "}");
  227. CHECK(object.dump(4) == "{\n"
  228. " \"key\": {\n"
  229. " \"bytes\": [1, 2, 3, 4],\n"
  230. " \"subtype\": null\n"
  231. " }\n"
  232. "}");
  233. CHECK(object_empty.dump(4) == "{\n"
  234. " \"key\": {\n"
  235. " \"bytes\": [],\n"
  236. " \"subtype\": null\n"
  237. " }\n"
  238. "}");
  239. CHECK(object_with_subtype.dump(4) == "{\n"
  240. " \"key\": {\n"
  241. " \"bytes\": [1, 2, 3, 4],\n"
  242. " \"subtype\": 128\n"
  243. " }\n"
  244. "}");
  245. CHECK(object_empty_with_subtype.dump(4) == "{\n"
  246. " \"key\": {\n"
  247. " \"bytes\": [],\n"
  248. " \"subtype\": 128\n"
  249. " }\n"
  250. "}");
  251. CHECK(array.dump(4) == "[\n"
  252. " \"value\",\n"
  253. " 1,\n"
  254. " {\n"
  255. " \"bytes\": [1, 2, 3, 4],\n"
  256. " \"subtype\": null\n"
  257. " }\n"
  258. "]");
  259. CHECK(array_empty.dump(4) == "[\n"
  260. " \"value\",\n"
  261. " 1,\n"
  262. " {\n"
  263. " \"bytes\": [],\n"
  264. " \"subtype\": null\n"
  265. " }\n"
  266. "]");
  267. CHECK(array_with_subtype.dump(4) == "[\n"
  268. " \"value\",\n"
  269. " 1,\n"
  270. " {\n"
  271. " \"bytes\": [1, 2, 3, 4],\n"
  272. " \"subtype\": 128\n"
  273. " }\n"
  274. "]");
  275. CHECK(array_empty_with_subtype.dump(4) == "[\n"
  276. " \"value\",\n"
  277. " 1,\n"
  278. " {\n"
  279. " \"bytes\": [],\n"
  280. " \"subtype\": 128\n"
  281. " }\n"
  282. "]");
  283. }
  284. }