subscript.cpp 5.6 KB

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