writertest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/document.h"
  16. #include "rapidjson/reader.h"
  17. #include "rapidjson/writer.h"
  18. #include "rapidjson/stringbuffer.h"
  19. #include "rapidjson/memorybuffer.h"
  20. #ifdef __clang__
  21. RAPIDJSON_DIAG_PUSH
  22. RAPIDJSON_DIAG_OFF(c++98-compat)
  23. #endif
  24. using namespace rapidjson;
  25. TEST(Writer, Compact) {
  26. StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ");
  27. StringBuffer buffer;
  28. Writer<StringBuffer> writer(buffer);
  29. buffer.ShrinkToFit();
  30. Reader reader;
  31. reader.Parse<0>(s, writer);
  32. EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", buffer.GetString());
  33. EXPECT_EQ(77u, buffer.GetSize());
  34. EXPECT_TRUE(writer.IsComplete());
  35. }
  36. // json -> parse -> writer -> json
  37. #define TEST_ROUNDTRIP(json) \
  38. { \
  39. StringStream s(json); \
  40. StringBuffer buffer; \
  41. Writer<StringBuffer> writer(buffer); \
  42. Reader reader; \
  43. reader.Parse<kParseFullPrecisionFlag>(s, writer); \
  44. EXPECT_STREQ(json, buffer.GetString()); \
  45. EXPECT_TRUE(writer.IsComplete()); \
  46. }
  47. TEST(Writer, Root) {
  48. TEST_ROUNDTRIP("null");
  49. TEST_ROUNDTRIP("true");
  50. TEST_ROUNDTRIP("false");
  51. TEST_ROUNDTRIP("0");
  52. TEST_ROUNDTRIP("\"foo\"");
  53. TEST_ROUNDTRIP("[]");
  54. TEST_ROUNDTRIP("{}");
  55. }
  56. TEST(Writer, Int) {
  57. TEST_ROUNDTRIP("[-1]");
  58. TEST_ROUNDTRIP("[-123]");
  59. TEST_ROUNDTRIP("[-2147483648]");
  60. }
  61. TEST(Writer, UInt) {
  62. TEST_ROUNDTRIP("[0]");
  63. TEST_ROUNDTRIP("[1]");
  64. TEST_ROUNDTRIP("[123]");
  65. TEST_ROUNDTRIP("[2147483647]");
  66. TEST_ROUNDTRIP("[4294967295]");
  67. }
  68. TEST(Writer, Int64) {
  69. TEST_ROUNDTRIP("[-1234567890123456789]");
  70. TEST_ROUNDTRIP("[-9223372036854775808]");
  71. }
  72. TEST(Writer, Uint64) {
  73. TEST_ROUNDTRIP("[1234567890123456789]");
  74. TEST_ROUNDTRIP("[9223372036854775807]");
  75. }
  76. TEST(Writer, String) {
  77. TEST_ROUNDTRIP("[\"Hello\"]");
  78. TEST_ROUNDTRIP("[\"Hello\\u0000World\"]");
  79. TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]");
  80. #if RAPIDJSON_HAS_STDSTRING
  81. {
  82. StringBuffer buffer;
  83. Writer<StringBuffer> writer(buffer);
  84. writer.String(std::string("Hello\n"));
  85. EXPECT_STREQ("\"Hello\\n\"", buffer.GetString());
  86. }
  87. #endif
  88. }
  89. TEST(Writer, Issue_889) {
  90. char buf[100] = "Hello";
  91. StringBuffer buffer;
  92. Writer<StringBuffer> writer(buffer);
  93. writer.StartArray();
  94. writer.String(buf);
  95. writer.EndArray();
  96. EXPECT_STREQ("[\"Hello\"]", buffer.GetString());
  97. EXPECT_TRUE(writer.IsComplete()); \
  98. }
  99. TEST(Writer, ScanWriteUnescapedString) {
  100. const char json[] = "[\" \\\"0123456789ABCDEF\"]";
  101. // ^ scanning stops here.
  102. char buffer2[sizeof(json) + 32];
  103. // Use different offset to test different alignments
  104. for (int i = 0; i < 32; i++) {
  105. char* p = buffer2 + i;
  106. memcpy(p, json, sizeof(json));
  107. TEST_ROUNDTRIP(p);
  108. }
  109. }
  110. TEST(Writer, Double) {
  111. TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
  112. TEST_ROUNDTRIP("0.0");
  113. TEST_ROUNDTRIP("-0.0"); // Issue #289
  114. TEST_ROUNDTRIP("1e30");
  115. TEST_ROUNDTRIP("1.0");
  116. TEST_ROUNDTRIP("5e-324"); // Min subnormal positive double
  117. TEST_ROUNDTRIP("2.225073858507201e-308"); // Max subnormal positive double
  118. TEST_ROUNDTRIP("2.2250738585072014e-308"); // Min normal positive double
  119. TEST_ROUNDTRIP("1.7976931348623157e308"); // Max double
  120. }
  121. // UTF8 -> TargetEncoding -> UTF8
  122. template <typename TargetEncoding>
  123. void TestTranscode(const char* json) {
  124. StringStream s(json);
  125. GenericStringBuffer<TargetEncoding> buffer;
  126. Writer<GenericStringBuffer<TargetEncoding>, UTF8<>, TargetEncoding> writer(buffer);
  127. Reader reader;
  128. reader.Parse(s, writer);
  129. StringBuffer buffer2;
  130. Writer<StringBuffer> writer2(buffer2);
  131. GenericReader<TargetEncoding, UTF8<> > reader2;
  132. GenericStringStream<TargetEncoding> s2(buffer.GetString());
  133. reader2.Parse(s2, writer2);
  134. EXPECT_STREQ(json, buffer2.GetString());
  135. }
  136. TEST(Writer, Transcode) {
  137. const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
  138. // UTF8 -> UTF16 -> UTF8
  139. TestTranscode<UTF8<> >(json);
  140. // UTF8 -> ASCII -> UTF8
  141. TestTranscode<ASCII<> >(json);
  142. // UTF8 -> UTF16 -> UTF8
  143. TestTranscode<UTF16<> >(json);
  144. // UTF8 -> UTF32 -> UTF8
  145. TestTranscode<UTF32<> >(json);
  146. // UTF8 -> AutoUTF -> UTF8
  147. UTFType types[] = { kUTF8, kUTF16LE , kUTF16BE, kUTF32LE , kUTF32BE };
  148. for (size_t i = 0; i < 5; i++) {
  149. StringStream s(json);
  150. MemoryBuffer buffer;
  151. AutoUTFOutputStream<unsigned, MemoryBuffer> os(buffer, types[i], true);
  152. Writer<AutoUTFOutputStream<unsigned, MemoryBuffer>, UTF8<>, AutoUTF<unsigned> > writer(os);
  153. Reader reader;
  154. reader.Parse(s, writer);
  155. StringBuffer buffer2;
  156. Writer<StringBuffer> writer2(buffer2);
  157. GenericReader<AutoUTF<unsigned>, UTF8<> > reader2;
  158. MemoryStream s2(buffer.GetBuffer(), buffer.GetSize());
  159. AutoUTFInputStream<unsigned, MemoryStream> is(s2);
  160. reader2.Parse(is, writer2);
  161. EXPECT_STREQ(json, buffer2.GetString());
  162. }
  163. }
  164. #include <sstream>
  165. class OStreamWrapper {
  166. public:
  167. typedef char Ch;
  168. OStreamWrapper(std::ostream& os) : os_(os) {}
  169. Ch Peek() const { assert(false); return '\0'; }
  170. Ch Take() { assert(false); return '\0'; }
  171. size_t Tell() const { return 0; }
  172. Ch* PutBegin() { assert(false); return 0; }
  173. void Put(Ch c) { os_.put(c); }
  174. void Flush() { os_.flush(); }
  175. size_t PutEnd(Ch*) { assert(false); return 0; }
  176. private:
  177. OStreamWrapper(const OStreamWrapper&);
  178. OStreamWrapper& operator=(const OStreamWrapper&);
  179. std::ostream& os_;
  180. };
  181. TEST(Writer, OStreamWrapper) {
  182. StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"u64\": 1234567890123456789, \"i64\":-1234567890123456789 } ");
  183. std::stringstream ss;
  184. OStreamWrapper os(ss);
  185. Writer<OStreamWrapper> writer(os);
  186. Reader reader;
  187. reader.Parse<0>(s, writer);
  188. std::string actual = ss.str();
  189. EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}", actual.c_str());
  190. }
  191. TEST(Writer, AssertRootMayBeAnyValue) {
  192. #define T(x)\
  193. {\
  194. StringBuffer buffer;\
  195. Writer<StringBuffer> writer(buffer);\
  196. EXPECT_TRUE(x);\
  197. }
  198. T(writer.Bool(false));
  199. T(writer.Bool(true));
  200. T(writer.Null());
  201. T(writer.Int(0));
  202. T(writer.Uint(0));
  203. T(writer.Int64(0));
  204. T(writer.Uint64(0));
  205. T(writer.Double(0));
  206. T(writer.String("foo"));
  207. #undef T
  208. }
  209. TEST(Writer, AssertIncorrectObjectLevel) {
  210. StringBuffer buffer;
  211. Writer<StringBuffer> writer(buffer);
  212. writer.StartObject();
  213. writer.EndObject();
  214. ASSERT_THROW(writer.EndObject(), AssertException);
  215. }
  216. TEST(Writer, AssertIncorrectArrayLevel) {
  217. StringBuffer buffer;
  218. Writer<StringBuffer> writer(buffer);
  219. writer.StartArray();
  220. writer.EndArray();
  221. ASSERT_THROW(writer.EndArray(), AssertException);
  222. }
  223. TEST(Writer, AssertIncorrectEndObject) {
  224. StringBuffer buffer;
  225. Writer<StringBuffer> writer(buffer);
  226. writer.StartObject();
  227. ASSERT_THROW(writer.EndArray(), AssertException);
  228. }
  229. TEST(Writer, AssertIncorrectEndArray) {
  230. StringBuffer buffer;
  231. Writer<StringBuffer> writer(buffer);
  232. writer.StartObject();
  233. ASSERT_THROW(writer.EndArray(), AssertException);
  234. }
  235. TEST(Writer, AssertObjectKeyNotString) {
  236. #define T(x)\
  237. {\
  238. StringBuffer buffer;\
  239. Writer<StringBuffer> writer(buffer);\
  240. writer.StartObject();\
  241. ASSERT_THROW(x, AssertException); \
  242. }
  243. T(writer.Bool(false));
  244. T(writer.Bool(true));
  245. T(writer.Null());
  246. T(writer.Int(0));
  247. T(writer.Uint(0));
  248. T(writer.Int64(0));
  249. T(writer.Uint64(0));
  250. T(writer.Double(0));
  251. T(writer.StartObject());
  252. T(writer.StartArray());
  253. #undef T
  254. }
  255. TEST(Writer, AssertMultipleRoot) {
  256. StringBuffer buffer;
  257. Writer<StringBuffer> writer(buffer);
  258. writer.StartObject();
  259. writer.EndObject();
  260. ASSERT_THROW(writer.StartObject(), AssertException);
  261. writer.Reset(buffer);
  262. writer.Null();
  263. ASSERT_THROW(writer.Int(0), AssertException);
  264. writer.Reset(buffer);
  265. writer.String("foo");
  266. ASSERT_THROW(writer.StartArray(), AssertException);
  267. writer.Reset(buffer);
  268. writer.StartArray();
  269. writer.EndArray();
  270. //ASSERT_THROW(writer.Double(3.14), AssertException);
  271. }
  272. TEST(Writer, RootObjectIsComplete) {
  273. StringBuffer buffer;
  274. Writer<StringBuffer> writer(buffer);
  275. EXPECT_FALSE(writer.IsComplete());
  276. writer.StartObject();
  277. EXPECT_FALSE(writer.IsComplete());
  278. writer.String("foo");
  279. EXPECT_FALSE(writer.IsComplete());
  280. writer.Int(1);
  281. EXPECT_FALSE(writer.IsComplete());
  282. writer.EndObject();
  283. EXPECT_TRUE(writer.IsComplete());
  284. }
  285. TEST(Writer, RootArrayIsComplete) {
  286. StringBuffer buffer;
  287. Writer<StringBuffer> writer(buffer);
  288. EXPECT_FALSE(writer.IsComplete());
  289. writer.StartArray();
  290. EXPECT_FALSE(writer.IsComplete());
  291. writer.String("foo");
  292. EXPECT_FALSE(writer.IsComplete());
  293. writer.Int(1);
  294. EXPECT_FALSE(writer.IsComplete());
  295. writer.EndArray();
  296. EXPECT_TRUE(writer.IsComplete());
  297. }
  298. TEST(Writer, RootValueIsComplete) {
  299. #define T(x)\
  300. {\
  301. StringBuffer buffer;\
  302. Writer<StringBuffer> writer(buffer);\
  303. EXPECT_FALSE(writer.IsComplete()); \
  304. x; \
  305. EXPECT_TRUE(writer.IsComplete()); \
  306. }
  307. T(writer.Null());
  308. T(writer.Bool(true));
  309. T(writer.Bool(false));
  310. T(writer.Int(0));
  311. T(writer.Uint(0));
  312. T(writer.Int64(0));
  313. T(writer.Uint64(0));
  314. T(writer.Double(0));
  315. T(writer.String(""));
  316. #undef T
  317. }
  318. TEST(Writer, InvalidEncoding) {
  319. // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  320. {
  321. GenericStringBuffer<UTF16<> > buffer;
  322. Writer<GenericStringBuffer<UTF16<> >, UTF8<>, UTF16<> > writer(buffer);
  323. writer.StartArray();
  324. EXPECT_FALSE(writer.String("\xfe"));
  325. EXPECT_FALSE(writer.String("\xff"));
  326. EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff"));
  327. writer.EndArray();
  328. }
  329. // Fail in encoding
  330. {
  331. StringBuffer buffer;
  332. Writer<StringBuffer, UTF32<> > writer(buffer);
  333. static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
  334. EXPECT_FALSE(writer.String(s));
  335. }
  336. // Fail in unicode escaping in ASCII output
  337. {
  338. StringBuffer buffer;
  339. Writer<StringBuffer, UTF32<>, ASCII<> > writer(buffer);
  340. static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
  341. EXPECT_FALSE(writer.String(s));
  342. }
  343. }
  344. TEST(Writer, ValidateEncoding) {
  345. {
  346. StringBuffer buffer;
  347. Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer);
  348. writer.StartArray();
  349. EXPECT_TRUE(writer.String("\x24")); // Dollar sign U+0024
  350. EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2
  351. EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC
  352. EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E
  353. writer.EndArray();
  354. EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString());
  355. }
  356. // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  357. {
  358. StringBuffer buffer;
  359. Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer);
  360. writer.StartArray();
  361. EXPECT_FALSE(writer.String("\xfe"));
  362. EXPECT_FALSE(writer.String("\xff"));
  363. EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff"));
  364. writer.EndArray();
  365. }
  366. }
  367. TEST(Writer, InvalidEventSequence) {
  368. // {]
  369. {
  370. StringBuffer buffer;
  371. Writer<StringBuffer> writer(buffer);
  372. writer.StartObject();
  373. EXPECT_THROW(writer.EndArray(), AssertException);
  374. EXPECT_FALSE(writer.IsComplete());
  375. }
  376. // [}
  377. {
  378. StringBuffer buffer;
  379. Writer<StringBuffer> writer(buffer);
  380. writer.StartArray();
  381. EXPECT_THROW(writer.EndObject(), AssertException);
  382. EXPECT_FALSE(writer.IsComplete());
  383. }
  384. // { 1:
  385. {
  386. StringBuffer buffer;
  387. Writer<StringBuffer> writer(buffer);
  388. writer.StartObject();
  389. EXPECT_THROW(writer.Int(1), AssertException);
  390. EXPECT_FALSE(writer.IsComplete());
  391. }
  392. // { 'a' }
  393. {
  394. StringBuffer buffer;
  395. Writer<StringBuffer> writer(buffer);
  396. writer.StartObject();
  397. writer.Key("a");
  398. EXPECT_THROW(writer.EndObject(), AssertException);
  399. EXPECT_FALSE(writer.IsComplete());
  400. }
  401. // { 'a':'b','c' }
  402. {
  403. StringBuffer buffer;
  404. Writer<StringBuffer> writer(buffer);
  405. writer.StartObject();
  406. writer.Key("a");
  407. writer.String("b");
  408. writer.Key("c");
  409. EXPECT_THROW(writer.EndObject(), AssertException);
  410. EXPECT_FALSE(writer.IsComplete());
  411. }
  412. }
  413. TEST(Writer, NaN) {
  414. double nan = std::numeric_limits<double>::quiet_NaN();
  415. EXPECT_TRUE(internal::Double(nan).IsNan());
  416. StringBuffer buffer;
  417. {
  418. Writer<StringBuffer> writer(buffer);
  419. EXPECT_FALSE(writer.Double(nan));
  420. }
  421. {
  422. Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
  423. EXPECT_TRUE(writer.Double(nan));
  424. EXPECT_STREQ("NaN", buffer.GetString());
  425. }
  426. GenericStringBuffer<UTF16<> > buffer2;
  427. Writer<GenericStringBuffer<UTF16<> > > writer2(buffer2);
  428. EXPECT_FALSE(writer2.Double(nan));
  429. }
  430. TEST(Writer, Inf) {
  431. double inf = std::numeric_limits<double>::infinity();
  432. EXPECT_TRUE(internal::Double(inf).IsInf());
  433. StringBuffer buffer;
  434. {
  435. Writer<StringBuffer> writer(buffer);
  436. EXPECT_FALSE(writer.Double(inf));
  437. }
  438. {
  439. Writer<StringBuffer> writer(buffer);
  440. EXPECT_FALSE(writer.Double(-inf));
  441. }
  442. {
  443. Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
  444. EXPECT_TRUE(writer.Double(inf));
  445. }
  446. {
  447. Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer);
  448. EXPECT_TRUE(writer.Double(-inf));
  449. }
  450. EXPECT_STREQ("Infinity-Infinity", buffer.GetString());
  451. }
  452. TEST(Writer, RawValue) {
  453. StringBuffer buffer;
  454. Writer<StringBuffer> writer(buffer);
  455. writer.StartObject();
  456. writer.Key("a");
  457. writer.Int(1);
  458. writer.Key("raw");
  459. const char json[] = "[\"Hello\\nWorld\", 123.456]";
  460. writer.RawValue(json, strlen(json), kArrayType);
  461. writer.EndObject();
  462. EXPECT_TRUE(writer.IsComplete());
  463. EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
  464. }
  465. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  466. static Writer<StringBuffer> WriterGen(StringBuffer &target) {
  467. Writer<StringBuffer> writer(target);
  468. writer.StartObject();
  469. writer.Key("a");
  470. writer.Int(1);
  471. return writer;
  472. }
  473. TEST(Writer, MoveCtor) {
  474. StringBuffer buffer;
  475. Writer<StringBuffer> writer(WriterGen(buffer));
  476. writer.EndObject();
  477. EXPECT_TRUE(writer.IsComplete());
  478. EXPECT_STREQ("{\"a\":1}", buffer.GetString());
  479. }
  480. #endif
  481. #ifdef __clang__
  482. RAPIDJSON_DIAG_POP
  483. #endif