subscript.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonObject::operator[]") {
  7. DynamicJsonDocument doc(4096);
  8. JsonObject obj = doc.to<JsonObject>();
  9. SECTION("int") {
  10. obj["hello"] = 123;
  11. REQUIRE(123 == obj["hello"].as<int>());
  12. REQUIRE(true == obj["hello"].is<int>());
  13. REQUIRE(false == obj["hello"].is<bool>());
  14. }
  15. SECTION("volatile int") { // issue #415
  16. volatile int i = 123;
  17. obj["hello"] = i;
  18. REQUIRE(123 == obj["hello"].as<int>());
  19. REQUIRE(true == obj["hello"].is<int>());
  20. REQUIRE(false == obj["hello"].is<bool>());
  21. }
  22. SECTION("double") {
  23. obj["hello"] = 123.45;
  24. REQUIRE(true == obj["hello"].is<double>());
  25. REQUIRE(false == obj["hello"].is<long>());
  26. REQUIRE(123.45 == obj["hello"].as<double>());
  27. }
  28. SECTION("bool") {
  29. obj["hello"] = true;
  30. REQUIRE(true == obj["hello"].is<bool>());
  31. REQUIRE(false == obj["hello"].is<long>());
  32. REQUIRE(true == obj["hello"].as<bool>());
  33. }
  34. SECTION("const char*") {
  35. obj["hello"] = "h3110";
  36. REQUIRE(true == obj["hello"].is<const char*>());
  37. REQUIRE(false == obj["hello"].is<long>());
  38. REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
  39. }
  40. SECTION("array") {
  41. DynamicJsonDocument doc2(4096);
  42. JsonArray arr = doc2.to<JsonArray>();
  43. obj["hello"] = arr;
  44. REQUIRE(arr == obj["hello"].as<JsonArray>());
  45. REQUIRE(true == obj["hello"].is<JsonArray>());
  46. REQUIRE(false == obj["hello"].is<JsonObject>());
  47. }
  48. SECTION("object") {
  49. DynamicJsonDocument doc2(4096);
  50. JsonObject obj2 = doc2.to<JsonObject>();
  51. obj["hello"] = obj2;
  52. REQUIRE(obj2 == obj["hello"].as<JsonObject>());
  53. REQUIRE(true == obj["hello"].is<JsonObject>());
  54. REQUIRE(false == obj["hello"].is<JsonArray>());
  55. }
  56. SECTION("array subscript") {
  57. DynamicJsonDocument doc2(4096);
  58. JsonArray arr = doc2.to<JsonArray>();
  59. arr.add(42);
  60. obj["a"] = arr[0];
  61. REQUIRE(42 == obj["a"]);
  62. }
  63. SECTION("object subscript") {
  64. DynamicJsonDocument doc2(4096);
  65. JsonObject obj2 = doc2.to<JsonObject>();
  66. obj2["x"] = 42;
  67. obj["a"] = obj2["x"];
  68. REQUIRE(42 == obj["a"]);
  69. }
  70. SECTION("char key[]") { // issue #423
  71. char key[] = "hello";
  72. obj[key] = 42;
  73. REQUIRE(42 == obj[key]);
  74. }
  75. SECTION("should not duplicate const char*") {
  76. obj["hello"] = "world";
  77. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  78. REQUIRE(expectedSize == doc.memoryUsage());
  79. }
  80. SECTION("should duplicate char* value") {
  81. obj["hello"] = const_cast<char*>("world");
  82. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
  83. REQUIRE(expectedSize == doc.memoryUsage());
  84. }
  85. SECTION("should duplicate char* key") {
  86. obj[const_cast<char*>("hello")] = "world";
  87. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
  88. REQUIRE(expectedSize == doc.memoryUsage());
  89. }
  90. SECTION("should duplicate char* key&value") {
  91. obj[const_cast<char*>("hello")] = const_cast<char*>("world");
  92. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(5);
  93. REQUIRE(expectedSize <= doc.memoryUsage());
  94. }
  95. SECTION("should duplicate std::string value") {
  96. obj["hello"] = std::string("world");
  97. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
  98. REQUIRE(expectedSize == doc.memoryUsage());
  99. }
  100. SECTION("should duplicate std::string key") {
  101. obj[std::string("hello")] = "world";
  102. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
  103. REQUIRE(expectedSize == doc.memoryUsage());
  104. }
  105. SECTION("should duplicate std::string key&value") {
  106. obj[std::string("hello")] = std::string("world");
  107. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(5);
  108. REQUIRE(expectedSize <= doc.memoryUsage());
  109. }
  110. SECTION("should duplicate a non-static JsonString key") {
  111. obj[JsonString("hello", JsonString::Copied)] = "world";
  112. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
  113. REQUIRE(expectedSize == doc.memoryUsage());
  114. }
  115. SECTION("should not duplicate a static JsonString key") {
  116. obj[JsonString("hello", JsonString::Linked)] = "world";
  117. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  118. REQUIRE(expectedSize == doc.memoryUsage());
  119. }
  120. SECTION("should ignore null key") {
  121. // object must have a value to make a call to strcmp()
  122. obj["dummy"] = 42;
  123. const char* null = 0;
  124. obj[null] = 666;
  125. REQUIRE(obj.size() == 1);
  126. REQUIRE(obj[null] == null);
  127. }
  128. SECTION("obj[key].to<JsonArray>()") {
  129. JsonArray arr = obj["hello"].to<JsonArray>();
  130. REQUIRE(arr.isNull() == false);
  131. }
  132. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  133. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  134. SECTION("obj[VLA] = str") {
  135. size_t i = 16;
  136. char vla[i];
  137. strcpy(vla, "hello");
  138. obj[vla] = "world";
  139. REQUIRE(std::string("world") == obj["hello"]);
  140. }
  141. SECTION("obj[str] = VLA") { // issue #416
  142. size_t i = 32;
  143. char vla[i];
  144. strcpy(vla, "world");
  145. obj["hello"] = vla;
  146. REQUIRE(std::string("world") == obj["hello"].as<const char*>());
  147. }
  148. SECTION("obj.set(VLA, str)") {
  149. size_t i = 16;
  150. char vla[i];
  151. strcpy(vla, "hello");
  152. obj[vla] = "world";
  153. REQUIRE(std::string("world") == obj["hello"]);
  154. }
  155. SECTION("obj.set(str, VLA)") {
  156. size_t i = 32;
  157. char vla[i];
  158. strcpy(vla, "world");
  159. obj["hello"].set(vla);
  160. REQUIRE(std::string("world") == obj["hello"].as<const char*>());
  161. }
  162. SECTION("obj[VLA]") {
  163. size_t i = 16;
  164. char vla[i];
  165. strcpy(vla, "hello");
  166. deserializeJson(doc, "{\"hello\":\"world\"}");
  167. obj = doc.as<JsonObject>();
  168. REQUIRE(std::string("world") == obj[vla]);
  169. }
  170. #endif
  171. SECTION("chain") {
  172. obj.createNestedObject("hello")["world"] = 123;
  173. REQUIRE(123 == obj["hello"]["world"].as<int>());
  174. REQUIRE(true == obj["hello"]["world"].is<int>());
  175. REQUIRE(false == obj["hello"]["world"].is<bool>());
  176. }
  177. }