tutorial.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Hello World example
  2. // This example shows basic usage of DOM-style API.
  3. #include "rapidjson/document.h" // rapidjson's DOM-style API
  4. #include "rapidjson/prettywriter.h" // for stringify JSON
  5. #include "rapidjson/filestream.h" // wrapper of C stream for prettywriter as output
  6. #include <cstdio>
  7. using namespace rapidjson;
  8. int main(int, char*[]) {
  9. ////////////////////////////////////////////////////////////////////////////
  10. // 1. Parse a JSON text string to a document.
  11. const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
  12. printf("Original JSON:\n %s\n", json);
  13. Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
  14. #if 0
  15. // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
  16. if (document.Parse<0>(json).HasParseError())
  17. return 1;
  18. #else
  19. // In-situ parsing, decode strings directly in the source string. Source must be string.
  20. {
  21. char buffer[sizeof(json)];
  22. memcpy(buffer, json, sizeof(json));
  23. if (document.ParseInsitu<0>(buffer).HasParseError())
  24. return 1;
  25. }
  26. #endif
  27. printf("\nParsing to document succeeded.\n");
  28. ////////////////////////////////////////////////////////////////////////////
  29. // 2. Access values in document.
  30. printf("\nAccess values in document:\n");
  31. assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
  32. assert(document.HasMember("hello"));
  33. assert(document["hello"].IsString());
  34. printf("hello = %s\n", document["hello"].GetString());
  35. // Since version 0.2, you can use single lookup to check the existing of member and its value:
  36. Value::Member* hello = document.FindMember("hello");
  37. assert(hello != 0);
  38. assert(hello->value.IsString());
  39. assert(strcmp("world", hello->value.GetString()) == 0);
  40. (void)hello;
  41. assert(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().
  42. printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
  43. assert(document["f"].IsBool());
  44. printf("f = %s\n", document["f"].GetBool() ? "true" : "false");
  45. printf("n = %s\n", document["n"].IsNull() ? "null" : "?");
  46. assert(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.
  47. assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
  48. printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]
  49. assert(document["pi"].IsNumber());
  50. assert(document["pi"].IsDouble());
  51. printf("pi = %g\n", document["pi"].GetDouble());
  52. {
  53. const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
  54. assert(a.IsArray());
  55. for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
  56. printf("a[%d] = %d\n", i, a[i].GetInt());
  57. // Note:
  58. //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
  59. int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work.
  60. int z = a[0u].GetInt(); // This works too.
  61. (void)y;
  62. (void)z;
  63. // Iterating array with iterators
  64. printf("a = ");
  65. for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
  66. printf("%d ", itr->GetInt());
  67. printf("\n");
  68. }
  69. // Iterating object members
  70. static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
  71. for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
  72. printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);
  73. ////////////////////////////////////////////////////////////////////////////
  74. // 3. Modify values in document.
  75. // Change i to a bigger number
  76. {
  77. uint64_t f20 = 1; // compute factorial of 20
  78. for (uint64_t j = 1; j <= 20; j++)
  79. f20 *= j;
  80. document["i"] = f20; // Alternate form: document["i"].SetUint64(f20)
  81. assert(!document["i"].IsInt()); // No longer can be cast as int or uint.
  82. }
  83. // Adding values to array.
  84. {
  85. Value& a = document["a"]; // This time we uses non-const reference.
  86. Document::AllocatorType& allocator = document.GetAllocator();
  87. for (int i = 5; i <= 10; i++)
  88. a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.
  89. // Fluent API
  90. a.PushBack("Lua", allocator).PushBack("Mio", allocator);
  91. }
  92. // Making string values.
  93. // This version of SetString() just store the pointer to the string.
  94. // So it is for literal and string that exists within value's life-cycle.
  95. {
  96. document["hello"] = "rapidjson"; // This will invoke strlen()
  97. // Faster version:
  98. // document["hello"].SetString("rapidjson", 9);
  99. }
  100. // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
  101. Value author;
  102. {
  103. char buffer[10];
  104. int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string.
  105. author.SetString(buffer, static_cast<size_t>(len), document.GetAllocator());
  106. // Shorter but slower version:
  107. // document["hello"].SetString(buffer, document.GetAllocator());
  108. // Constructor version:
  109. // Value author(buffer, len, document.GetAllocator());
  110. // Value author(buffer, document.GetAllocator());
  111. memset(buffer, 0, sizeof(buffer)); // For demonstration purpose.
  112. }
  113. // Variable 'buffer' is unusable now but 'author' has already made a copy.
  114. document.AddMember("author", author, document.GetAllocator());
  115. assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.
  116. ////////////////////////////////////////////////////////////////////////////
  117. // 4. Stringify JSON
  118. printf("\nModified JSON with reformatting:\n");
  119. FileStream f(stdout);
  120. PrettyWriter<FileStream> writer(f);
  121. document.Accept(writer); // Accept() traverses the DOM and generates Handler events.
  122. return 0;
  123. }