subscript.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. REQUIRE(std::string("h3110") == obj["hello"].as<char*>()); // <- short hand
  40. }
  41. SECTION("array") {
  42. DynamicJsonDocument doc2(4096);
  43. JsonArray arr = doc2.to<JsonArray>();
  44. obj["hello"] = arr;
  45. REQUIRE(arr == obj["hello"].as<JsonArray>());
  46. REQUIRE(true == obj["hello"].is<JsonArray>());
  47. REQUIRE(false == obj["hello"].is<JsonObject>());
  48. }
  49. SECTION("object") {
  50. DynamicJsonDocument doc2(4096);
  51. JsonObject obj2 = doc2.to<JsonObject>();
  52. obj["hello"] = obj2;
  53. REQUIRE(obj2 == obj["hello"].as<JsonObject>());
  54. REQUIRE(true == obj["hello"].is<JsonObject>());
  55. REQUIRE(false == obj["hello"].is<JsonArray>());
  56. }
  57. SECTION("array subscript") {
  58. DynamicJsonDocument doc2(4096);
  59. JsonArray arr = doc2.to<JsonArray>();
  60. arr.add(42);
  61. obj["a"] = arr[0];
  62. REQUIRE(42 == obj["a"]);
  63. }
  64. SECTION("object subscript") {
  65. DynamicJsonDocument doc2(4096);
  66. JsonObject obj2 = doc2.to<JsonObject>();
  67. obj2["x"] = 42;
  68. obj["a"] = obj2["x"];
  69. REQUIRE(42 == obj["a"]);
  70. }
  71. SECTION("char key[]") { // issue #423
  72. char key[] = "hello";
  73. obj[key] = 42;
  74. REQUIRE(42 == obj[key]);
  75. }
  76. SECTION("should not duplicate const char*") {
  77. obj["hello"] = "world";
  78. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  79. REQUIRE(expectedSize == doc.memoryUsage());
  80. }
  81. SECTION("should duplicate char* value") {
  82. obj["hello"] = const_cast<char*>("world");
  83. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6);
  84. REQUIRE(expectedSize == doc.memoryUsage());
  85. }
  86. SECTION("should duplicate char* key") {
  87. obj[const_cast<char*>("hello")] = "world";
  88. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6);
  89. REQUIRE(expectedSize == doc.memoryUsage());
  90. }
  91. SECTION("should duplicate char* key&value") {
  92. obj[const_cast<char*>("hello")] = const_cast<char*>("world");
  93. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(6);
  94. REQUIRE(expectedSize <= doc.memoryUsage());
  95. }
  96. SECTION("should duplicate std::string value") {
  97. obj["hello"] = std::string("world");
  98. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6);
  99. REQUIRE(expectedSize == doc.memoryUsage());
  100. }
  101. SECTION("should duplicate std::string key") {
  102. obj[std::string("hello")] = "world";
  103. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6);
  104. REQUIRE(expectedSize == doc.memoryUsage());
  105. }
  106. SECTION("should duplicate std::string key&value") {
  107. obj[std::string("hello")] = std::string("world");
  108. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(6);
  109. REQUIRE(expectedSize <= doc.memoryUsage());
  110. }
  111. SECTION("should duplicate a non-static JsonString key") {
  112. obj[JsonString("hello", false)] = "world";
  113. const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6);
  114. REQUIRE(expectedSize == doc.memoryUsage());
  115. }
  116. SECTION("should not duplicate a static JsonString key") {
  117. obj[JsonString("hello", true)] = "world";
  118. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  119. REQUIRE(expectedSize == doc.memoryUsage());
  120. }
  121. SECTION("should ignore null key") {
  122. // object must have a value to make a call to strcmp()
  123. obj["dummy"] = 42;
  124. const char* null = 0;
  125. obj[null] = 666;
  126. REQUIRE(obj.size() == 1);
  127. REQUIRE(obj[null] == null);
  128. }
  129. SECTION("obj[key].to<JsonArray>()") {
  130. JsonArray arr = obj["hello"].to<JsonArray>();
  131. REQUIRE(arr.isNull() == false);
  132. }
  133. #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
  134. !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
  135. SECTION("obj[VLA] = str") {
  136. int i = 16;
  137. char vla[i];
  138. strcpy(vla, "hello");
  139. obj[vla] = "world";
  140. REQUIRE(std::string("world") == obj["hello"]);
  141. }
  142. SECTION("obj[str] = VLA") { // issue #416
  143. int i = 32;
  144. char vla[i];
  145. strcpy(vla, "world");
  146. obj["hello"] = vla;
  147. REQUIRE(std::string("world") == obj["hello"].as<char*>());
  148. }
  149. SECTION("obj.set(VLA, str)") {
  150. int i = 16;
  151. char vla[i];
  152. strcpy(vla, "hello");
  153. obj[vla] = "world";
  154. REQUIRE(std::string("world") == obj["hello"]);
  155. }
  156. SECTION("obj.set(str, VLA)") {
  157. int i = 32;
  158. char vla[i];
  159. strcpy(vla, "world");
  160. obj["hello"].set(vla);
  161. REQUIRE(std::string("world") == obj["hello"].as<char*>());
  162. }
  163. SECTION("obj[VLA]") {
  164. int i = 16;
  165. char vla[i];
  166. strcpy(vla, "hello");
  167. deserializeJson(doc, "{\"hello\":\"world\"}");
  168. obj = doc.as<JsonObject>();
  169. REQUIRE(std::string("world") == obj[vla]);
  170. }
  171. #endif
  172. SECTION("chain") {
  173. obj.createNestedObject("hello")["world"] = 123;
  174. REQUIRE(123 == obj["hello"]["world"].as<int>());
  175. REQUIRE(true == obj["hello"]["world"].is<int>());
  176. REQUIRE(false == obj["hello"]["world"].is<bool>());
  177. }
  178. }