json_value_ut.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include "json_value.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/input.h>
  4. using namespace NJson;
  5. Y_UNIT_TEST_SUITE(TJsonValueTest) {
  6. Y_UNIT_TEST(Equal) {
  7. UNIT_ASSERT(1 == TJsonValue(1));
  8. UNIT_ASSERT(TJsonValue(1) == 1);
  9. UNIT_ASSERT(2 != TJsonValue(1));
  10. UNIT_ASSERT(TJsonValue(1) != 2);
  11. }
  12. Y_UNIT_TEST(UndefTest) {
  13. TJsonValue undef;
  14. TJsonValue null(JSON_NULL);
  15. TJsonValue _false(false);
  16. TJsonValue zeroInt(0);
  17. TJsonValue zeroDouble(0.0);
  18. TJsonValue emptyStr("");
  19. TJsonValue emptyArray(JSON_ARRAY);
  20. TJsonValue emptyMap(JSON_MAP);
  21. UNIT_ASSERT(!undef.IsDefined());
  22. UNIT_ASSERT(!null.IsDefined()); // json NULL is undefined too!
  23. UNIT_ASSERT(_false.IsDefined());
  24. UNIT_ASSERT(zeroInt.IsDefined());
  25. UNIT_ASSERT(zeroDouble.IsDefined());
  26. UNIT_ASSERT(emptyStr.IsDefined());
  27. UNIT_ASSERT(emptyArray.IsDefined());
  28. UNIT_ASSERT(emptyMap.IsDefined());
  29. UNIT_ASSERT(undef == TJsonValue());
  30. UNIT_ASSERT(undef != null);
  31. UNIT_ASSERT(undef != _false);
  32. UNIT_ASSERT(undef != zeroInt);
  33. UNIT_ASSERT(undef != zeroDouble);
  34. UNIT_ASSERT(undef != emptyStr);
  35. UNIT_ASSERT(undef != emptyArray);
  36. UNIT_ASSERT(undef != emptyMap);
  37. }
  38. Y_UNIT_TEST(DefaultCompareTest) {
  39. {
  40. TJsonValue lhs;
  41. TJsonValue rhs;
  42. UNIT_ASSERT(lhs == rhs);
  43. UNIT_ASSERT(rhs == lhs);
  44. }
  45. {
  46. TJsonValue lhs;
  47. TJsonValue rhs(JSON_NULL);
  48. UNIT_ASSERT(lhs != rhs);
  49. UNIT_ASSERT(rhs != lhs);
  50. }
  51. }
  52. Y_UNIT_TEST(NullCompareTest) {
  53. TJsonValue lhs(JSON_NULL);
  54. TJsonValue rhs(JSON_NULL);
  55. UNIT_ASSERT(lhs == rhs);
  56. UNIT_ASSERT(rhs == lhs);
  57. }
  58. Y_UNIT_TEST(StringCompareTest) {
  59. {
  60. TJsonValue lhs(JSON_STRING);
  61. TJsonValue rhs(JSON_STRING);
  62. UNIT_ASSERT(lhs == rhs);
  63. UNIT_ASSERT(rhs == lhs);
  64. }
  65. {
  66. TJsonValue lhs("");
  67. TJsonValue rhs("");
  68. UNIT_ASSERT(lhs == rhs);
  69. UNIT_ASSERT(rhs == lhs);
  70. }
  71. {
  72. TJsonValue lhs("abc");
  73. TJsonValue rhs("abc");
  74. UNIT_ASSERT(lhs == rhs);
  75. UNIT_ASSERT(rhs == lhs);
  76. }
  77. {
  78. TJsonValue lhs("1");
  79. TJsonValue rhs(1);
  80. UNIT_ASSERT(lhs != rhs);
  81. UNIT_ASSERT(rhs != lhs);
  82. }
  83. }
  84. Y_UNIT_TEST(ArrayCompareTest) {
  85. {
  86. TJsonValue lhs(JSON_ARRAY);
  87. TJsonValue rhs(JSON_ARRAY);
  88. UNIT_ASSERT(lhs == rhs);
  89. UNIT_ASSERT(rhs == lhs);
  90. }
  91. {
  92. TJsonValue lhs;
  93. TJsonValue rhs;
  94. lhs.AppendValue(TJsonValue());
  95. UNIT_ASSERT(lhs != rhs);
  96. UNIT_ASSERT(rhs != lhs);
  97. }
  98. {
  99. TJsonValue lhs;
  100. TJsonValue rhs;
  101. lhs.AppendValue(1);
  102. lhs.AppendValue("2");
  103. lhs.AppendValue(3.0);
  104. lhs.AppendValue(TJsonValue());
  105. lhs.AppendValue(TJsonValue(JSON_NULL));
  106. rhs.AppendValue(1);
  107. rhs.AppendValue("2");
  108. rhs.AppendValue(3.0);
  109. rhs.AppendValue(TJsonValue());
  110. rhs.AppendValue(TJsonValue(JSON_NULL));
  111. UNIT_ASSERT(lhs == rhs);
  112. UNIT_ASSERT(rhs == lhs);
  113. }
  114. {
  115. TJsonValue lhs;
  116. TJsonValue rhs;
  117. lhs.AppendValue(1);
  118. rhs.AppendValue("1");
  119. UNIT_ASSERT(lhs != rhs);
  120. UNIT_ASSERT(rhs != lhs);
  121. }
  122. }
  123. Y_UNIT_TEST(CompareTest) {
  124. {
  125. TJsonValue lhs;
  126. lhs.InsertValue("null value", TJsonValue(JSON_NULL));
  127. lhs.InsertValue("int key", TJsonValue(10));
  128. lhs.InsertValue("double key", TJsonValue(11.11));
  129. lhs.InsertValue("string key", TJsonValue("string"));
  130. TJsonValue array;
  131. array.AppendValue(1);
  132. array.AppendValue(2);
  133. array.AppendValue(3);
  134. array.AppendValue("string");
  135. lhs.InsertValue("array", array);
  136. lhs.InsertValue("bool key", TJsonValue(true));
  137. TJsonValue rhs;
  138. rhs = lhs;
  139. UNIT_ASSERT(lhs == rhs);
  140. UNIT_ASSERT(rhs == lhs);
  141. }
  142. {
  143. // Insert keys in different orders
  144. const int NUM_KEYS = 1000;
  145. TJsonValue lhs;
  146. for (int i = 0; i < NUM_KEYS; ++i)
  147. lhs.InsertValue(ToString(i), i);
  148. TJsonValue rhs;
  149. for (int i = 0; i < NUM_KEYS; i += 2)
  150. rhs.InsertValue(ToString(i), i);
  151. for (int i = 1; i < NUM_KEYS; i += 2)
  152. rhs.InsertValue(ToString(i), i);
  153. UNIT_ASSERT(lhs == rhs);
  154. UNIT_ASSERT(rhs == lhs);
  155. }
  156. {
  157. TJsonValue lhs;
  158. lhs.InsertValue("null value", TJsonValue(JSON_NULL));
  159. lhs.InsertValue("int key", TJsonValue(10));
  160. lhs.InsertValue("double key", TJsonValue(11.11));
  161. lhs.InsertValue("string key", TJsonValue("string"));
  162. TJsonValue array;
  163. array.AppendValue(1);
  164. array.AppendValue(2);
  165. array.AppendValue(3);
  166. array.AppendValue("string");
  167. lhs.InsertValue("array", array);
  168. lhs.InsertValue("bool key", TJsonValue(true));
  169. TJsonValue rhs;
  170. rhs.InsertValue("null value", TJsonValue(JSON_NULL));
  171. rhs.InsertValue("int key", TJsonValue(10));
  172. rhs.InsertValue("double key", TJsonValue(11.11));
  173. rhs.InsertValue("string key", TJsonValue("string"));
  174. rhs.InsertValue("bool key", TJsonValue(true));
  175. UNIT_ASSERT(lhs != rhs);
  176. UNIT_ASSERT(rhs != lhs);
  177. }
  178. }
  179. Y_UNIT_TEST(SwapTest) {
  180. {
  181. TJsonValue lhs;
  182. lhs.InsertValue("a", "b");
  183. TJsonValue lhsCopy = lhs;
  184. TJsonValue rhs(JSON_NULL);
  185. TJsonValue rhsCopy = rhs;
  186. UNIT_ASSERT(lhs == lhsCopy);
  187. UNIT_ASSERT(rhs == rhsCopy);
  188. lhs.Swap(rhs);
  189. UNIT_ASSERT(rhs == lhsCopy);
  190. UNIT_ASSERT(lhs == rhsCopy);
  191. lhs.Swap(rhs);
  192. UNIT_ASSERT(lhs == lhsCopy);
  193. UNIT_ASSERT(rhs == rhsCopy);
  194. }
  195. }
  196. Y_UNIT_TEST(GetValueByPathTest) {
  197. {
  198. TJsonValue lhs;
  199. TJsonValue first;
  200. TJsonValue second;
  201. TJsonValue last;
  202. first.InsertValue("e", "f");
  203. second.InsertValue("c", first);
  204. last.InsertValue("a", second);
  205. lhs.InsertValue("l", last);
  206. TJsonValue result;
  207. UNIT_ASSERT(lhs.GetValueByPath("l/a/c/e", result, '/'));
  208. UNIT_ASSERT(result.GetStringRobust() == "f");
  209. UNIT_ASSERT(!lhs.GetValueByPath("l/a/c/se", result, '/'));
  210. UNIT_ASSERT(lhs.GetValueByPath("l/a/c", result, '/'));
  211. UNIT_ASSERT(result.GetStringRobust() == "{\"e\":\"f\"}");
  212. // faster TStringBuf version
  213. UNIT_ASSERT_EQUAL(*lhs.GetValueByPath("l", '/'), last);
  214. UNIT_ASSERT_EQUAL(*lhs.GetValueByPath("l/a", '/'), second);
  215. UNIT_ASSERT_EQUAL(*lhs.GetValueByPath("l/a/c", '/'), first);
  216. UNIT_ASSERT_EQUAL(*lhs.GetValueByPath("l.a.c.e", '.'), "f");
  217. UNIT_ASSERT_EQUAL(lhs.GetValueByPath("l/a/c/e/x", '/'), NULL);
  218. UNIT_ASSERT_EQUAL(lhs.GetValueByPath("a/c/e/x", '/'), NULL);
  219. UNIT_ASSERT_EQUAL(lhs.GetValueByPath("nokey", '/'), NULL);
  220. UNIT_ASSERT_EQUAL(*lhs.GetValueByPath("", '/'), lhs); // itself
  221. TJsonValue array;
  222. TJsonValue third;
  223. array[0] = first;
  224. array[1] = second;
  225. third["t"] = array;
  226. UNIT_ASSERT(array.GetValueByPath("[0].e", result));
  227. UNIT_ASSERT(result.GetStringRobust() == "f");
  228. UNIT_ASSERT(third.GetValueByPath("t.[0].e", result));
  229. UNIT_ASSERT(result.GetStringRobust() == "f");
  230. UNIT_ASSERT(third.GetValueByPath("t.[1].c.e", result));
  231. UNIT_ASSERT(result.GetStringRobust() == "f");
  232. UNIT_ASSERT(!third.GetValueByPath("t.[2]", result));
  233. UNIT_ASSERT(third.SetValueByPath("t.[2]", "g"));
  234. UNIT_ASSERT(third.GetValueByPath("t.[2]", result));
  235. UNIT_ASSERT(result.GetStringRobust() == "g");
  236. UNIT_ASSERT(lhs.SetValueByPath("l/a/c/se", "h", '/'));
  237. UNIT_ASSERT(lhs.GetValueByPath("l/a/c/se", result, '/'));
  238. UNIT_ASSERT(result.GetStringRobust() == "h");
  239. }
  240. }
  241. Y_UNIT_TEST(GetValueByPathConstTest) {
  242. TJsonValue lhs;
  243. TJsonValue first;
  244. TJsonValue second;
  245. TJsonValue last;
  246. first.InsertValue("e", "f");
  247. second.InsertValue("c", first);
  248. last.InsertValue("a", second);
  249. lhs.InsertValue("l", last);
  250. {
  251. const TJsonValue* result = lhs.GetValueByPath("l", '/');
  252. UNIT_ASSERT_EQUAL(*result, last);
  253. }
  254. {
  255. const TJsonValue* result = lhs.GetValueByPath("l/a", '/');
  256. UNIT_ASSERT_EQUAL(*result, second);
  257. }
  258. {
  259. const TJsonValue* result = lhs.GetValueByPath("l/a/c", '/');
  260. UNIT_ASSERT_EQUAL(*result, first);
  261. }
  262. {
  263. const TJsonValue* result = lhs.GetValueByPath("l.a.c.e", '.');
  264. UNIT_ASSERT_EQUAL(*result, "f");
  265. }
  266. {
  267. const TJsonValue* result = lhs.GetValueByPath("l/a/c/e/x", '/');
  268. UNIT_ASSERT_EQUAL(result, nullptr);
  269. }
  270. {
  271. const TJsonValue* result = lhs.GetValueByPath("a/c/e/x", '/');
  272. UNIT_ASSERT_EQUAL(result, nullptr);
  273. }
  274. {
  275. const TJsonValue* result = lhs.GetValueByPath("nokey", '/');
  276. UNIT_ASSERT_EQUAL(result, nullptr);
  277. }
  278. {
  279. const TJsonValue* result = lhs.GetValueByPath("", '/');
  280. UNIT_ASSERT_EQUAL(*result, lhs); // itself
  281. }
  282. TJsonValue array;
  283. TJsonValue third;
  284. array[0] = first;
  285. array[1] = second;
  286. third["t"] = array;
  287. UNIT_ASSERT(array.GetValueByPath("[0].e", '.')->GetStringRobust() == "f");
  288. UNIT_ASSERT(third.GetValueByPath("t.[0].e", '.')->GetStringRobust() == "f");
  289. UNIT_ASSERT(third.GetValueByPath("t.[1].c.e", '.')->GetStringRobust() == "f");
  290. }
  291. Y_UNIT_TEST(EraseValueFromArray) {
  292. {
  293. TJsonValue vec;
  294. vec.AppendValue(TJsonValue(0));
  295. vec.AppendValue(TJsonValue(1));
  296. vec.AppendValue(TJsonValue("2"));
  297. vec.AppendValue(TJsonValue("3.14"));
  298. TJsonValue vec1;
  299. vec1.AppendValue(TJsonValue(0));
  300. vec1.AppendValue(TJsonValue("2"));
  301. vec1.AppendValue(TJsonValue("3.14"));
  302. TJsonValue vec2;
  303. vec2.AppendValue(TJsonValue(0));
  304. vec2.AppendValue(TJsonValue("2"));
  305. TJsonValue vec3;
  306. vec3.AppendValue(TJsonValue("2"));
  307. TJsonValue vec4(JSON_ARRAY);
  308. UNIT_ASSERT(vec.IsArray());
  309. UNIT_ASSERT(vec.GetArray().size() == 4);
  310. vec.EraseValue(1);
  311. UNIT_ASSERT(vec.GetArray().size() == 3);
  312. UNIT_ASSERT(vec == vec1);
  313. vec.EraseValue(2);
  314. UNIT_ASSERT(vec.GetArray().size() == 2);
  315. UNIT_ASSERT(vec == vec2);
  316. vec.EraseValue(0);
  317. UNIT_ASSERT(vec.GetArray().size() == 1);
  318. UNIT_ASSERT(vec == vec3);
  319. vec.EraseValue(0);
  320. UNIT_ASSERT(vec.GetArray().size() == 0);
  321. UNIT_ASSERT(vec == vec4);
  322. }
  323. }
  324. Y_UNIT_TEST(NonConstMethodsTest) {
  325. {
  326. TJsonValue src;
  327. TJsonValue value1;
  328. value1.AppendValue(1);
  329. value1.AppendValue(2);
  330. src.InsertValue("key", value1);
  331. src.InsertValue("key1", "HI!");
  332. TJsonValue dst;
  333. TJsonValue value2;
  334. value2.AppendValue(1);
  335. value2.AppendValue(2);
  336. value2.AppendValue(3);
  337. dst.InsertValue("key", value2);
  338. src.GetValueByPath("key", '.')->AppendValue(3);
  339. src.EraseValue("key1");
  340. UNIT_ASSERT(src == dst);
  341. dst.GetValueByPath("key", '.')->EraseValue(0);
  342. UNIT_ASSERT(src != dst);
  343. src.GetValueByPath("key", '.')->EraseValue(0);
  344. UNIT_ASSERT(src == dst);
  345. }
  346. {
  347. TJsonValue src;
  348. TJsonValue value1;
  349. TJsonValue arr1;
  350. value1.InsertValue("key", "value");
  351. arr1.AppendValue(value1);
  352. arr1.AppendValue(value1);
  353. arr1.AppendValue(value1);
  354. src.InsertValue("arr", arr1);
  355. TJsonValue dst;
  356. TJsonValue value2;
  357. TJsonValue arr2;
  358. value2.InsertValue("key", "value");
  359. value2.InsertValue("yek", "eulav");
  360. arr2.AppendValue(value2);
  361. arr2.AppendValue(value2);
  362. arr2.AppendValue(value2);
  363. arr2.AppendValue(value2);
  364. dst.InsertValue("arr", arr2);
  365. src["arr"].AppendValue(value1);
  366. for (auto& node : src["arr"].GetArraySafe()) {
  367. node.InsertValue("yek", "eulav");
  368. }
  369. UNIT_ASSERT(src == dst);
  370. }
  371. {
  372. TJsonValue src;
  373. TJsonValue value1;
  374. TJsonValue arr1;
  375. value1.InsertValue("key", "value");
  376. arr1.AppendValue(value1);
  377. arr1.AppendValue(value1);
  378. arr1.AppendValue(value1);
  379. src.InsertValue("arr", arr1);
  380. TJsonValue dst;
  381. TJsonValue value2;
  382. TJsonValue arr2;
  383. value2.InsertValue("key", "value");
  384. value2.InsertValue("yek", "eulav");
  385. arr2.AppendValue(value2);
  386. arr2.AppendValue(value2);
  387. arr2.AppendValue(value2);
  388. arr2.AppendValue(value2);
  389. dst.InsertValue("arr", arr2);
  390. src["arr"].AppendValue(value1);
  391. for (auto& node : src.GetValueByPath("arr", '.')->GetArraySafe()) {
  392. node.InsertValue("yek", "eulav");
  393. }
  394. UNIT_ASSERT(src == dst);
  395. }
  396. {
  397. TJsonValue json;
  398. json.InsertValue("key", "value");
  399. try {
  400. json.GetArraySafe();
  401. UNIT_ASSERT(false);
  402. } catch (const TJsonException&) {
  403. }
  404. const TJsonValue constJson(json);
  405. try {
  406. constJson.GetArray();
  407. } catch (...) {
  408. UNIT_ASSERT(false);
  409. }
  410. }
  411. {
  412. // Check non-const GetArraySafe()
  413. TJsonValue json{JSON_ARRAY};
  414. json.GetArraySafe().push_back(TJsonValue{"foo"});
  415. TJsonValue expectedJson;
  416. expectedJson.AppendValue(TJsonValue{"foo"});
  417. UNIT_ASSERT(json == expectedJson);
  418. TJsonValue::TArray jsonArray = std::move(json.GetArraySafe());
  419. TJsonValue::TArray expectedArray = {TJsonValue{"foo"}};
  420. UNIT_ASSERT(jsonArray == expectedArray);
  421. }
  422. {
  423. // Check non-const GetMap()
  424. TJsonValue json{JSON_MAP};
  425. json.GetMapSafe()["foo"] = "bar";
  426. TJsonValue expectedJson;
  427. expectedJson.InsertValue("foo", "bar");
  428. UNIT_ASSERT(json == expectedJson);
  429. TJsonValue::TMapType jsonMap = std::move(json.GetMapSafe());
  430. TJsonValue::TMapType expectedMap = {{"foo", TJsonValue{"bar"}}};
  431. UNIT_ASSERT(jsonMap == expectedMap);
  432. }
  433. }
  434. Y_UNIT_TEST(NonexistentFieldAccessTest) {
  435. {
  436. TJsonValue json;
  437. json.InsertValue("some", "key");
  438. UNIT_ASSERT(!json["some"]["weird"]["access"]["sequence"].Has("value"));
  439. UNIT_ASSERT(!json["some"]["weird"]["access"]["sequence"].IsDefined());
  440. UNIT_ASSERT(json["some"].GetType() == JSON_MAP);
  441. }
  442. }
  443. Y_UNIT_TEST(DefaultValuesTest) {
  444. {
  445. TJsonValue json;
  446. json.InsertValue("some", "key");
  447. json.InsertValue("existing", 1.2);
  448. UNIT_ASSERT_VALUES_EQUAL(json["existing"].GetDoubleSafe(), 1.2);
  449. UNIT_ASSERT_VALUES_EQUAL(json["existing"].GetDoubleSafe(15), 1.2);
  450. UNIT_ASSERT_EXCEPTION(json["some"].GetUIntegerSafe(), yexception);
  451. UNIT_ASSERT_EXCEPTION(json["some"].GetUIntegerSafe(12), yexception);
  452. UNIT_ASSERT_EXCEPTION(json["nonexistent"].GetUIntegerSafe(), yexception);
  453. UNIT_ASSERT_VALUES_EQUAL(json["nonexistent"].GetUIntegerSafe(12), 12);
  454. UNIT_ASSERT_VALUES_EQUAL(json["nonexistent"]["more_nonexistent"].GetUIntegerSafe(12), 12);
  455. json.InsertValue("map", TJsonValue(JSON_MAP));
  456. UNIT_ASSERT_VALUES_EQUAL(json["map"]["nonexistent"].GetUIntegerSafe(12), 12);
  457. }
  458. }
  459. Y_UNIT_TEST(GetArrayPointerInArrayTest) {
  460. TJsonValue outer;
  461. {
  462. TJsonValue json;
  463. json.AppendValue(1);
  464. json.AppendValue(2);
  465. json.AppendValue(3);
  466. outer.AppendValue(json);
  467. }
  468. const TJsonValue::TArray* array = nullptr;
  469. GetArrayPointer(outer, 0, &array);
  470. UNIT_ASSERT_VALUES_EQUAL((*array)[1], 2);
  471. }
  472. Y_UNIT_TEST(GetArrayPointerInMapTest) {
  473. TJsonValue outer;
  474. {
  475. TJsonValue json;
  476. json.AppendValue(1);
  477. json.AppendValue(2);
  478. json.AppendValue(3);
  479. outer.InsertValue("x", json);
  480. }
  481. const TJsonValue::TArray* array = nullptr;
  482. GetArrayPointer(outer, "x", &array);
  483. UNIT_ASSERT_VALUES_EQUAL((*array)[1], 2);
  484. }
  485. Y_UNIT_TEST(GetMapPointerInArrayTest) {
  486. TJsonValue outer;
  487. {
  488. TJsonValue json;
  489. json.InsertValue("a", 1);
  490. json.InsertValue("b", 2);
  491. json.InsertValue("c", 3);
  492. outer.AppendValue(json);
  493. }
  494. const TJsonValue::TMapType* map = nullptr;
  495. GetMapPointer(outer, 0, &map);
  496. UNIT_ASSERT_VALUES_EQUAL((*map).at("b"), 2);
  497. }
  498. Y_UNIT_TEST(GetMapPointerInMapTest) {
  499. TJsonValue outer;
  500. {
  501. TJsonValue json;
  502. json.InsertValue("a", 1);
  503. json.InsertValue("b", 2);
  504. json.InsertValue("c", 3);
  505. outer.InsertValue("x", json);
  506. }
  507. const TJsonValue::TMapType* map = nullptr;
  508. GetMapPointer(outer, "x", &map);
  509. UNIT_ASSERT_VALUES_EQUAL((*map).at("b"), 2);
  510. }
  511. Y_UNIT_TEST(GetIntegerRobustBignumStringTest) {
  512. TString value = "1626862681464633683";
  513. TJsonValue json(value);
  514. UNIT_ASSERT_VALUES_EQUAL(json.GetUIntegerRobust(), FromString<ui64>(value));
  515. UNIT_ASSERT_VALUES_EQUAL(json.GetIntegerRobust(), FromString<i64>(value));
  516. }
  517. Y_UNIT_TEST(MoveSubpartToSelf) {
  518. TJsonValue json;
  519. json[0] = "testing 0";
  520. json[1] = "testing 1";
  521. json[2] = "testing 2";
  522. json = std::move(json[1]);
  523. UNIT_ASSERT_VALUES_EQUAL(json.GetString(), "testing 1");
  524. const char* longTestString =
  525. "Testing TJsonValue& operator=(TJsonValue&&) subpart self moving "
  526. "after TJsonValue was constrcuted from TString&&.";
  527. json["hello"] = TString{longTestString};
  528. json = std::move(json["hello"]);
  529. UNIT_ASSERT_VALUES_EQUAL(json.GetString(), longTestString);
  530. }
  531. Y_UNIT_TEST(TJsonArrayMapConstructor) {
  532. TJsonMap emptyMap;
  533. UNIT_ASSERT_VALUES_EQUAL(emptyMap.GetType(), JSON_MAP);
  534. UNIT_ASSERT_VALUES_EQUAL(emptyMap.GetMapSafe().size(), 0);
  535. TJsonArray emptyArray;
  536. UNIT_ASSERT_VALUES_EQUAL(emptyArray.GetType(), JSON_ARRAY);
  537. UNIT_ASSERT_VALUES_EQUAL(emptyArray.GetArraySafe().size(), 0);
  538. TJsonMap filled = {
  539. {"1", 1},
  540. {"2", "2"},
  541. {"3", TJsonArray{3}},
  542. {"4", TJsonMap{{"5", 5}}},
  543. };
  544. UNIT_ASSERT_VALUES_EQUAL(filled.GetType(), JSON_MAP);
  545. UNIT_ASSERT_VALUES_EQUAL(filled["1"], TJsonValue{1});
  546. UNIT_ASSERT_VALUES_EQUAL(filled["2"], TJsonValue{"2"});
  547. UNIT_ASSERT_VALUES_EQUAL(filled["3"].GetArraySafe().size(), 1);
  548. UNIT_ASSERT_VALUES_EQUAL(filled["3"][0], TJsonValue{3});
  549. UNIT_ASSERT_VALUES_EQUAL(filled["4"].GetMapSafe().size(), 1);
  550. UNIT_ASSERT_VALUES_EQUAL(filled["4"]["5"], TJsonValue{5});
  551. }
  552. } // TJsonValueTest