scheme_json_ut.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <library/cpp/scheme/scimpl_private.h>
  2. #include <library/cpp/scheme/ut_utils/scheme_ut_utils.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/stream/null.h>
  5. #include <util/string/subst.h>
  6. #include <util/string/util.h>
  7. #include <type_traits>
  8. #include <library/cpp/string_utils/quote/quote.h>
  9. using namespace std::string_view_literals;
  10. Y_UNIT_TEST_SUITE(TSchemeJsonTest) {
  11. Y_UNIT_TEST(TestJson) {
  12. const char* json = "[\n"
  13. " {\n"
  14. " \"url\":\"foo\",\n"
  15. " \"title\":\"bar\",\n"
  16. " \"passages\":[\"foo\", \"bar\"],\n"
  17. " }\n"
  18. "]";
  19. {
  20. const NSc::TValue& v = NSc::TValue::FromJson(json);
  21. UNIT_ASSERT_VALUES_EQUAL("bar", (TStringBuf)v.TrySelect("0/passages/1"));
  22. UNIT_ASSERT(v.PathExists("0/passages/0"));
  23. UNIT_ASSERT(v.PathExists("[0]/[passages]/[0]"));
  24. UNIT_ASSERT(v.PathExists("[0][passages][0]"));
  25. UNIT_ASSERT(v.PathExists(""));
  26. UNIT_ASSERT(!v.PathExists("`"));
  27. UNIT_ASSERT(v.TrySelect("").Has(0));
  28. UNIT_ASSERT(!v.PathExists("1"));
  29. UNIT_ASSERT(!v.PathExists("0/passages1"));
  30. UNIT_ASSERT(!v.PathExists("0/passages/2"));
  31. UNIT_ASSERT(!v.PathExists("0/passages/2"));
  32. UNIT_ASSERT_VALUES_EQUAL(0, (double)v.TrySelect("0/passages/2"));
  33. UNIT_ASSERT(!v.PathExists("0/passages/2"));
  34. }
  35. {
  36. const NSc::TValue& vv = NSc::TValue::FromJson("[ test ]]");
  37. UNIT_ASSERT(vv.IsNull());
  38. }
  39. {
  40. const char* json = "[a,b],[a,b]";
  41. const NSc::TValue& v = NSc::TValue::FromJson(json);
  42. UNIT_ASSERT(v.IsNull());
  43. }
  44. {
  45. const char* json = "[null,null]";
  46. const NSc::TValue& v = NSc::TValue::FromJson(json);
  47. UNIT_ASSERT(v.PathExists("1"));
  48. UNIT_ASSERT(!v.PathExists("2"));
  49. }
  50. {
  51. const char* json = "{ a : b : c }";
  52. NSc::TValue v;
  53. UNIT_ASSERT(!NSc::TValue::FromJson(v, json));
  54. UNIT_ASSERT(v.IsNull());
  55. }
  56. {
  57. const char* json = "[a:b]";
  58. UNIT_ASSERT(NSc::TValue::FromJson(json).IsNull());
  59. }
  60. {
  61. UNIT_ASSERT_VALUES_EQUAL("{\n \"a\" : \"b\",\n \"c\" : \"d\"\n}",
  62. NSc::TValue::FromJson("{a:b,c:d}").ToJson(NSc::TValue::JO_PRETTY));
  63. }
  64. }
  65. Y_UNIT_TEST(TestSafeJson) {
  66. TString ss;
  67. ss.reserve(256);
  68. for (int i = 0; i < 256; ++i) {
  69. ss.append((char)i);
  70. }
  71. NSc::TValue v;
  72. v[ss] = "xxx";
  73. v["xxx"] = ss;
  74. UNIT_ASSERT_VALUES_EQUAL("{\"xxx\":null}", v.ToJson(NSc::TValue::JO_SKIP_UNSAFE));
  75. UNIT_ASSERT_VALUES_EQUAL("{\"xxx\":null}", v.ToJson(NSc::TValue::JO_SAFE));
  76. UNIT_ASSERT_VALUES_EQUAL("{"
  77. "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r"
  78. "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018"
  79. "\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"#$%&'()*+,-./0123456789"
  80. ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F"
  81. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93"
  82. "\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
  83. "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB"
  84. "\xBC\xBD\xBE\xBF\\xC0\\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE"
  85. "\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1"
  86. "\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4"
  87. "\\xF5\\xF6\\xF7\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\":"
  88. "\"xxx\","
  89. "\"xxx\":"
  90. "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r"
  91. "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018"
  92. "\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"#$%&'()*+,-./0123456789"
  93. ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F"
  94. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93"
  95. "\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7"
  96. "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB"
  97. "\xBC\xBD\xBE\xBF\\xC0\\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE"
  98. "\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1"
  99. "\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4"
  100. "\\xF5\\xF6\\xF7\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\""
  101. "}",
  102. v.ToJson(NSc::TValue::JO_SORT_KEYS));
  103. UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::FromJson(v.ToJson())));
  104. {
  105. NSc::TValue value;
  106. TString articleName{"\xC2\xC2\xCF"};
  107. value["text"] = articleName;
  108. UNIT_ASSERT_VALUES_EQUAL(value.ToJson(), "{\"text\":\"\xC2\xC2\xCF\"}");
  109. UNIT_ASSERT_VALUES_EQUAL(value.ToJsonSafe(), "{\"text\":null}");
  110. }
  111. }
  112. Y_UNIT_TEST(TestJsonEscape) {
  113. NSc::TValue v("\10\7\6\5\4\3\2\1\0"sv);
  114. UNIT_ASSERT_VALUES_EQUAL(v.ToJson(), "\"\\b\\u0007\\u0006\\u0005\\u0004\\u0003\\u0002\\u0001\\u0000\"");
  115. }
  116. Y_UNIT_TEST(TestStrictJson) {
  117. UNIT_ASSERT_NO_EXCEPTION(NSc::TValue::FromJsonThrow("{a:b}"));
  118. UNIT_ASSERT_EXCEPTION(NSc::TValue::FromJsonThrow("{a:b}", NSc::TValue::JO_PARSER_STRICT), yexception);
  119. UNIT_ASSERT_NO_EXCEPTION(NSc::TValue::FromJsonThrow("{\"a\":\"b\"}", NSc::TValue::JO_PARSER_STRICT));
  120. }
  121. Y_UNIT_TEST(TestJsonValue) {
  122. NSc::TValue a = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4,str,{b:{c:d}}],e:f}");
  123. NSc::TValue b = NSc::TValue::FromJsonValue(a.ToJsonValue());
  124. UNIT_ASSERT_JSON_EQ_JSON(a, b);
  125. }
  126. Y_UNIT_TEST(TestJsonEmptyContainers) {
  127. {
  128. NSc::TValue a = NSc::NUt::AssertFromJson("{a:[]}");
  129. NSc::TValue b = NSc::TValue::FromJsonValue(a.ToJsonValue());
  130. UNIT_ASSERT_JSON_EQ_JSON(a, b);
  131. }
  132. {
  133. NSc::TValue a = NSc::NUt::AssertFromJson("{a:{}}");
  134. NSc::TValue b = NSc::TValue::FromJsonValue(a.ToJsonValue());
  135. UNIT_ASSERT_JSON_EQ_JSON(a, b);
  136. }
  137. }
  138. Y_UNIT_TEST(TestDuplicateKeys) {
  139. const TStringBuf duplicatedKeys = "{\"a\":[{\"b\":1, \"b\":42}]}";
  140. UNIT_ASSERT_NO_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys));
  141. UNIT_ASSERT_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS), yexception);
  142. UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys).IsDict());
  143. UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS).IsNull());
  144. }
  145. }