JSONTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //===-- JSONTest.cpp - JSON unit tests --------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/JSON.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. #include "gmock/gmock.h"
  11. #include "gtest/gtest.h"
  12. namespace llvm {
  13. namespace json {
  14. namespace {
  15. std::string s(const Value &E) { return llvm::formatv("{0}", E).str(); }
  16. std::string sp(const Value &E) { return llvm::formatv("{0:2}", E).str(); }
  17. TEST(JSONTest, Types) {
  18. EXPECT_EQ("true", s(true));
  19. EXPECT_EQ("null", s(nullptr));
  20. EXPECT_EQ("2.5", s(2.5));
  21. EXPECT_EQ(R"("foo")", s("foo"));
  22. EXPECT_EQ("[1,2,3]", s({1, 2, 3}));
  23. EXPECT_EQ(R"({"x":10,"y":20})", s(Object{{"x", 10}, {"y", 20}}));
  24. #ifdef NDEBUG
  25. EXPECT_EQ(R"("��")", s("\xC0\x80"));
  26. EXPECT_EQ(R"({"��":0})", s(Object{{"\xC0\x80", 0}}));
  27. #else
  28. EXPECT_DEATH(s("\xC0\x80"), "Invalid UTF-8");
  29. EXPECT_DEATH(s(Object{{"\xC0\x80", 0}}), "Invalid UTF-8");
  30. #endif
  31. }
  32. TEST(JSONTest, Constructors) {
  33. // Lots of edge cases around empty and singleton init lists.
  34. EXPECT_EQ("[[[3]]]", s({{{3}}}));
  35. EXPECT_EQ("[[[]]]", s({{{}}}));
  36. EXPECT_EQ("[[{}]]", s({{Object{}}}));
  37. EXPECT_EQ(R"({"A":{"B":{}}})", s(Object{{"A", Object{{"B", Object{}}}}}));
  38. EXPECT_EQ(R"({"A":{"B":{"X":"Y"}}})",
  39. s(Object{{"A", Object{{"B", Object{{"X", "Y"}}}}}}));
  40. EXPECT_EQ("null", s(llvm::Optional<double>()));
  41. EXPECT_EQ("2.5", s(llvm::Optional<double>(2.5)));
  42. EXPECT_EQ("[[2.5,null]]", s(std::vector<std::vector<llvm::Optional<double>>>{
  43. {2.5, llvm::None}}));
  44. }
  45. TEST(JSONTest, StringOwnership) {
  46. char X[] = "Hello";
  47. Value Alias = static_cast<const char *>(X);
  48. X[1] = 'a';
  49. EXPECT_EQ(R"("Hallo")", s(Alias));
  50. std::string Y = "Hello";
  51. Value Copy = Y;
  52. Y[1] = 'a';
  53. EXPECT_EQ(R"("Hello")", s(Copy));
  54. }
  55. TEST(JSONTest, CanonicalOutput) {
  56. // Objects are sorted (but arrays aren't)!
  57. EXPECT_EQ(R"({"a":1,"b":2,"c":3})", s(Object{{"a", 1}, {"c", 3}, {"b", 2}}));
  58. EXPECT_EQ(R"(["a","c","b"])", s({"a", "c", "b"}));
  59. EXPECT_EQ("3", s(3.0));
  60. }
  61. TEST(JSONTest, Escaping) {
  62. std::string test = {
  63. 0, // Strings may contain nulls.
  64. '\b', '\f', // Have mnemonics, but we escape numerically.
  65. '\r', '\n', '\t', // Escaped with mnemonics.
  66. 'S', '\"', '\\', // Printable ASCII characters.
  67. '\x7f', // Delete is not escaped.
  68. '\xce', '\x94', // Non-ASCII UTF-8 is not escaped.
  69. };
  70. std::string teststring = R"("\u0000\u0008\u000c\r\n\tS\"\\)"
  71. "\x7f\xCE\x94\"";
  72. EXPECT_EQ(teststring, s(test));
  73. EXPECT_EQ(R"({"object keys are\nescaped":true})",
  74. s(Object{{"object keys are\nescaped", true}}));
  75. }
  76. TEST(JSONTest, PrettyPrinting) {
  77. const char str[] = R"({
  78. "empty_array": [],
  79. "empty_object": {},
  80. "full_array": [
  81. 1,
  82. null
  83. ],
  84. "full_object": {
  85. "nested_array": [
  86. {
  87. "property": "value"
  88. }
  89. ]
  90. }
  91. })";
  92. EXPECT_EQ(str, sp(Object{
  93. {"empty_object", Object{}},
  94. {"empty_array", {}},
  95. {"full_array", {1, nullptr}},
  96. {"full_object",
  97. Object{
  98. {"nested_array",
  99. {Object{
  100. {"property", "value"},
  101. }}},
  102. }},
  103. }));
  104. }
  105. TEST(JSONTest, Parse) {
  106. auto Compare = [](llvm::StringRef S, Value Expected) {
  107. if (auto E = parse(S)) {
  108. // Compare both string forms and with operator==, in case we have bugs.
  109. EXPECT_EQ(*E, Expected);
  110. EXPECT_EQ(sp(*E), sp(Expected));
  111. } else {
  112. handleAllErrors(E.takeError(), [S](const llvm::ErrorInfoBase &E) {
  113. FAIL() << "Failed to parse JSON >>> " << S << " <<<: " << E.message();
  114. });
  115. }
  116. };
  117. Compare(R"(true)", true);
  118. Compare(R"(false)", false);
  119. Compare(R"(null)", nullptr);
  120. Compare(R"(42)", 42);
  121. Compare(R"(2.5)", 2.5);
  122. Compare(R"(2e50)", 2e50);
  123. Compare(R"(1.2e3456789)", std::numeric_limits<double>::infinity());
  124. Compare(R"("foo")", "foo");
  125. Compare(R"("\"\\\b\f\n\r\t")", "\"\\\b\f\n\r\t");
  126. Compare(R"("\u0000")", llvm::StringRef("\0", 1));
  127. Compare("\"\x7f\"", "\x7f");
  128. Compare(R"("\ud801\udc37")", u8"\U00010437"); // UTF16 surrogate pair escape.
  129. Compare("\"\xE2\x82\xAC\xF0\x9D\x84\x9E\"", u8"\u20ac\U0001d11e"); // UTF8
  130. Compare(
  131. R"("LoneLeading=\ud801, LoneTrailing=\udc01, LeadingLeadingTrailing=\ud801\ud801\udc37")",
  132. u8"LoneLeading=\ufffd, LoneTrailing=\ufffd, "
  133. u8"LeadingLeadingTrailing=\ufffd\U00010437"); // Invalid unicode.
  134. Compare(R"({"":0,"":0})", Object{{"", 0}});
  135. Compare(R"({"obj":{},"arr":[]})", Object{{"obj", Object{}}, {"arr", {}}});
  136. Compare(R"({"\n":{"\u0000":[[[[]]]]}})",
  137. Object{{"\n", Object{
  138. {llvm::StringRef("\0", 1), {{{{}}}}},
  139. }}});
  140. Compare("\r[\n\t] ", {});
  141. }
  142. TEST(JSONTest, ParseErrors) {
  143. auto ExpectErr = [](llvm::StringRef Msg, llvm::StringRef S) {
  144. if (auto E = parse(S)) {
  145. // Compare both string forms and with operator==, in case we have bugs.
  146. FAIL() << "Parsed JSON >>> " << S << " <<< but wanted error: " << Msg;
  147. } else {
  148. handleAllErrors(E.takeError(), [S, Msg](const llvm::ErrorInfoBase &E) {
  149. EXPECT_THAT(E.message(), testing::HasSubstr(Msg)) << S;
  150. });
  151. }
  152. };
  153. ExpectErr("Unexpected EOF", "");
  154. ExpectErr("Unexpected EOF", "[");
  155. ExpectErr("Text after end of document", "[][]");
  156. ExpectErr("Invalid JSON value (false?)", "fuzzy");
  157. ExpectErr("Expected , or ]", "[2?]");
  158. ExpectErr("Expected object key", "{a:2}");
  159. ExpectErr("Expected : after object key", R"({"a",2})");
  160. ExpectErr("Expected , or } after object property", R"({"a":2 "b":3})");
  161. ExpectErr("Invalid JSON value", R"([&%!])");
  162. ExpectErr("Invalid JSON value (number?)", "1e1.0");
  163. ExpectErr("Unterminated string", R"("abc\"def)");
  164. ExpectErr("Control character in string", "\"abc\ndef\"");
  165. ExpectErr("Invalid escape sequence", R"("\030")");
  166. ExpectErr("Invalid \\u escape sequence", R"("\usuck")");
  167. ExpectErr("[3:3, byte=19]", R"({
  168. "valid": 1,
  169. invalid: 2
  170. })");
  171. ExpectErr("Invalid UTF-8 sequence", "\"\xC0\x80\""); // WTF-8 null
  172. }
  173. // Direct tests of isUTF8 and fixUTF8. Internal uses are also tested elsewhere.
  174. TEST(JSONTest, UTF8) {
  175. for (const char *Valid : {
  176. "this is ASCII text",
  177. "thïs tëxt häs BMP chäräctërs",
  178. "𐌶𐌰L𐌾𐍈 C𐍈𐌼𐌴𐍃",
  179. }) {
  180. EXPECT_TRUE(isUTF8(Valid)) << Valid;
  181. EXPECT_EQ(fixUTF8(Valid), Valid);
  182. }
  183. for (auto Invalid : std::vector<std::pair<const char *, const char *>>{
  184. {"lone trailing \x81\x82 bytes", "lone trailing �� bytes"},
  185. {"missing trailing \xD0 bytes", "missing trailing � bytes"},
  186. {"truncated character \xD0", "truncated character �"},
  187. {"not \xC1\x80 the \xE0\x9f\xBF shortest \xF0\x83\x83\x83 encoding",
  188. "not �� the ��� shortest ���� encoding"},
  189. {"too \xF9\x80\x80\x80\x80 long", "too ����� long"},
  190. {"surrogate \xED\xA0\x80 invalid \xF4\x90\x80\x80",
  191. "surrogate ��� invalid ����"}}) {
  192. EXPECT_FALSE(isUTF8(Invalid.first)) << Invalid.first;
  193. EXPECT_EQ(fixUTF8(Invalid.first), Invalid.second);
  194. }
  195. }
  196. TEST(JSONTest, Inspection) {
  197. llvm::Expected<Value> Doc = parse(R"(
  198. {
  199. "null": null,
  200. "boolean": false,
  201. "number": 2.78,
  202. "string": "json",
  203. "array": [null, true, 3.14, "hello", [1,2,3], {"time": "arrow"}],
  204. "object": {"fruit": "banana"}
  205. }
  206. )");
  207. EXPECT_TRUE(!!Doc);
  208. Object *O = Doc->getAsObject();
  209. ASSERT_TRUE(O);
  210. EXPECT_FALSE(O->getNull("missing"));
  211. EXPECT_FALSE(O->getNull("boolean"));
  212. EXPECT_TRUE(O->getNull("null"));
  213. EXPECT_EQ(O->getNumber("number"), llvm::Optional<double>(2.78));
  214. EXPECT_FALSE(O->getInteger("number"));
  215. EXPECT_EQ(O->getString("string"), llvm::Optional<llvm::StringRef>("json"));
  216. ASSERT_FALSE(O->getObject("missing"));
  217. ASSERT_FALSE(O->getObject("array"));
  218. ASSERT_TRUE(O->getObject("object"));
  219. EXPECT_EQ(*O->getObject("object"), (Object{{"fruit", "banana"}}));
  220. Array *A = O->getArray("array");
  221. ASSERT_TRUE(A);
  222. EXPECT_EQ((*A)[1].getAsBoolean(), llvm::Optional<bool>(true));
  223. ASSERT_TRUE((*A)[4].getAsArray());
  224. EXPECT_EQ(*(*A)[4].getAsArray(), (Array{1, 2, 3}));
  225. EXPECT_EQ((*(*A)[4].getAsArray())[1].getAsInteger(),
  226. llvm::Optional<int64_t>(2));
  227. int I = 0;
  228. for (Value &E : *A) {
  229. if (I++ == 5) {
  230. ASSERT_TRUE(E.getAsObject());
  231. EXPECT_EQ(E.getAsObject()->getString("time"),
  232. llvm::Optional<llvm::StringRef>("arrow"));
  233. } else
  234. EXPECT_FALSE(E.getAsObject());
  235. }
  236. }
  237. // Verify special integer handling - we try to preserve exact int64 values.
  238. TEST(JSONTest, Integers) {
  239. struct {
  240. const char *Desc;
  241. Value Val;
  242. const char *Str;
  243. llvm::Optional<int64_t> AsInt;
  244. llvm::Optional<double> AsNumber;
  245. } TestCases[] = {
  246. {
  247. "Non-integer. Stored as double, not convertible.",
  248. double{1.5},
  249. "1.5",
  250. llvm::None,
  251. 1.5,
  252. },
  253. {
  254. "Integer, not exact double. Stored as int64, convertible.",
  255. int64_t{0x4000000000000001},
  256. "4611686018427387905",
  257. int64_t{0x4000000000000001},
  258. double{0x4000000000000000},
  259. },
  260. {
  261. "Negative integer, not exact double. Stored as int64, convertible.",
  262. int64_t{-0x4000000000000001},
  263. "-4611686018427387905",
  264. int64_t{-0x4000000000000001},
  265. double{-0x4000000000000000},
  266. },
  267. {
  268. "Dynamically exact integer. Stored as double, convertible.",
  269. double{0x6000000000000000},
  270. "6.9175290276410819e+18",
  271. int64_t{0x6000000000000000},
  272. double{0x6000000000000000},
  273. },
  274. {
  275. "Dynamically integer, >64 bits. Stored as double, not convertible.",
  276. 1.5 * double{0x8000000000000000},
  277. "1.3835058055282164e+19",
  278. llvm::None,
  279. 1.5 * double{0x8000000000000000},
  280. },
  281. };
  282. for (const auto &T : TestCases) {
  283. EXPECT_EQ(T.Str, s(T.Val)) << T.Desc;
  284. llvm::Expected<Value> Doc = parse(T.Str);
  285. EXPECT_TRUE(!!Doc) << T.Desc;
  286. EXPECT_EQ(Doc->getAsInteger(), T.AsInt) << T.Desc;
  287. EXPECT_EQ(Doc->getAsNumber(), T.AsNumber) << T.Desc;
  288. EXPECT_EQ(T.Val, *Doc) << T.Desc;
  289. EXPECT_EQ(T.Str, s(*Doc)) << T.Desc;
  290. }
  291. }
  292. // Sample struct with typical JSON-mapping rules.
  293. struct CustomStruct {
  294. CustomStruct() : B(false) {}
  295. CustomStruct(std::string S, llvm::Optional<int> I, bool B)
  296. : S(S), I(I), B(B) {}
  297. std::string S;
  298. llvm::Optional<int> I;
  299. bool B;
  300. };
  301. inline bool operator==(const CustomStruct &L, const CustomStruct &R) {
  302. return L.S == R.S && L.I == R.I && L.B == R.B;
  303. }
  304. inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
  305. const CustomStruct &S) {
  306. return OS << "(" << S.S << ", " << (S.I ? std::to_string(*S.I) : "None")
  307. << ", " << S.B << ")";
  308. }
  309. bool fromJSON(const Value &E, CustomStruct &R) {
  310. ObjectMapper O(E);
  311. if (!O || !O.map("str", R.S) || !O.map("int", R.I))
  312. return false;
  313. O.map("bool", R.B);
  314. return true;
  315. }
  316. TEST(JSONTest, Deserialize) {
  317. std::map<std::string, std::vector<CustomStruct>> R;
  318. CustomStruct ExpectedStruct = {"foo", 42, true};
  319. std::map<std::string, std::vector<CustomStruct>> Expected;
  320. Value J = Object{
  321. {"foo",
  322. Array{
  323. Object{
  324. {"str", "foo"},
  325. {"int", 42},
  326. {"bool", true},
  327. {"unknown", "ignored"},
  328. },
  329. Object{{"str", "bar"}},
  330. Object{
  331. {"str", "baz"}, {"bool", "string"}, // OK, deserialize ignores.
  332. },
  333. }}};
  334. Expected["foo"] = {
  335. CustomStruct("foo", 42, true),
  336. CustomStruct("bar", llvm::None, false),
  337. CustomStruct("baz", llvm::None, false),
  338. };
  339. ASSERT_TRUE(fromJSON(J, R));
  340. EXPECT_EQ(R, Expected);
  341. CustomStruct V;
  342. EXPECT_FALSE(fromJSON(nullptr, V)) << "Not an object " << V;
  343. EXPECT_FALSE(fromJSON(Object{}, V)) << "Missing required field " << V;
  344. EXPECT_FALSE(fromJSON(Object{{"str", 1}}, V)) << "Wrong type " << V;
  345. // Optional<T> must parse as the correct type if present.
  346. EXPECT_FALSE(fromJSON(Object{{"str", 1}, {"int", "string"}}, V))
  347. << "Wrong type for Optional<T> " << V;
  348. }
  349. TEST(JSONTest, Stream) {
  350. auto StreamStuff = [](unsigned Indent) {
  351. std::string S;
  352. llvm::raw_string_ostream OS(S);
  353. OStream J(OS, Indent);
  354. J.object([&] {
  355. J.attributeArray("foo", [&] {
  356. J.value(nullptr);
  357. J.value(42.5);
  358. J.arrayBegin();
  359. J.value(43);
  360. J.arrayEnd();
  361. });
  362. J.attributeBegin("bar");
  363. J.objectBegin();
  364. J.objectEnd();
  365. J.attributeEnd();
  366. J.attribute("baz", "xyz");
  367. });
  368. return OS.str();
  369. };
  370. const char *Plain = R"({"foo":[null,42.5,[43]],"bar":{},"baz":"xyz"})";
  371. EXPECT_EQ(Plain, StreamStuff(0));
  372. const char *Pretty = R"({
  373. "foo": [
  374. null,
  375. 42.5,
  376. [
  377. 43
  378. ]
  379. ],
  380. "bar": {},
  381. "baz": "xyz"
  382. })";
  383. EXPECT_EQ(Pretty, StreamStuff(2));
  384. }
  385. } // namespace
  386. } // namespace json
  387. } // namespace llvm