JSON.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/JSON/rapidjson.h>
  9. #include <AzCore/JSON/document.h> // rapidjson's DOM-style API
  10. #include <AzCore/JSON/prettywriter.h> // for stringify JSON
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. using namespace AZ;
  13. using namespace rapidjson;
  14. namespace UnitTest
  15. {
  16. /**
  17. * Rapid XML parser
  18. */
  19. class RapidJSON
  20. : public LeakDetectionFixture
  21. {
  22. public:
  23. RapidJSON() {}
  24. void run()
  25. {
  26. // rapidjson is already tested (has unittests), for now just run the tututorial
  27. // otherwise we can include all of it's test
  28. const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
  29. Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
  30. // In-situ parsing, decode strings directly in the source string. Source must be string.
  31. char buffer[sizeof(json)];
  32. memcpy(buffer, json, sizeof(json));
  33. AZ_TEST_ASSERT(document.ParseInsitu(buffer).HasParseError() == false);
  34. ////////////////////////////////////////////////////////////////////////////
  35. // 2. Access values in document.
  36. AZ_TEST_ASSERT(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
  37. AZ_TEST_ASSERT(document.HasMember("hello"));
  38. AZ_TEST_ASSERT(document["hello"].IsString());
  39. // Since version 0.2, you can use single lookup to check the existing of member and its value:
  40. Value::MemberIterator hello = document.FindMember("hello");
  41. AZ_TEST_ASSERT(hello != document.MemberEnd());
  42. AZ_TEST_ASSERT(hello->value.IsString());
  43. AZ_TEST_ASSERT(strcmp("world", hello->value.GetString()) == 0);
  44. (void)hello;
  45. AZ_TEST_ASSERT(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().
  46. AZ_TEST_ASSERT(document["f"].IsBool());
  47. AZ_TEST_ASSERT(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.
  48. AZ_TEST_ASSERT(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
  49. AZ_TEST_ASSERT(document["pi"].IsNumber());
  50. AZ_TEST_ASSERT(document["pi"].IsDouble());
  51. {
  52. const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
  53. AZ_TEST_ASSERT(a.IsArray());
  54. int y = a[0].GetInt();
  55. (void)y;
  56. }
  57. // Iterating object members
  58. //static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
  59. for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
  60. {
  61. (void)itr;
  62. //printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);
  63. }
  64. ////////////////////////////////////////////////////////////////////////////
  65. // 3. Modify values in document.
  66. // Change i to a bigger number
  67. {
  68. uint64_t f20 = 1; // compute factorial of 20
  69. for (uint64_t j = 1; j <= 20; j++)
  70. {
  71. f20 *= j;
  72. }
  73. document["i"] = f20; // Alternate form: document["i"].SetUint64(f20)
  74. AZ_TEST_ASSERT(!document["i"].IsInt()); // No longer can be cast as int or uint.
  75. }
  76. // Adding values to array.
  77. {
  78. Value& a = document["a"]; // This time we uses non-const reference.
  79. Document::AllocatorType& allocator = document.GetAllocator();
  80. for (int i = 5; i <= 10; i++)
  81. {
  82. a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.
  83. }
  84. // Fluent API
  85. a.PushBack("Lua", allocator).PushBack("Mio", allocator);
  86. }
  87. // Making string values.
  88. // This version of SetString() just store the pointer to the string.
  89. // So it is for literal and string that exists within value's life-cycle.
  90. {
  91. document["hello"] = "rapidjson"; // This will invoke strlen()
  92. // Faster version:
  93. // document["hello"].SetString("rapidjson", 9);
  94. }
  95. // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
  96. Value author;
  97. {
  98. char lbuffer[10];
  99. int len = azsnprintf(lbuffer, AZ_ARRAY_SIZE(lbuffer), "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string.
  100. author.SetString(lbuffer, static_cast<rapidjson_ly::SizeType>(len), document.GetAllocator());
  101. // Shorter but slower version:
  102. // document["hello"].SetString(buffer, document.GetAllocator());
  103. // Constructor version:
  104. // Value author(lbuffer, len, document.GetAllocator());
  105. // Value author(lbuffer, document.GetAllocator());
  106. memset(lbuffer, 0, sizeof(lbuffer)); // For demonstration purpose.
  107. }
  108. // Variable 'buffer' is unusable now but 'author' has already made a copy.
  109. document.AddMember("author", author, document.GetAllocator());
  110. AZ_TEST_ASSERT(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.
  111. ////////////////////////////////////////////////////////////////////////////
  112. // 4. Stringify JSON
  113. StringBuffer sb;
  114. PrettyWriter<StringBuffer> writer(sb);
  115. document.Accept(writer); // Accept() traverses the DOM and generates Handler events.
  116. // Test error handling
  117. {
  118. Document badDoc;
  119. badDoc.Parse("{ dflakjdflkajdlfkja");
  120. AZ_TEST_ASSERT(badDoc.HasParseError());
  121. AZ_TEST_ASSERT(badDoc.GetParseError() != kParseErrorNone);
  122. AZ_TEST_ASSERT(badDoc.GetErrorOffset() != 0);
  123. }
  124. }
  125. };
  126. TEST_F(RapidJSON, Test)
  127. {
  128. run();
  129. }
  130. }