json_reader_ut.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include <library/cpp/json/json_reader.h>
  2. #include <library/cpp/json/json_writer.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/stream/str.h>
  5. using namespace NJson;
  6. class TReformatCallbacks: public TJsonCallbacks {
  7. TJsonWriter& Writer;
  8. public:
  9. TReformatCallbacks(TJsonWriter& writer)
  10. : Writer(writer)
  11. {
  12. }
  13. bool OnBoolean(bool val) override {
  14. Writer.Write(val);
  15. return true;
  16. }
  17. bool OnInteger(long long val) override {
  18. Writer.Write(val);
  19. return true;
  20. }
  21. bool OnUInteger(unsigned long long val) override {
  22. Writer.Write(val);
  23. return true;
  24. }
  25. bool OnString(const TStringBuf& val) override {
  26. Writer.Write(val);
  27. return true;
  28. }
  29. bool OnDouble(double val) override {
  30. Writer.Write(val);
  31. return true;
  32. }
  33. bool OnOpenArray() override {
  34. Writer.OpenArray();
  35. return true;
  36. }
  37. bool OnCloseArray() override {
  38. Writer.CloseArray();
  39. return true;
  40. }
  41. bool OnOpenMap() override {
  42. Writer.OpenArray();
  43. return true;
  44. }
  45. bool OnCloseMap() override {
  46. Writer.CloseArray();
  47. return true;
  48. }
  49. bool OnMapKey(const TStringBuf& val) override {
  50. Writer.Write(val);
  51. return true;
  52. }
  53. };
  54. void GenerateDeepJson(TStringStream& stream, ui64 depth) {
  55. stream << "{\"key\":";
  56. for (ui32 i = 0; i < depth - 1; ++i) {
  57. stream << "[";
  58. }
  59. for (ui32 i = 0; i < depth - 1; ++i) {
  60. stream << "]";
  61. }
  62. stream << "}";
  63. }
  64. Y_UNIT_TEST_SUITE(TJsonReaderTest) {
  65. Y_UNIT_TEST(JsonReformatTest) {
  66. TString data = "{\"null value\": null, \"intkey\": 10, \"double key\": 11.11, \"string key\": \"string\", \"array\": [1,2,3,\"TString\"], \"bool key\": true}";
  67. TString result1, result2;
  68. {
  69. TStringStream in;
  70. in << data;
  71. TStringStream out;
  72. TJsonWriter writer(&out, false);
  73. TReformatCallbacks cb(writer);
  74. ReadJson(&in, &cb);
  75. writer.Flush();
  76. result1 = out.Str();
  77. }
  78. {
  79. TStringStream in;
  80. in << result1;
  81. TStringStream out;
  82. TJsonWriter writer(&out, false);
  83. TReformatCallbacks cb(writer);
  84. ReadJson(&in, &cb);
  85. writer.Flush();
  86. result2 = out.Str();
  87. }
  88. UNIT_ASSERT_VALUES_EQUAL(result1, result2);
  89. }
  90. Y_UNIT_TEST(TJsonEscapedApostrophe) {
  91. TString jsonString = "{ \"foo\" : \"bar\\'buzz\" }";
  92. {
  93. TStringStream in;
  94. in << jsonString;
  95. TStringStream out;
  96. TJsonWriter writer(&out, false);
  97. TReformatCallbacks cb(writer);
  98. UNIT_ASSERT(!ReadJson(&in, &cb));
  99. }
  100. {
  101. TStringStream in;
  102. in << jsonString;
  103. TStringStream out;
  104. TJsonWriter writer(&out, false);
  105. TReformatCallbacks cb(writer);
  106. UNIT_ASSERT(ReadJson(&in, false, true, &cb));
  107. writer.Flush();
  108. UNIT_ASSERT_EQUAL(out.Str(), "[\"foo\",\"bar'buzz\"]");
  109. }
  110. }
  111. Y_UNIT_TEST(TJsonTreeTest) {
  112. TString data = "{\"intkey\": 10, \"double key\": 11.11, \"null value\":null, \"string key\": \"string\", \"array\": [1,2,3,\"TString\"], \"bool key\": true}";
  113. TStringStream in;
  114. in << data;
  115. TJsonValue value;
  116. ReadJsonTree(&in, &value);
  117. UNIT_ASSERT_VALUES_EQUAL(value["intkey"].GetInteger(), 10);
  118. UNIT_ASSERT_DOUBLES_EQUAL(value["double key"].GetDouble(), 11.11, 0.001);
  119. UNIT_ASSERT_VALUES_EQUAL(value["bool key"].GetBoolean(), true);
  120. UNIT_ASSERT_VALUES_EQUAL(value["absent string key"].GetString(), TString(""));
  121. UNIT_ASSERT_VALUES_EQUAL(value["string key"].GetString(), TString("string"));
  122. UNIT_ASSERT_VALUES_EQUAL(value["array"][0].GetInteger(), 1);
  123. UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), 2);
  124. UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), 3);
  125. UNIT_ASSERT_VALUES_EQUAL(value["array"][3].GetString(), TString("TString"));
  126. UNIT_ASSERT(value["null value"].IsNull());
  127. // AsString
  128. UNIT_ASSERT_VALUES_EQUAL(value["intkey"].GetStringRobust(), "10");
  129. UNIT_ASSERT_VALUES_EQUAL(value["double key"].GetStringRobust(), "11.11");
  130. UNIT_ASSERT_VALUES_EQUAL(value["bool key"].GetStringRobust(), "true");
  131. UNIT_ASSERT_VALUES_EQUAL(value["string key"].GetStringRobust(), "string");
  132. UNIT_ASSERT_VALUES_EQUAL(value["array"].GetStringRobust(), "[1,2,3,\"TString\"]");
  133. UNIT_ASSERT_VALUES_EQUAL(value["null value"].GetStringRobust(), "null");
  134. const TJsonValue::TArray* array;
  135. UNIT_ASSERT(GetArrayPointer(value, "array", &array));
  136. UNIT_ASSERT_VALUES_EQUAL(value["array"].GetArray().size(), array->size());
  137. UNIT_ASSERT_VALUES_EQUAL(value["array"][0].GetInteger(), (*array)[0].GetInteger());
  138. UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), (*array)[1].GetInteger());
  139. UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), (*array)[2].GetInteger());
  140. UNIT_ASSERT_VALUES_EQUAL(value["array"][3].GetString(), (*array)[3].GetString());
  141. }
  142. Y_UNIT_TEST(TJsonRomaTest) {
  143. TString data = "{\"test\": [ {\"name\": \"A\"} ]}";
  144. TStringStream in;
  145. in << data;
  146. TJsonValue value;
  147. ReadJsonTree(&in, &value);
  148. UNIT_ASSERT_VALUES_EQUAL(value["test"][0]["name"].GetString(), TString("A"));
  149. }
  150. Y_UNIT_TEST(TJsonReadTreeWithComments) {
  151. {
  152. TString leadingCommentData = "{ // \"test\" : 1 \n}";
  153. {
  154. // No comments allowed
  155. TStringStream in;
  156. in << leadingCommentData;
  157. TJsonValue value;
  158. UNIT_ASSERT(!ReadJsonTree(&in, false, &value));
  159. }
  160. {
  161. // Comments allowed
  162. TStringStream in;
  163. in << leadingCommentData;
  164. TJsonValue value;
  165. UNIT_ASSERT(ReadJsonTree(&in, true, &value));
  166. UNIT_ASSERT(!value.Has("test"));
  167. }
  168. }
  169. {
  170. TString trailingCommentData = "{ \"test1\" : 1 // \"test2\" : 2 \n }";
  171. {
  172. // No comments allowed
  173. TStringStream in;
  174. in << trailingCommentData;
  175. TJsonValue value;
  176. UNIT_ASSERT(!ReadJsonTree(&in, false, &value));
  177. }
  178. {
  179. // Comments allowed
  180. TStringStream in;
  181. in << trailingCommentData;
  182. TJsonValue value;
  183. UNIT_ASSERT(ReadJsonTree(&in, true, &value));
  184. UNIT_ASSERT(value.Has("test1"));
  185. UNIT_ASSERT_EQUAL(value["test1"].GetInteger(), 1);
  186. UNIT_ASSERT(!value.Has("test2"));
  187. }
  188. }
  189. }
  190. Y_UNIT_TEST(TJsonSignedIntegerTest) {
  191. {
  192. TStringStream in;
  193. in << "{ \"test\" : " << Min<i64>() << " }";
  194. TJsonValue value;
  195. UNIT_ASSERT(ReadJsonTree(&in, &value));
  196. UNIT_ASSERT(value.Has("test"));
  197. UNIT_ASSERT(value["test"].IsInteger());
  198. UNIT_ASSERT(!value["test"].IsUInteger());
  199. UNIT_ASSERT_EQUAL(value["test"].GetInteger(), Min<i64>());
  200. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), Min<i64>());
  201. } // Min<i64>()
  202. {
  203. TStringStream in;
  204. in << "{ \"test\" : " << Max<i64>() + 1ull << " }";
  205. TJsonValue value;
  206. UNIT_ASSERT(ReadJsonTree(&in, &value));
  207. UNIT_ASSERT(value.Has("test"));
  208. UNIT_ASSERT(!value["test"].IsInteger());
  209. UNIT_ASSERT(value["test"].IsUInteger());
  210. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), (i64)(Max<i64>() + 1ull));
  211. } // Max<i64>() + 1
  212. }
  213. Y_UNIT_TEST(TJsonUnsignedIntegerTest) {
  214. {
  215. TStringStream in;
  216. in << "{ \"test\" : 1 }";
  217. TJsonValue value;
  218. UNIT_ASSERT(ReadJsonTree(&in, &value));
  219. UNIT_ASSERT(value.Has("test"));
  220. UNIT_ASSERT(value["test"].IsInteger());
  221. UNIT_ASSERT(value["test"].IsUInteger());
  222. UNIT_ASSERT_EQUAL(value["test"].GetInteger(), 1);
  223. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), 1);
  224. UNIT_ASSERT_EQUAL(value["test"].GetUInteger(), 1);
  225. UNIT_ASSERT_EQUAL(value["test"].GetUIntegerRobust(), 1);
  226. } // 1
  227. {
  228. TStringStream in;
  229. in << "{ \"test\" : -1 }";
  230. TJsonValue value;
  231. UNIT_ASSERT(ReadJsonTree(&in, &value));
  232. UNIT_ASSERT(value.Has("test"));
  233. UNIT_ASSERT(value["test"].IsInteger());
  234. UNIT_ASSERT(!value["test"].IsUInteger());
  235. UNIT_ASSERT_EQUAL(value["test"].GetInteger(), -1);
  236. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), -1);
  237. UNIT_ASSERT_EQUAL(value["test"].GetUInteger(), 0);
  238. UNIT_ASSERT_EQUAL(value["test"].GetUIntegerRobust(), static_cast<unsigned long long>(-1));
  239. } // -1
  240. {
  241. TStringStream in;
  242. in << "{ \"test\" : 18446744073709551615 }";
  243. TJsonValue value;
  244. UNIT_ASSERT(ReadJsonTree(&in, &value));
  245. UNIT_ASSERT(value.Has("test"));
  246. UNIT_ASSERT(!value["test"].IsInteger());
  247. UNIT_ASSERT(value["test"].IsUInteger());
  248. UNIT_ASSERT_EQUAL(value["test"].GetInteger(), 0);
  249. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), static_cast<long long>(18446744073709551615ull));
  250. UNIT_ASSERT_EQUAL(value["test"].GetUInteger(), 18446744073709551615ull);
  251. UNIT_ASSERT_EQUAL(value["test"].GetUIntegerRobust(), 18446744073709551615ull);
  252. } // 18446744073709551615
  253. {
  254. TStringStream in;
  255. in << "{ \"test\" : 1.1 }";
  256. TJsonValue value;
  257. UNIT_ASSERT(ReadJsonTree(&in, &value));
  258. UNIT_ASSERT(value.Has("test"));
  259. UNIT_ASSERT(!value["test"].IsInteger());
  260. UNIT_ASSERT(!value["test"].IsUInteger());
  261. UNIT_ASSERT_EQUAL(value["test"].GetInteger(), 0);
  262. UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), static_cast<long long>(1.1));
  263. UNIT_ASSERT_EQUAL(value["test"].GetUInteger(), 0);
  264. UNIT_ASSERT_EQUAL(value["test"].GetUIntegerRobust(), static_cast<unsigned long long>(1.1));
  265. } // 1.1
  266. {
  267. TStringStream in;
  268. in << "{ \"test\" : [1, 18446744073709551615] }";
  269. TJsonValue value;
  270. UNIT_ASSERT(ReadJsonTree(&in, &value));
  271. UNIT_ASSERT(value.Has("test"));
  272. UNIT_ASSERT(value["test"].IsArray());
  273. UNIT_ASSERT_EQUAL(value["test"].GetArray().size(), 2);
  274. UNIT_ASSERT(value["test"][0].IsInteger());
  275. UNIT_ASSERT(value["test"][0].IsUInteger());
  276. UNIT_ASSERT_EQUAL(value["test"][0].GetInteger(), 1);
  277. UNIT_ASSERT_EQUAL(value["test"][0].GetUInteger(), 1);
  278. UNIT_ASSERT(!value["test"][1].IsInteger());
  279. UNIT_ASSERT(value["test"][1].IsUInteger());
  280. UNIT_ASSERT_EQUAL(value["test"][1].GetUInteger(), 18446744073709551615ull);
  281. }
  282. } // TJsonUnsignedIntegerTest
  283. Y_UNIT_TEST(TJsonDoubleTest) {
  284. {
  285. TStringStream in;
  286. in << "{ \"test\" : 1.0 }";
  287. TJsonValue value;
  288. UNIT_ASSERT(ReadJsonTree(&in, &value));
  289. UNIT_ASSERT(value.Has("test"));
  290. UNIT_ASSERT(value["test"].IsDouble());
  291. UNIT_ASSERT_EQUAL(value["test"].GetDouble(), 1.0);
  292. UNIT_ASSERT_EQUAL(value["test"].GetDoubleRobust(), 1.0);
  293. } // 1.0
  294. {
  295. TStringStream in;
  296. in << "{ \"test\" : 1 }";
  297. TJsonValue value;
  298. UNIT_ASSERT(ReadJsonTree(&in, &value));
  299. UNIT_ASSERT(value.Has("test"));
  300. UNIT_ASSERT(value["test"].IsDouble());
  301. UNIT_ASSERT_EQUAL(value["test"].GetDouble(), 1.0);
  302. UNIT_ASSERT_EQUAL(value["test"].GetDoubleRobust(), 1.0);
  303. } // 1
  304. {
  305. TStringStream in;
  306. in << "{ \"test\" : -1 }";
  307. TJsonValue value;
  308. UNIT_ASSERT(ReadJsonTree(&in, &value));
  309. UNIT_ASSERT(value.Has("test"));
  310. UNIT_ASSERT(value["test"].IsDouble());
  311. UNIT_ASSERT_EQUAL(value["test"].GetDouble(), -1.0);
  312. UNIT_ASSERT_EQUAL(value["test"].GetDoubleRobust(), -1.0);
  313. } // -1
  314. {
  315. TStringStream in;
  316. in << "{ \"test\" : " << Max<ui64>() << " }";
  317. TJsonValue value;
  318. UNIT_ASSERT(ReadJsonTree(&in, &value));
  319. UNIT_ASSERT(value.Has("test"));
  320. UNIT_ASSERT(!value["test"].IsDouble());
  321. UNIT_ASSERT_EQUAL(value["test"].GetDouble(), 0.0);
  322. UNIT_ASSERT_EQUAL(value["test"].GetDoubleRobust(), static_cast<double>(Max<ui64>()));
  323. } // Max<ui64>()
  324. } // TJsonDoubleTest
  325. Y_UNIT_TEST(TJsonInvalidTest) {
  326. {
  327. // No exceptions mode.
  328. TStringStream in;
  329. in << "{ \"test\" : }";
  330. TJsonValue value;
  331. UNIT_ASSERT(!ReadJsonTree(&in, &value));
  332. }
  333. {
  334. // Exception throwing mode.
  335. TStringStream in;
  336. in << "{ \"test\" : }";
  337. TJsonValue value;
  338. UNIT_ASSERT_EXCEPTION(ReadJsonTree(&in, &value, true), TJsonException);
  339. }
  340. }
  341. Y_UNIT_TEST(TJsonMemoryLeakTest) {
  342. // after https://clubs.at.yandex-team.ru/stackoverflow/3691
  343. TString s = ".";
  344. NJson::TJsonValue json;
  345. try {
  346. TStringInput in(s);
  347. NJson::ReadJsonTree(&in, &json, true);
  348. } catch (...) {
  349. }
  350. } // TJsonMemoryLeakTest
  351. Y_UNIT_TEST(TJsonDuplicateKeysWithNullValuesTest) {
  352. const TString json = "{\"\":null,\"\":\"\"}";
  353. TStringInput in(json);
  354. NJson::TJsonValue v;
  355. UNIT_ASSERT(ReadJsonTree(&in, &v));
  356. UNIT_ASSERT(v.IsMap());
  357. UNIT_ASSERT_VALUES_EQUAL(1, v.GetMap().size());
  358. UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->first);
  359. UNIT_ASSERT(v.GetMap().begin()->second.IsString());
  360. UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->second.GetString());
  361. }
  362. // Parsing an extremely deep json tree would result in stack overflow.
  363. // Not crashing on one is a good indicator of iterative mode.
  364. Y_UNIT_TEST(TJsonIterativeTest) {
  365. constexpr ui32 brackets = static_cast<ui32>(1e5);
  366. TStringStream jsonStream;
  367. GenerateDeepJson(jsonStream, brackets);
  368. TJsonReaderConfig config;
  369. config.UseIterativeParser = true;
  370. config.MaxDepth = static_cast<ui32>(1e3);
  371. TJsonValue v;
  372. UNIT_ASSERT(!ReadJsonTree(&jsonStream, &config, &v));
  373. }
  374. Y_UNIT_TEST(TJsonMaxDepthTest) {
  375. constexpr ui32 depth = static_cast<ui32>(1e3);
  376. {
  377. TStringStream jsonStream;
  378. GenerateDeepJson(jsonStream, depth);
  379. TJsonReaderConfig config;
  380. config.MaxDepth = depth;
  381. TJsonValue v;
  382. UNIT_ASSERT(ReadJsonTree(&jsonStream, &config, &v));
  383. }
  384. {
  385. TStringStream jsonStream;
  386. GenerateDeepJson(jsonStream, depth);
  387. TJsonReaderConfig config;
  388. config.MaxDepth = depth - 1;
  389. TJsonValue v;
  390. UNIT_ASSERT(!ReadJsonTree(&jsonStream, &config, &v));
  391. }
  392. }
  393. }
  394. static const TString YANDEX_STREAMING_JSON("{\"a\":1}//d{\"b\":2}");
  395. Y_UNIT_TEST_SUITE(TCompareReadJsonFast) {
  396. Y_UNIT_TEST(NoEndl) {
  397. NJson::TJsonValue parsed;
  398. bool success = NJson::ReadJsonTree(YANDEX_STREAMING_JSON, &parsed, false);
  399. bool fast_success = NJson::ReadJsonFastTree(YANDEX_STREAMING_JSON, &parsed, false);
  400. UNIT_ASSERT(success == fast_success);
  401. }
  402. Y_UNIT_TEST(WithEndl) {
  403. NJson::TJsonValue parsed1;
  404. NJson::TJsonValue parsed2;
  405. bool success = NJson::ReadJsonTree(YANDEX_STREAMING_JSON + "\n", &parsed1, false);
  406. bool fast_success = NJson::ReadJsonFastTree(YANDEX_STREAMING_JSON + "\n", &parsed2, false);
  407. UNIT_ASSERT_VALUES_EQUAL(success, fast_success);
  408. }
  409. Y_UNIT_TEST(NoQuotes) {
  410. TString streamingJson = "{a:1}";
  411. NJson::TJsonValue parsed;
  412. bool success = NJson::ReadJsonTree(streamingJson, &parsed, false);
  413. bool fast_success = NJson::ReadJsonFastTree(streamingJson, &parsed, false);
  414. UNIT_ASSERT(success != fast_success);
  415. }
  416. }