documenttest.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "unittest.h"
  2. #include "rapidjson/document.h"
  3. #include "rapidjson/writer.h"
  4. #include <sstream>
  5. using namespace rapidjson;
  6. TEST(Document, Parse) {
  7. Document doc;
  8. doc.Parse<0>(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
  9. EXPECT_TRUE(doc.IsObject());
  10. EXPECT_TRUE(doc.HasMember("hello"));
  11. Value& hello = doc["hello"];
  12. EXPECT_TRUE(hello.IsString());
  13. EXPECT_STREQ("world", hello.GetString());
  14. EXPECT_TRUE(doc.HasMember("t"));
  15. Value& t = doc["t"];
  16. EXPECT_TRUE(t.IsTrue());
  17. EXPECT_TRUE(doc.HasMember("f"));
  18. Value& f = doc["f"];
  19. EXPECT_TRUE(f.IsFalse());
  20. EXPECT_TRUE(doc.HasMember("n"));
  21. Value& n = doc["n"];
  22. EXPECT_TRUE(n.IsNull());
  23. EXPECT_TRUE(doc.HasMember("i"));
  24. Value& i = doc["i"];
  25. EXPECT_TRUE(i.IsNumber());
  26. EXPECT_EQ(123, i.GetInt());
  27. EXPECT_TRUE(doc.HasMember("pi"));
  28. Value& pi = doc["pi"];
  29. EXPECT_TRUE(pi.IsNumber());
  30. EXPECT_EQ(3.1416, pi.GetDouble());
  31. EXPECT_TRUE(doc.HasMember("a"));
  32. Value& a = doc["a"];
  33. EXPECT_TRUE(a.IsArray());
  34. EXPECT_EQ(4u, a.Size());
  35. for (SizeType i = 0; i < 4; i++)
  36. EXPECT_EQ(i + 1, a[i].GetUint());
  37. }
  38. // This should be slow due to assignment in inner-loop.
  39. struct OutputStringStream : public std::ostringstream {
  40. typedef char Ch;
  41. void Put(char c) {
  42. put(c);
  43. }
  44. void Flush() {}
  45. };
  46. TEST(Document, AcceptWriter) {
  47. Document doc;
  48. doc.Parse<0>(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
  49. OutputStringStream os;
  50. Writer<OutputStringStream> writer(os);
  51. doc.Accept(writer);
  52. EXPECT_EQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}", os.str());
  53. }
  54. // Issue 44: SetStringRaw doesn't work with wchar_t
  55. TEST(Document, UTF16_Document) {
  56. GenericDocument< UTF16<> > json;
  57. json.Parse<kParseValidateEncodingFlag>(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]");
  58. ASSERT_TRUE(json.IsArray());
  59. GenericValue< UTF16<> >& v = json[0u];
  60. ASSERT_TRUE(v.IsObject());
  61. GenericValue< UTF16<> >& s = v[L"created_at"];
  62. ASSERT_TRUE(s.IsString());
  63. EXPECT_EQ(0, wcscmp(L"Wed Oct 30 17:13:20 +0000 2012", s.GetString()));
  64. }
  65. // Issue 22: Memory corruption via operator=
  66. // Fixed by making unimplemented assignment operator private.
  67. //TEST(Document, Assignment) {
  68. // Document d1;
  69. // Document d2;
  70. // d1 = d2;
  71. //}